Mongo Cake Plugin for CakePHP 2.0

35
MongoCake Plugin A data manipulation paradigm shift Sunday, September 4, 11

Transcript of Mongo Cake Plugin for CakePHP 2.0

Page 1: Mongo Cake Plugin for CakePHP 2.0

MongoCake PluginA data manipulation paradigm shift

Sunday, September 4, 11

Page 2: Mongo Cake Plugin for CakePHP 2.0

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

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

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

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

I wanted This!

+

Well, not quite...

Sunday, September 4, 11

Page 7: Mongo Cake Plugin for CakePHP 2.0

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

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

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

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

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

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

+

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

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

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

+

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

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

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

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

Array Access

Can also be used to access associated documents’ properties

Sunday, September 4, 11

Page 21: Mongo Cake Plugin for CakePHP 2.0

associations

Sunday, September 4, 11

Page 22: Mongo Cake Plugin for CakePHP 2.0

Array Query Builders

Sunday, September 4, 11

Page 23: Mongo Cake Plugin for CakePHP 2.0

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

+ +

BAKING MY PLAN

Sunday, September 4, 11

Page 25: Mongo Cake Plugin for CakePHP 2.0

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

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

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

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

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

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

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

In Controllers

Sunday, September 4, 11

Page 33: Mongo Cake Plugin for CakePHP 2.0

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

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

Time for a quick demo?

thanks for attending!http://github.com/lorenzo/MongoCake

Sunday, September 4, 11