Database Access & ORM
These two classes are usually responsible for managing almost everythingthat happens regarding your data, its validity, interactions and evolutionof the information workflow in your domain of work.
CakePHP’s built-in ORM specializes in relational databases, but can be extendedto support alternative datasources.
The CakePHP ORM borrows ideas and concepts from both ActiveRecord and Datamapperpatterns. It aims to create a hybrid implementation that combines aspects ofboth patterns to create a fast, simple to use ORM.
Note
If you are familiar with previous versions of CakePHP, you should read theNew ORM Upgrade Guide for important differences between CakePHP 3.0and older versions of CakePHP.
To get started you don’t have to write any code. If you’ve followed the you can just start using the ORM. For example if we wanted to load some data from our table we could do:
Table classes use the CamelCased version of the table name with the suffix as the class name. Once your class has been created you get a referenceto it using the ORM\Locator\TableLocator
through as before:
Now that we have a concrete table class, we’ll probably want to use a concreteentity class. Entity classes let you define accessor and mutator methods, definecustom logic for individual records and much more. We’ll start off by adding thefollowing to src/Model/Entity/Article.php after the <?php
opening tag:
Entities use the singular CamelCase version of the table name as their classname by default. Now that we have created our entity class, when weload entities from the database we’ll get instances of our new Article class:
See the chapters on and Entities for moreinformation on how to use table objects and entities in your application.