A data manipulation paradigm shiftSunday, September 4, 11
Why 	 	 	 ?                Slow as hell                PHP 4 support                Why are you even here at CakeFest?Sund...
Ok, seriously...                Easy to follow conventions                Flexible in awesome proportions                P...
But.. (there’s always one)                Frankenstein-like implementation of                ActiveRecord                C...
A faint memory of CS theory                Objects should be cohesive and                decoupled from each otherSunday, ...
I wanted This!                                +                                       Well, not quite...Sunday, September ...
I wanted the POPOs                                           You would do this using afterFind in Cake                    ...
why		 	 	 	 	 ?                Very mature data abstraction layer                Establishing as the de-facto persistence ...
My personal reasons                Writing a ORM/ODM is hard and time                consuming (already spent weeks portin...
My original idea                Use doctrine to support Model returning                objects in CakePHP as a plugin for ...
Why		 	 	 	 	 ?                Smaller set of options, allowed me to quickly                implement most common features...
Why		 	 	 	 	 ?                They say it’s fast!                Something between NoSQL and relational                In...
+                A natural way to create a database: one php                object corresponds to one document            ...
+               A PHP object represents a               document in a collection               Settings are described with...
+             Each embedded document is             represented by another object             Objects can implement their ...
+       Queries are done through a       repository object       Doctrine finds are very       similar to CakePHP ones     ...
Lazy Queries         Queries can be created using         a builder         array(‘field’ =>$value) is         similar, but...
My plan                  Map $object->property access to method                  accessor if the property was unreachable ...
Accessing this property will automatically                          call the respective setUserName() and                 ...
Can also be used to access associated                               documents’ properties                          Array A...
associationsSunday, September 4, 11
Array Query BuildersSunday, September 4, 11
More Items in my plan                  Support model callbacks                  Validation                  Seamless prope...
BAKING MY PLAN     	 	 	 	 +	 	 	 	 	 	 	 	 	 +              	Sunday, September 4, 11
A Document                          You need to import CakeDocument and use it as                          a base class fo...
Associations                            Aliases are used to map data                            coming from a form to a pr...
Association data                  Association data is automatically loaded with the                  object               ...
Finding                                                             A query array takes the same keys as using            ...
Custom Finders      Similar to normal custom finders but can also be defined statically as an      array in the class definit...
Saving your data                                save() is always saveAll()                                You can alter ob...
Almost like Home                Validation is done in the same way as you’re used to                You have (almost) all ...
In ControllersSunday, September 4, 11
Pagination                             DocumentPaginator                             You can keep stacking                ...
Authentication                                 Use DocumentAuth                                 Auth->user() will         ...
Time for a quick                               demo?                                    thanks for attending!             ...
Upcoming SlideShare
Loading in …5
×

Mongo Cake Plugin for CakePHP 2.0

7,364 views

Published on

Published in: Technology

Mongo Cake Plugin for CakePHP 2.0

  1. 1. A data manipulation paradigm shiftSunday, 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 minutesSunday, 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 layerSunday, September 4, 11
  5. 5. A faint memory of CS theory Objects should be cohesive and decoupled from each otherSunday, 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 themSunday, 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 withSunday, 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 buzzwordSunday, 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 stuffSunday, 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 associationsSunday, 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 propertiesSunday, 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 accessSunday, 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 waySunday, 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 arraysSunday, 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 builderSunday, September 4, 11
  19. 19. Accessing this property will automatically call the respective setUserName() and getUserName() methods! transparent AccessorSSunday, September 4, 11
  20. 20. Can also be used to access associated documents’ properties Array AccessSunday, September 4, 11
  21. 21. associationsSunday, September 4, 11
  22. 22. Array Query BuildersSunday, 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 scopesSunday, 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 mongoDBSunday, 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 likeSunday, 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 iteratedSunday, 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 FormHelperSunday, September 4, 11
  32. 32. In ControllersSunday, 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/MongoCakeSunday, September 4, 11

×