SlideShare une entreprise Scribd logo
1  sur  35
Télécharger pour lire hors ligne
A 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

                                           You would do this using afterFind in Cake




                     But I wanted to automatically persist them

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 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
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
Accessing this property will automatically
                          call the respective setUserName() and
                          getUserName() methods!




      transparent AccessorS

Sunday, September 4, 11
Can also be used to access associated
                               documents’ properties




                          Array Access

Sunday, September 4, 11
associations

Sunday, September 4, 11
Array Query Builders
Sunday, September 4, 11
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
BAKING MY PLAN




     	 	 	 	 +	 	 	 	 	 	 	 	 	 +
              	

Sunday, September 4, 11
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
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
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
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
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

Contenu connexe

Similaire à Mongo Cake Plugin for CakePHP 2.0

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comtutorialsruby
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comayandoesnotemail
 
TripCase Unit Testing with Jasmine
TripCase Unit Testing with JasmineTripCase Unit Testing with Jasmine
TripCase Unit Testing with JasmineStephen Pond
 
Rcos presentation
Rcos presentationRcos presentation
Rcos presentationmskmoorthy
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperNyros Technologies
 
Hierarchical Systems Policy Management in a Puppet/LDAP Environment
Hierarchical Systems Policy Management in a Puppet/LDAP EnvironmentHierarchical Systems Policy Management in a Puppet/LDAP Environment
Hierarchical Systems Policy Management in a Puppet/LDAP EnvironmentPuppet
 
Drupal and the rise of the documents
Drupal and the rise of the documentsDrupal and the rise of the documents
Drupal and the rise of the documentsClaudio Beatrice
 
ElasticSearch in Production: lessons learned
ElasticSearch in Production: lessons learnedElasticSearch in Production: lessons learned
ElasticSearch in Production: lessons learnedBeyondTrees
 
SWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebSWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebPascal-Nicolas Becker
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Nilesh Jayanandana
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Addy Osmani
 
Selenium webdriver course content rakesh hansalia
Selenium webdriver course content rakesh hansaliaSelenium webdriver course content rakesh hansalia
Selenium webdriver course content rakesh hansaliaRakesh Hansalia
 
Third party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationshipThird party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationshipSascha Brinkmann
 
Introduction to the core.ns application framework
Introduction to the core.ns application frameworkIntroduction to the core.ns application framework
Introduction to the core.ns application frameworkVladimir Ulogov
 
Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Phase2
 
Drupal 8 meets to symphony
Drupal 8 meets to symphonyDrupal 8 meets to symphony
Drupal 8 meets to symphonyBrahampal Singh
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS DirectivesChristian Lilley
 

Similaire à Mongo Cake Plugin for CakePHP 2.0 (20)

oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
oop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.comoop_in_php_tutorial_for_killerphp.com
oop_in_php_tutorial_for_killerphp.com
 
Oop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.comOop in php_tutorial_for_killerphp.com
Oop in php_tutorial_for_killerphp.com
 
Oop in php tutorial
Oop in php tutorialOop in php tutorial
Oop in php tutorial
 
TripCase Unit Testing with Jasmine
TripCase Unit Testing with JasmineTripCase Unit Testing with Jasmine
TripCase Unit Testing with Jasmine
 
Rcos presentation
Rcos presentationRcos presentation
Rcos presentation
 
Oops in PHP By Nyros Developer
Oops in PHP By Nyros DeveloperOops in PHP By Nyros Developer
Oops in PHP By Nyros Developer
 
Hierarchical Systems Policy Management in a Puppet/LDAP Environment
Hierarchical Systems Policy Management in a Puppet/LDAP EnvironmentHierarchical Systems Policy Management in a Puppet/LDAP Environment
Hierarchical Systems Policy Management in a Puppet/LDAP Environment
 
Drupal and the rise of the documents
Drupal and the rise of the documentsDrupal and the rise of the documents
Drupal and the rise of the documents
 
ElasticSearch in Production: lessons learned
ElasticSearch in Production: lessons learnedElasticSearch in Production: lessons learned
ElasticSearch in Production: lessons learned
 
SWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic WebSWIB14 Weaving repository contents into the Semantic Web
SWIB14 Weaving repository contents into the Semantic Web
 
Oop in php_tutorial
Oop in php_tutorialOop in php_tutorial
Oop in php_tutorial
 
Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6Introduction to Ecmascript - ES6
Introduction to Ecmascript - ES6
 
Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)Future-proofing Your JavaScript Apps (Compact edition)
Future-proofing Your JavaScript Apps (Compact edition)
 
Selenium webdriver course content rakesh hansalia
Selenium webdriver course content rakesh hansaliaSelenium webdriver course content rakesh hansalia
Selenium webdriver course content rakesh hansalia
 
Third party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationshipThird party libraries and OSGi - a complicated relationship
Third party libraries and OSGi - a complicated relationship
 
Introduction to the core.ns application framework
Introduction to the core.ns application frameworkIntroduction to the core.ns application framework
Introduction to the core.ns application framework
 
