Mongo Cake Plugin for CakePHP 2.0

Post on 17-May-2015

6.665 views 2 download

Tags:

Transcript of Mongo Cake Plugin for CakePHP 2.0

MongoCake PluginA data manipulation paradigm shift

Sunday, September 4, 11

Why ?

Slow as hell

PHP 4 support

Why are you even here at CakeFest?

Sunday, September 4, 11

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

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

A faint memory of CS theory

Objects should be cohesive and decoupled from each other

Sunday, September 4, 11

I wanted This!

+

Well, not quite...

Sunday, September 4, 11

I wanted the POPOs

But I wanted to automatically persist them

You would do this using afterFind in Cake

Sunday, September 4, 11

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

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

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

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

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

+

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

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

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

+

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

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

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

transparent AccessorS

Accessing this property will automaticallycall the respective setUserName() and getUserName() methods!

Sunday, September 4, 11

Array Access

Can also be used to access associated documents’ properties

Sunday, September 4, 11

associations

Sunday, September 4, 11

Array Query Builders

Sunday, September 4, 11

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

+ +

BAKING MY PLAN

Sunday, September 4, 11

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

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

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

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

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

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

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

In Controllers

Sunday, September 4, 11

Pagination

DocumentPaginator

You can keep stacking finders after calling paginate()

Iterate in your views as you’re used to.

Sunday, September 4, 11

Authentication

Use DocumentAuth

Auth->user() will return the document object

Don’t look for array data directly in the session!

Sunday, September 4, 11

Time for a quick demo?

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

Sunday, September 4, 11