[READ-ONLY] A flexible, lightweight and powerful Object-Relational Mapper for PHP, implemented using the DataMapper pattern. This repo is a split of the main code that can be found in https://github.com/cakephp/cakephp
PHP
Latest commit 5685d64 Jan 11, 2017 @markstory markstory committed on GitHub Merge pull request #9960 from cleptric/counter-cache
CounterCache should ignore dirty fields
Permalink
Failed to load latest commit information.
Association Fix has many save replace doesn't respect association conditions. Dec 31, 2016
Behavior Merge pull request #9960 from cleptric/counter-cache Jan 11, 2017
Exception Remove superfluous whitespace in doc blocks Aug 10, 2016
Locator Fix up yellow warnings in IDE by proper doc block typehinting. Sep 7, 2016
Rule Remove unused use statements Dec 12, 2016
Association.php Fix up yellow warnings in IDE by proper doc block typehinting. Sep 7, 2016
AssociationCollection.php Fix up yellow warnings in IDE by proper doc block typehinting. Sep 7, 2016
AssociationsNormalizerTrait.php Cast values before iterating. Feb 18, 2015
Behavior.php Fix up yellow warnings in IDE by proper doc block typehinting. Sep 7, 2016
BehaviorRegistry.php Fix CS error. Jul 13, 2016
EagerLoadable.php Fix up yellow warnings in IDE by proper doc block typehinting. Sep 7, 2016
EagerLoader.php Do fewer allocations for simple default values. Aug 29, 2016
Entity.php Fix CS error. Jul 13, 2016
LICENSE.txt Adds license file for ORM subtree split Feb 9, 2016
LazyEagerLoader.php Fix CS error. Jul 13, 2016
Marshaller.php Remove unused use statements Dec 12, 2016
PropertyMarshalInterface.php Fix typo in method name and interface. Aug 22, 2016
Query.php Remove unused use statements Dec 12, 2016
README.md Fix option name in ORM package's README Sep 2, 2016
ResultSet.php Don't set associated record to null when autofields is disabled. Sep 29, 2016
RulesChecker.php Merge pull request #9090 from ionas/fixExistsIn-New Jul 23, 2016
SaveOptionsBuilder.php phpcs fixes Aug 8, 2016
Table.php Improve descriptiveness of exception message Nov 9, 2016
TableRegistry.php Remove superfluous whitespace in doc blocks Aug 10, 2016
composer.json update suggest with better explanation for i18n Oct 7, 2016

README.md

Total Downloads License

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::config('default', [
    'className' => 'Cake\Database\Connection',
    'driver' => 'Cake\Database\Driver\Mysql',
    'database' => 'test',
    'username' => 'root',
    'password' => 'secret',
    'cacheMetadata' => false // If set to `true` you need to install the optional "cakephp/cache" package.
]);

Once a 'default' connection is registered, it will be used by all the Table mappers if no explicit connection is defined.

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\TableRegistry;

$articles = TableRegistry::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\TableRegistry;

$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 = TableRegistry::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 = TableRegistry::get('Articles');
$article = $articles->get(2);
$articles->delete($article);

Additional Documentation

Consult the CakePHP ORM documentation for more in-depth documentation.