Build and Deploy Sites Using Features
Build and Deploy Sites Using Features Build and Deploy Sites Using Features
Build and Deploy Sites Using Features
 
Drupal 8 meets to symphony
Drupal 8 meets to symphonyDrupal 8 meets to symphony
Drupal 8 meets to symphony
 
Intro to Angular.JS Directives
Intro to Angular.JS DirectivesIntro to Angular.JS Directives
Intro to Angular.JS Directives
 

Plus de José Lorenzo Rodríguez Urdaneta (7)

Agile database access with CakePHP 3
Agile database access with CakePHP 3Agile database access with CakePHP 3
Agile database access with CakePHP 3
 
Faster develoment with CakePHP 3
Faster develoment with CakePHP 3Faster develoment with CakePHP 3
Faster develoment with CakePHP 3
 
Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3Advanced Querying with CakePHP 3
Advanced Querying with CakePHP 3
 
CakeFest 2013 keynote
CakeFest 2013 keynoteCakeFest 2013 keynote
CakeFest 2013 keynote
 
CakePHP 3.0: Embracing the future
CakePHP 3.0: Embracing the futureCakePHP 3.0: Embracing the future
CakePHP 3.0: Embracing the future
 
ZeroMQ in PHP
ZeroMQ in PHPZeroMQ in PHP
ZeroMQ in PHP
 
Making the most out of CakePHP 2.2
Making the most out of CakePHP 2.2Making the most out of CakePHP 2.2
Making the most out of CakePHP 2.2
 

Dernier

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelDeepika Singh
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamUiPathCommunity
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistandanishmna97
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...apidays
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontologyjohnbeverley2021
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businesspanagenda
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodJuan lago vázquez
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerThousandEyes
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyKhushali Kathiriya
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoffsammart93
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingEdi Saputra
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century educationjfdjdjcjdnsjd
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDropbox
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...apidays
 

Dernier (20)

Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot ModelMcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
Mcleodganj Call Girls 🥰 8617370543 Service Offer VIP Hot Model
 
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 AmsterdamDEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
DEV meet-up UiPath Document Understanding May 7 2024 Amsterdam
 
CNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In PakistanCNIC Information System with Pakdata Cf In Pakistan
CNIC Information System with Pakdata Cf In Pakistan
 
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
Apidays New York 2024 - Passkeys: Developing APIs to enable passwordless auth...
 
Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..Understanding the FAA Part 107 License ..
Understanding the FAA Part 107 License ..
 
Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Six Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal OntologySix Myths about Ontologies: The Basics of Formal Ontology
Six Myths about Ontologies: The Basics of Formal Ontology
 
Why Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire businessWhy Teams call analytics are critical to your entire business
Why Teams call analytics are critical to your entire business
 
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin WoodPolkadot JAM Slides - Token2049 - By Dr. Gavin Wood
Polkadot JAM Slides - Token2049 - By Dr. Gavin Wood
 
How to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected WorkerHow to Troubleshoot Apps for the Modern Connected Worker
How to Troubleshoot Apps for the Modern Connected Worker
 
Artificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : UncertaintyArtificial Intelligence Chap.5 : Uncertainty
Artificial Intelligence Chap.5 : Uncertainty
 
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot TakeoffStrategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
Strategize a Smooth Tenant-to-tenant Migration and Copilot Takeoff
 
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost SavingRepurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
Repurposing LNG terminals for Hydrogen Ammonia: Feasibility and Cost Saving
 
presentation ICT roal in 21st century education
presentation ICT roal in 21st century educationpresentation ICT roal in 21st century education
presentation ICT roal in 21st century education
 
DBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor PresentationDBX First Quarter 2024 Investor Presentation
DBX First Quarter 2024 Investor Presentation
 
Platformless Horizons for Digital Adaptability
Platformless Horizons for Digital AdaptabilityPlatformless Horizons for Digital Adaptability
Platformless Horizons for Digital Adaptability
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
Apidays New York 2024 - Accelerating FinTech Innovation by Vasa Krishnan, Fin...
 

Mongo Cake Plugin for CakePHP 2.0

  • 1. A data manipulation paradigm shift Sunday, September 4, 11
  • 2. Why ? Slow as hell PHP 4 support Why are you even here at CakeFest? Sunday, September 4, 11
  • 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. 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. A faint memory of CS theory Objects should be cohesive and decoupled from each other Sunday, September 4, 11
  • 6. I wanted This! + Well, not quite... Sunday, September 4, 11
  • 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. 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. 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. 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. 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. 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. + 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. + 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. + 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. + 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. 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. 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. Accessing this property will automatically call the respective setUserName() and getUserName() methods! transparent AccessorS Sunday, September 4, 11
  • 20. Can also be used to access associated documents’ properties Array Access Sunday, September 4, 11
  • 22. Array Query Builders Sunday, September 4, 11
  • 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. BAKING MY PLAN + + Sunday, September 4, 11
  • 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. 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. 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. 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. 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. 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. 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
  • 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. 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. Time for a quick demo? thanks for attending! http://github.com/lorenzo/MongoCake Sunday, September 4, 11