Successfully reported this slideshow.
We use your LinkedIn profile and activity data to personalize ads and to show you more relevant ads. You can change your ad preferences anytime.

Entity Query API


Published on

Entity field query api session

  • Be the first to comment

  • Be the first to like this

Entity Query API

  1. 1. Marc Ingram Entity Field Query API
  2. 2. The need for a query builder <ul><li>Where we are now </li><ul><li>Drupal 6 has no concept of a query builder </li><ul><li> Feb 2008 </li></ul><li>Every query must be hand crafted for a specific database
  3. 3. View offers the ability to create custom queries on the fly </li><ul><li>View 3 allows for plugable backends
  4. 4. Views based queries are not always optimal </li></ul><li>D7 changes this </li></ul></ul>
  5. 5. Views in core <ul><li>Why for developers? </li><ul><li>No GUI
  6. 6. Lacks the structured tpl structure that views provides
  7. 7. Need to be able to write code </li></ul><li>Why is this possible? </li><ul><li>Fields in core
  8. 8. Plugable storage </li></ul></ul>
  9. 9. Fields in Core <ul><li>What does fields in core mean? </li><ul><li>Bundles/Entities </li><ul><li>This is the basic building block </li><ul><li>Taxonomy terms, Taxonomy vocabularies, Users, Nodes, Files </li></ul><li>You can build your own </li></ul><li>Fields </li><ul><li>Allows you define fields that can be used by entities </li></ul><li>Instances </li><ul><li>Associates fields with given entities and bundles </li></ul></ul></ul>
  10. 10. Entities and bundles <ul><li>hook_entity_info() </li><ul><li>Allows you to create new entities
  11. 11. Example of a node entity
  12. 12. hook_entity_info_alter() </li></ul><li>Bundle is a sub class of an entity </li><ul><li>CCK content type
  13. 13. Taxonomy terms are classified by vocabularies
  14. 14. Bundles can have there own properties </li></ul></ul>
  15. 15. Fields <ul><li>All entities are now fieldable </li><ul><li>This means the concept of cck is no longer restricted to nodes </li></ul><li>Core comes with predefined field types </li><ul><li>Text, Options, Number, List </li></ul><li>CCK minus user and node reference these are still contrib
  16. 16. Defining a field </li></ul>
  17. 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. 18. Bundles <ul><li>A bundle is a collection of fields associated with an entity </li><ul><li>Sub types can have different bundles – story vs business
  19. 19. Widgets can be defined here along with display modes </li><ul><li>Each field can have a different widget on a different bundle </li><ul><li>field_update_instance
  20. 20. field_create_instance </li></ul></ul></ul></ul>
  21. 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. 22. Entity field api <ul><li>Entity query api </li><ul><li>The ability to create queries that are agnostic with regards field storage engines
  23. 23. What does that mean? </li><ul><li>What is field storage?
  24. 24. Drupal 7 we don't need to store fields in just mysql </li><ul><li>Main choices at the moment </li><ul><li>sql_storage (core)
  25. 25. Mongodb and actively being used by </li></ul><li>Will allow for sites to select the most appropriate storage mechanism but for scalability nosql is likely to become the weapon of choice </li></ul><li>The same query will work over multiple storage engines </li></ul></ul></ul>
  26. 26. Entity query api <ul><li>What does a entity field query look like
  27. 27. Filters </li><ul><li>Entity conditions
  28. 28. Property conditions
  29. 29. Field conditions </li></ul><li>Sorts, Count, Limit
  30. 30. More here </li></ul>
  31. 31. Examples <ul><li>How to roll your own </li><ul><li>hook_field_storage_query() </li></ul><li>Now for some code </li><ul><li>An example query </li><ul><li>Benefits of entity_cache and memcache </li></ul></ul><li>Native queries </li><ul><li>Mysql
  32. 32. Mongo </li></ul></ul>
  33. 33. No Kittens were harmed <ul><li>Didn't we mention portable queries..... </li><ul><li>A demo and code walkthrough in which no kittens are harmed!!! </li><ul><li>Mysql vs mongodb spot the difference
  34. 34. Some basic mongo cli queries
  35. 35. The joy of theming!! </li></ul></ul><li>Why D7 has multiple options for storage </li><ul><li>Not all sites have the same need
  36. 36. The nature of d7 storage in mysql is problematic for scalability
  37. 37. New api allows for backends to swap out with little rework in the drupal layer </li></ul></ul>
  38. 38. Views in core?? <ul><li>Entity query api </li><ul><li>Negatives </li><ul><li>Lacks the flexibility of views
  39. 39. Lack the templating structure of view
  40. 40. Has no concept of relationships </li></ul><li>But.... </li><ul><li>Database agnostic
  41. 41. Supports all queries in views that do need relationships
  42. 42. Lightweight
  43. 43. Renderable arrays make theming a joy anyway!!! </li></ul></ul></ul>
  44. 44. Thank you <ul><li>Any questions? </li></ul>