Successfully reported this slideshow.
Your SlideShare is downloading. ×

Mongo Cake Plugin for CakePHP 2.0

Advertisement

More Related Content

Advertisement

Mongo Cake Plugin for CakePHP 2.0

  1. 1. A data manipulation paradigm shift Sunday, September 4, 11
  2. 2. Why ? Slow as hell PHP 4 support Why are you even here at CakeFest? Sunday, September 4, 11
  3. 3. Ok, seriously... Easy to follow conventions Flexible in awesome proportions Pragmatic Good set of tools to get you productive within minutes Sunday, September 4, 11
  4. 4. But.. (there’s always one) Frankenstein-like implementation of ActiveRecord Callbacks not firing on associations (Translate behavior anyone?) Models (still) returning arrays for results Arrays are not encouraging users to have a fat model layer Sunday, September 4, 11
  5. 5. A faint memory of CS theory Objects should be cohesive and decoupled from each other Sunday, September 4, 11
  6. 6. I wanted This! + Well, not quite... Sunday, September 4, 11
  7. 7. I wanted the POPOs You would do this using afterFind in Cake But I wanted to automatically persist them Sunday, September 4, 11
  8. 8. why ? Very mature data abstraction layer Establishing as the de-facto persistence library in PHP Does not impose anything on your objects, they are just POPOs Support for callbacks, dynamic query building, result lazy loading... Sunday, September 4, 11
  9. 9. My personal reasons Writing a ORM/ODM is hard and time consuming (already spent weeks porting DboSource class to use PDO) It’s already done, tested and used in other projects and frameworks Looked like it would stay out of the way when integrating it with Sunday, September 4, 11
  10. 10. My original idea Use doctrine to support Model returning objects in CakePHP as a plugin for 2.0 Conquer the world RDBMS world is too broad, I needed scope restrictions... Sunday, September 4, 11
  11. 11. Why ? Smaller set of options, allowed me to quickly implement most common features for a new model layer quickly Schema is created from PHP classes and not the other way (think faster fixture schema) It’s a common conference buzzword Sunday, September 4, 11
  12. 12. Why ? They say it’s fast! Something between NoSQL and relational Indices and references! Embedded documents It’s OK for typical CRUD stuff Sunday, September 4, 11
  13. 13. + A natural way to create a database: one php object corresponds to one document Documents consists of properties, accessors and more embedded documents You can also nest objects in PHP, so it still feels natural for expressing associations Sunday, September 4, 11
  14. 14. + A PHP object represents a document in a collection Settings are described with annotations in comments You can reference or embed other documents Associations are created through annotated properties Sunday, September 4, 11
  15. 15. + Each embedded document is represented by another object Objects can implement their own constructors to initialize internal properties It’s not mandatory to use private + setters & getters But, I also need the usual CakePHP array access Sunday, September 4, 11
  16. 16. + Queries are done through a repository object Doctrine finds are very similar to CakePHP ones It also has magic methods for mapping field names to query conditions But, I wanted a lazier way Sunday, September 4, 11
  17. 17. Lazy Queries Queries can be created using a builder array(‘field’ =>$value) is similar, but this is using class methods Will not issue an actual query to MongoDB unless accessed the first result I know people are used to creating queries with arrays Sunday, September 4, 11
  18. 18. My plan Map $object->property access to method accessor if the property was unreachable and had an accessor Implement the good old CakePHP array access Have a way to describe associations that are not too different from Cake CakePHP finders and array query builder Sunday, September 4, 11
  19. 19. Accessing this property will automatically call the respective setUserName() and getUserName() methods! transparent AccessorS Sunday, September 4, 11
  20. 20. Can also be used to access associated documents’ properties Array Access Sunday, September 4, 11
  21. 21. associations Sunday, September 4, 11
  22. 22. Array Query Builders Sunday, September 4, 11
  23. 23. More Items in my plan Support model callbacks Validation Seamless property setting from a form submission Pagination Authentication using a User document Named scopes Sunday, September 4, 11
  24. 24. BAKING MY PLAN + + Sunday, September 4, 11
  25. 25. A Document You need to import CakeDocument and use it as a base class for your Documents. Import associated documents at the beginning Annotate your classes with normal Doctrine annotations Properties can be public. The id should always be private. Setting properties as protected or private helps you build additional logic around them, such as sanitization, validation, etc. You can set defaults to be saved in mongoDB Sunday, September 4, 11
  26. 26. Associations Aliases are used to map data coming from a form to a property in the document Association types with the suffix Embedded will save data in the save document. (sorry, there’s no BelongsToEmbedded) Tries to emulate as much as possible the association names in CakePHP Check Doctrine documentation for defining more complex associations (limit, conditions, cascade, etc..) Sunday, September 4, 11
  27. 27. Association data Association data is automatically loaded with the object You can access as many levels of associations as you like Sunday, September 4, 11
  28. 28. Finding A query array takes the same keys as using normal CakePHP models (fields, conditions, order, limit, offset) Operators where implemented as in normal CakePHP models (!=, <, >, between...) Can query nested property attributes You still have access to the Doctrine API Finders are lazy, they will return a Query object so you can keep appending conditions to it. It will return values when iterated Sunday, September 4, 11
  29. 29. Custom Finders Similar to normal custom finders but can also be defined statically as an array in the class definition. Optionally use a third argument for extra options: User::recent(5) Sunday, September 4, 11
  30. 30. Saving your data save() is always saveAll() You can alter object properties directly and then save Array data uses the targetDocument name for aliasing associations if no alias is defined. Don’t forget to flush! (once per request should do it) Sunday, September 4, 11
  31. 31. Almost like Home Validation is done in the same way as you’re used to You have (almost) all the callbacks (beforeValidate, beforeSave, afterSave, beforeDelete, afterDelete) created and modified properties are auto populated Interacts very nicely with the FormHelper Sunday, September 4, 11
  32. 32. In Controllers Sunday, September 4, 11
  33. 33. Pagination DocumentPaginator You can keep stacking finders after calling paginate() Iterate in your views as you’re used to. Sunday, September 4, 11
  34. 34. Authentication Use DocumentAuth Auth->user() will return the document object Don’t look for array data directly in the session! Sunday, September 4, 11
  35. 35. Time for a quick demo? thanks for attending! http://github.com/lorenzo/MongoCake Sunday, September 4, 11

×