mongo cake plugin for cakephp 2.0
TRANSCRIPT
![Page 1: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/1.jpg)
MongoCake PluginA data manipulation paradigm shift
Sunday, September 4, 11
![Page 2: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/2.jpg)
Why ?
Slow as hell
PHP 4 support
Why are you even here at CakeFest?
Sunday, September 4, 11
![Page 3: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/3.jpg)
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
![Page 4: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/4.jpg)
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
![Page 5: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/5.jpg)
A faint memory of CS theory
Objects should be cohesive and decoupled from each other
Sunday, September 4, 11
![Page 6: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/6.jpg)
I wanted This!
+
Well, not quite...
Sunday, September 4, 11
![Page 7: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/7.jpg)
I wanted the POPOs
But I wanted to automatically persist them
You would do this using afterFind in Cake
Sunday, September 4, 11
![Page 8: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/8.jpg)
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
![Page 9: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/9.jpg)
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
![Page 10: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/10.jpg)
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
![Page 11: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/11.jpg)
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
![Page 12: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/12.jpg)
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
![Page 13: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/13.jpg)
+
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
![Page 14: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/14.jpg)
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
![Page 15: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/15.jpg)
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
![Page 16: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/16.jpg)
+
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
![Page 17: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/17.jpg)
Lazy QueriesQueries 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
![Page 18: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/18.jpg)
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
![Page 19: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/19.jpg)
transparent AccessorS
Accessing this property will automaticallycall the respective setUserName() and getUserName() methods!
Sunday, September 4, 11
![Page 20: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/20.jpg)
Array Access
Can also be used to access associated documents’ properties
Sunday, September 4, 11
![Page 21: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/21.jpg)
associations
Sunday, September 4, 11
![Page 22: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/22.jpg)
Array Query Builders
Sunday, September 4, 11
![Page 23: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/23.jpg)
More Items in my planSupport model callbacks
Validation
Seamless property setting from a form submission
Pagination
Authentication using a User document
Named scopes
Sunday, September 4, 11
![Page 24: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/24.jpg)
+ +
BAKING MY PLAN
Sunday, September 4, 11
![Page 25: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/25.jpg)
A Document
You need to import CakeDocument and use it asa base class for your Documents.
Annotate your classes with normal Doctrine annotationsProperties 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.
Import associated documents at the beginning
You can set defaults to be saved in mongoDB
Sunday, September 4, 11
![Page 26: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/26.jpg)
Associations
Tries to emulate as much as possible the association names in CakePHP
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)
Check Doctrine documentation for defining more complex associations (limit, conditions, cascade, etc..)
Sunday, September 4, 11
![Page 27: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/27.jpg)
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
![Page 28: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/28.jpg)
FindingA 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
![Page 29: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/29.jpg)
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
![Page 30: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/30.jpg)
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
![Page 31: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/31.jpg)
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
![Page 32: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/32.jpg)
In Controllers
Sunday, September 4, 11
![Page 33: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/33.jpg)
Pagination
DocumentPaginator
You can keep stacking finders after calling paginate()
Iterate in your views as you’re used to.
Sunday, September 4, 11
![Page 34: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/34.jpg)
Authentication
Use DocumentAuth
Auth->user() will return the document object
Don’t look for array data directly in the session!
Sunday, September 4, 11
![Page 35: Mongo Cake Plugin for CakePHP 2.0](https://reader035.vdocuments.mx/reader035/viewer/2022062220/5558246cd8b42a25588b4d03/html5/thumbnails/35.jpg)
Time for a quick demo?
thanks for attending!http://github.com/lorenzo/MongoCake
Sunday, September 4, 11