cakephp / orm
CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.
Requires
- php: >=8.1
- cakephp/collection: ^5.1
- cakephp/core: ^5.1
- cakephp/database: ^5.1
- cakephp/datasource: ^5.1
- cakephp/event: ^5.1
- cakephp/utility: ^5.1
- cakephp/validation: ^5.1
Requires (Dev)
- cakephp/cache: ^5.1
- cakephp/i18n: ^5.1
Suggests
- cakephp/cache: If you decide to use Query caching.
- cakephp/i18n: If you are using Translate/TimestampBehavior or Chronos types.
- dev-master
- 5.x-dev
- 5.1.4
- 5.1.2
- 5.1.1
- 5.1.0
- 5.1.0-RC2
- 5.1.0-RC1
- 5.0.11
- 5.0.10
- 5.0.9
- 5.0.8
- 5.0.7
- 5.0.6
- 5.0.5
- 5.0.4
- 5.0.3
- 5.0.2
- 5.0.0
- 5.0.0-RC2
- 5.0.0-RC1
- 5.0.0-beta2
- 5.0.0-beta1
- 4.x-dev
- 4.5.8
- 4.5.7
- 4.5.6
- 4.5.5
- 4.5.4
- 4.5.3
- 4.5.2
- 4.5.1
- 4.5.0
- 4.5.0-RC1
- 4.4.17
- 4.4.16
- 4.4.15
- 4.4.14
- 4.4.13
- 4.4.12
- 4.4.11
- 4.4.10
- 4.4.9
- 4.4.8
- 4.4.7
- 4.4.6
- 4.4.5
- 4.4.4
- 4.4.3
- 4.4.2
- 4.4.1
- 4.4.0
- 4.4.0-RC2
- 4.4.0-RC1
- 4.3.x-dev
- 4.3.11
- 4.3.10
- 4.3.9
- 4.3.8
- 4.3.7
- 4.3.6
- 4.3.5
- 4.3.4
- 4.3.3
- 4.3.2
- 4.3.1
- 4.3.0
- 4.3.0-RC4
- 4.3.0-RC3
- 4.3.0-RC2
- 4.3.0-RC1
- 4.2.x-dev
- 4.2.12
- 4.2.11
- 4.2.10
- 4.2.9
- 4.2.8
- 4.2.7
- 4.2.6
- 4.2.5
- 4.2.4
- 4.2.3
- 4.2.2
- 4.2.1
- 4.2.0
- 4.2.0-RC1
- 4.2.0-beta1
-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 3.5.4
- 3.5.3
- 3.5.2
- 3.5.1
- 3.5.0
- 3.5.0-RC2
- 3.5.0-RC1
- 3.4.x-dev
- 3.4.14
- 3.4.13
- 3.4.12
- 3.4.11
- 3.4.10
- 3.4.9
- 3.4.8
- 3.4.7
- 3.4.6
- 3.4.5
- 3.4.4
- 3.4.3
- 3.4.2
- 3.4.1
- 3.4.0
- 3.4.0-RC4
- 3.4.0-RC3
- 3.4.0-RC2
- 3.4.0-RC1
- 3.4.0-beta4
- 3.4.0-beta3
- 3.4.0-beta2
- 3.4.0-beta1
- 3.3.16
- 3.3.15
- 3.3.14
- 3.3.13
- 3.3.12
- 3.3.11
- 3.3.10
- 3.3.9
- 3.3.8
- 3.3.7
- 3.3.6
- 3.3.5
- 3.3.4
- 3.3.3
- 3.3.2
- 3.3.1
- 3.3.0
- 3.3.0-RC1
- 3.3.0-beta3
- 3.3.0-beta2
- 3.3.0-beta
- 3.2.x-dev
- 3.2.14
- 3.2.13
- 3.2.12
- 3.2.11
- 3.2.10
- 3.2.9
- 3.2.8
- 3.2.7
- 3.2.6
- 3.2.5
- 3.2.4
- 3.2.3
- 3.2.2
- 3.2.1
- 3.2.0
- 3.2.0-RC1
- 3.1.x-dev
- 3.1.13
- 3.1.10
- 3.1.9
- 3.1.8
- 3.1.7
- 3.1.6
- 3.1.5
- 3.1.4
- 3.1.3
- 3.1.2
- 3.1.1
- 3.1.0
- 3.1.0-RC1
- 3.1.0-beta2
- 3.1.0-beta
- 3.0.x-dev
- 3.0.18
- 3.0.15
- 3.0.13
- 3.0.12
- 3.0.11
- 3.0.10
- 3.0.9
- 3.0.8
- 3.0.7
- 3.0.6
- 3.0.5
- 3.0.4
- 3.0.3
- 3.0.2
- 3.0.1
- 3.0.0
- dev-5.next
- dev-4.next
- dev-3.next
- dev-3-1
- dev-3-0
This package is auto-updated.
Last update: 2024-12-13 04:03:23 UTC
README
CakePHP ORM
The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a datamapper pattern the ORM allows you to manipulate data as entities allowing you to create expressive domain layers in your applications.
Database engines supported
The CakePHP ORM is compatible with:
- MySQL 5.1+
- Postgres 8+
- SQLite3
- SQLServer 2008+
- Oracle (through a community plugin)
Connecting to the Database
The first thing you need to do when using this library is register a connection object. Before performing any operations with the connection, you need to specify a driver to use:
use Cake\Datasource\ConnectionManager; ConnectionManager::setConfig('default', [ 'className' => \Cake\Database\Connection::class, 'driver' => \Cake\Database\Driver\Mysql::class, 'database' => 'test', 'username' => 'root', 'password' => 'secret', 'cacheMetadata' => true, 'quoteIdentifiers' => false, ]);
Once a 'default' connection is registered, it will be used by all the Table mappers if no explicit connection is defined.
Using Table Locator
In order to access table instances you need to use a Table Locator.
use Cake\ORM\Locator\TableLocator; $locator = new TableLocator(); $articles = $locator->get('Articles');
You can also use a trait for easy access to the locator instance:
use Cake\ORM\Locator\LocatorAwareTrait; $articles = $this->getTableLocator()->get('Articles');
By default, classes using LocatorAwareTrait
will share a global locator instance.
You can inject your own locator instance into the object:
use Cake\ORM\Locator\TableLocator; use Cake\ORM\Locator\LocatorAwareTrait; $locator = new TableLocator(); $this->setTableLocator($locator); $articles = $this->getTableLocator()->get('Articles');
Creating Associations
In your table classes you can define the relations between your tables. CakePHP's ORM supports 4 association types out of the box:
- belongsTo - E.g. Many articles belong to a user.
- hasOne - E.g. A user has one profile.
- hasMany - E.g. A user has many articles.
- belongsToMany - E.g. An article belongsToMany tags.
You define associations in your table's initialize()
method. See the
documentation for
complete examples.
Reading Data
Once you've defined some table classes you can read existing data in your tables:
use Cake\ORM\Locator\LocatorAwareTrait; $articles = $this->getTableLocator()->get('Articles'); foreach ($articles->find() as $article) { echo $article->title; }
You can use the query builder to create complex queries, and a variety of methods to access your data.
Saving Data
Table objects provide ways to convert request data into entities, and then persist those entities to the database:
use Cake\ORM\Locator\LocatorAwareTrait; $data = [ 'title' => 'My first article', 'body' => 'It is a great article', 'user_id' => 1, 'tags' => [ '_ids' => [1, 2, 3] ], 'comments' => [ ['comment' => 'Good job'], ['comment' => 'Awesome work'], ] ]; $articles = $this->getTableLocator()->get('Articles'); $article = $articles->newEntity($data, [ 'associated' => ['Tags', 'Comments'] ]); $articles->save($article, [ 'associated' => ['Tags', 'Comments'] ])
The above shows how you can easily marshal and save an entity and its associations in a simple & powerful way. Consult the ORM documentation for more in-depth examples.
Deleting Data
Once you have a reference to an entity, you can use it to delete data:
$articles = $this->getTableLocator()->get('Articles'); $article = $articles->get(2); $articles->delete($article);
Meta Data Cache
It is recommended to enable metadata cache for production systems to avoid performance issues. For e.g. file system strategy your bootstrap file could look like this:
use Cake\Cache\Engine\FileEngine; $cacheConfig = [ 'className' => FileEngine::class, 'duration' => '+1 year', 'serialize' => true, 'prefix' => 'orm_', ]; Cache::setConfig('_cake_model_', $cacheConfig);
Cache configs are optional, so you must require cachephp/cache
to add one.
Creating Custom Table and Entity Classes
By default, the Cake ORM uses the \Cake\ORM\Table
and \Cake\ORM\Entity
classes to
interact with the database. While using the default classes makes sense for
quick scripts and small applications, you will often want to use your own
classes for adding your custom logic.
When using the ORM as a standalone package, you are free to choose where to
store these classes. For example, you could use the Data
folder for this:
<?php // in src/Data/Table/ArticlesTable.php namespace Acme\Data\Table; use Acme\Data\Entity\Article; use Acme\Data\Table\UsersTable; use Cake\ORM\Table; class ArticlesTable extends Table { public function initialize() { $this->setEntityClass(Article::class); $this->belongsTo('Users', ['className' => UsersTable::class]); } }
This table class is now setup to connect to the articles
table in your
database and return instances of Article
when fetching results. In order to
get an instance of this class, as shown before, you can use the TableLocator
:
<?php use Acme\Data\Table\ArticlesTable; use Cake\ORM\Locator\TableLocator; $locator = new TableLocator(); $articles = $locator->get('Articles', ['className' => ArticlesTable::class]);
Using Conventions-Based Loading
It may get quite tedious having to specify each time the class name to load. So the Cake ORM can do most of the work for you if you give it some configuration.
The convention is to have all ORM related classes inside the src/Model
folder,
that is the Model
sub-namespace for your app. So you will usually have the
src/Model/Table
and src/Model/Entity
folders in your project. But first, we
need to inform Cake of the namespace your application lives in:
<?php use Cake\Core\Configure; Configure::write('App.namespace', 'Acme');
You can also set a longer namaspace up to the place where the Model
folder is:
<?php use Cake\Core\Configure; Configure::write('App.namespace', 'My\Log\SubNamespace');
Additional Documentation
Consult the CakePHP ORM documentation for more in-depth documentation.