2017 © Pedro Peláez
 

library orm

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

image

cakephp/orm

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  • Tuesday, July 24, 2018
  • by cakephp
  • Repository
  • 32 Watchers
  • 126 Stars
  • 32,426 Installations
  • PHP
  • 49 Dependents
  • 2 Suggesters
  • 12 Forks
  • 0 Open issues
  • 100 Versions
  • 7 % Grown

The README.md

Total Downloads License, (*1)

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., (*2)

Database engines supported

The CakePHP ORM is compatible with:, (*3)

  • 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:, (*4)

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., (*5)

Using Table Locator

In order to access table instances you need to use a Table Locator., (*6)

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:, (*7)

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:, (*8)

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:, (*9)

  • 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., (*10)

Reading Data

Once you've defined some table classes you can read existing data in your tables:, (*11)

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., (*12)

Saving Data

Table objects provide ways to convert request data into entities, and then persist those entities to the database:, (*13)

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., (*14)

Deleting Data

Once you have a reference to an entity, you can use it to delete data:, (*15)

$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:, (*16)

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., (*17)

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., (*18)

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:, (*19)

<?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:, (*20)

<?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., (*21)

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:, (*22)

<?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:, (*23)

<?php
use Cake\Core\Configure;

Configure::write('App.namespace', 'My\Log\SubNamespace');

Additional Documentation

Consult the CakePHP ORM documentation for more in-depth documentation., (*24)

The Versions

24/07 2018

dev-master

9999999-dev https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

24/07 2018

3.6.9

3.6.9.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

21/07 2018

3.6.8

3.6.8.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

07/07 2018

3.6.7

3.6.7.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

23/06 2018

3.6.6

3.6.6.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

10/06 2018

3.6.5

3.6.5.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/05 2018

3.6.3

3.6.3.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/05 2018

3.6.4

3.6.4.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

25/04 2018

3.6.2

3.6.2.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

19/04 2018

3.6.1

3.6.1.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

14/04 2018

3.6.0

3.6.0.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

02/04 2018

3.5.15

3.5.15.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

02/04 2018

3.5.x-dev

3.5.9999999.9999999-dev https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

02/04 2018

3.5.17

3.5.17.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

24/03 2018

dev-3.next

dev-3.next https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

24/03 2018

3.6.0-RC1

3.6.0.0-RC1 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

24/03 2018

3.6.0-RC2

3.6.0.0-RC2 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

14/03 2018

3.6.0-beta3

3.6.0.0-beta3 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

10/03 2018

3.6.0-beta2

3.6.0.0-beta2 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

01/03 2018

3.6.0-beta1

3.6.0.0-beta1 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

28/02 2018

3.5.13

3.5.13.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

28/02 2018

3.5.14

3.5.14.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

27/01 2018

3.5.12

3.5.12.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/01 2018

3.5.11

3.5.11.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

22/12 2017

3.5.9

3.5.9.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

22/12 2017

3.5.10

3.5.10.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

05/12 2017

3.5.7

3.5.7.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

05/12 2017

3.5.8

3.5.8.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/10 2017

3.5.4

3.5.4.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/10 2017

3.5.5

3.5.5.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/10 2017

3.5.6

3.5.6.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

26/09 2017

3.5.3

3.5.3.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

29/08 2017

3.5.2

3.5.2.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

25/08 2017

3.5.1

3.5.1.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

17/08 2017

3.5.0

3.5.0.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

16/08 2017

3.4.13

3.4.13.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

16/08 2017

3.4.x-dev

3.4.9999999.9999999-dev https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

16/08 2017

3.4.14

3.4.14.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

12/08 2017

3.5.0-RC2

3.5.0.0-RC2 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

24/07 2017

3.5.0-RC1

3.5.0.0-RC1 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

18/07 2017

3.4.11

3.4.11.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

18/07 2017

3.4.12

3.4.12.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

06/07 2017

3.4.10

3.4.10.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

22/06 2017

3.4.9

3.4.9.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

11/06 2017

3.4.8

3.4.8.0 https://cakephp.org

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

orm cakephp data-mapper data-mapper pattern

15/05 2017
29/04 2017
23/03 2017
23/03 2017
08/03 2017
22/02 2017
18/02 2017
13/02 2017
10/02 2017
10/02 2017
04/02 2017

3.4.0-RC4

3.4.0.0-RC4

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

30/01 2017
29/01 2017
23/01 2017

3.4.0-RC3

3.4.0.0-RC3

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

18/01 2017

3.4.0-RC2

3.4.0.0-RC2

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

14/01 2017

3.4.0-RC1

3.4.0.0-RC1

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

11/01 2017
08/01 2017

3.4.0-beta4

3.4.0.0-beta4

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

01/01 2017

3.4.0-beta3

3.4.0.0-beta3

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

22/12 2016

3.4.0-beta1

3.4.0.0-beta1

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

22/12 2016

3.4.0-beta2

3.4.0.0-beta2

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

12/12 2016
03/12 2016
09/11 2016
05/11 2016
24/10 2016
07/10 2016
23/09 2016
23/09 2016
02/09 2016
20/08 2016
20/08 2016
13/08 2016
10/08 2016
04/08 2016

3.3.0-RC1

3.3.0.0-RC1

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

27/07 2016
27/07 2016

3.3.0-beta3

3.3.0.0-beta3

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

16/07 2016

3.3.0-beta2

3.3.0.0-beta2

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

07/07 2016

3.3.0-beta

3.3.0.0-beta

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

30/06 2016
20/06 2016
24/05 2016
11/05 2016
22/04 2016
09/04 2016
27/03 2016
11/03 2016
06/03 2016
17/02 2016
09/02 2016
30/01 2016
30/01 2016
29/01 2016

3.1.x-dev

3.1.9999999.9999999-dev

CakePHP ORM - Provides a flexible and powerful ORM implementing a data-mapper pattern.

  Sources   Download

MIT

The Requires

 

29/01 2016
29/01 2016