Entity Query API

download Entity Query API

If you can't read please download the document

description

Entity field query api session

Transcript of Entity Query API

  • 1. Marc Ingram Entity Field Query API Examiner.com http://drupal.org/user/77320

2. The need for a query builder

  • Where we are now
  • Drupal 6 has no concept of a query builder
  • http://buytaert.net/starting-to-work-on-drupal-7Feb 2008

Every query must be hand crafted for a specific database 3. View offers the ability to create custom queries on the fly

  • View 3 allows for plugable backends

4. Views based queries are not always optimal D7 changes this 5. Views in core

  • Why for developers?
  • No GUI

6. Lacks the structured tpl structure that views provides 7. Need to be able to write code Why is this possible?

  • Fields in core

8. Plugable storage 9. Fields in Core

  • What does fields in core mean?
  • Bundles/Entities
  • This is the basic building block
  • Taxonomy terms, Taxonomy vocabularies, Users, Nodes, Files

You can build your own Fields

  • Allows you define fields that can be used by entities

Instances

  • Associates fields with given entities and bundles

10. Entities and bundles

  • hook_entity_info()
  • Allows you to create new entities

11. Example of a node entity 12. hook_entity_info_alter() Bundle is a sub class of an entity

  • CCK content type

13. Taxonomy terms are classified by vocabularies 14. Bundles can have there own properties 15. Fields

  • All entities are now fieldable
  • This means the concept of cck is no longer restricted to nodes

Core comes with predefined field types

  • Text, Options, Number, List

CCK minus user and node reference these are still contrib 16. Defining a field 17. Creating a field $vocabulary = (object) array('name' => 'Newsworthy subject', 'machine_name'=> 'newsworthy_subject'); taxonomy_vocabulary_save($vocabulary); $vocabularies = taxonomy_vocabulary_get_names(); $field = array( 'field_name' => 'newsworthy_subject', 'type' => 'taxonomy_term_reference', 'settings' => array( 'required' => FALSE, 'allowed_values' => array( array( 'vid' => $vocabularies['newsworthy_subject']->vid, 'parent' => 0, ), ), ), ); field_create_field($field); 18. Bundles

  • A bundle is a collection of fields associated with an entity
  • Sub types can have different bundles story vs business

19. Widgets can be defined here along with display modes

  • Each field can have a different widget on a different bundle
  • field_update_instance

20. field_create_instance 21. Updating a field foreach (ex_taxonomy_get_content_types() as $content_type) { $content_type = node_type_set_defaults($content_type); node_type_save($content_type); // just for good measure - it won't do anything if the field is // already present node_add_body_field($content_type); // set the weight of the body instances. $body_instance = field_info_instance('node', 'body', $content_type->type); $body_instance['required'] = TRUE; $body_instance['widget']['weight'] = 1.2; $body_instance['display']['full'] = array( 'label' => 'hidden', 'type' => 'text_default', ); field_update_instance($body_instance); } 22. Entity field api

  • Entity query api
  • The ability to create queries that are agnostic with regards field storage engines

23. What does that mean?

  • What is field storage?

24. Drupal 7 we don't need to store fields in just mysql

  • Main choices at the moment
  • sql_storage (core)

25. Mongodb and actively being used by examiner.com Will allow for sites to select the most appropriate storage mechanism but for scalability nosql is likely to become the weapon of choice The same query will work over multiple storage engines 26. Entity query api

  • What does a entity field query look like

27. Filters

  • Entity conditions

28. Property conditions 29. Field conditions Sorts, Count, Limit 30. More here http://api.scratch.drupal.org/api/drupal/includes--entity.inc/class/EntityFieldQuery/7 31. Examples

  • How to roll your own
  • hook_field_storage_query()

Now for some code

  • An example query
  • Benefits of entity_cache and memcache

Native queries

  • Mysql

32. Mongo 33. No Kittens were harmed

  • Didn't we mention portable queries.....
  • A demo and code walkthrough in which no kittens are harmed!!!
  • Mysql vs mongodb spot the difference

34. Some basic mongo cli queries 35. The joy of theming!! Why D7 has multiple options for storage

  • Not all sites have the same need

36. The nature of d7 storage in mysql is problematic for scalability 37. New api allows for backends to swap out with little rework in the drupal layer 38. Views in core??

  • Entity query api
  • Negatives
  • Lacks the flexibility of views

39. Lack the templating structure of view 40. Has no concept of relationships But....

  • Database agnostic

41. Supports all queries in views that do need relationships 42. Lightweight 43. Renderable arrays make theming a joy anyway!!! 44. Thank you

  • Any questions?