Mark Story a8b93c75f8 Merge branch 'master' into 3.next 9 years ago
..
Association 7c23c08600 Fix up doblocks to be of more concrete type arrays 9 years ago
Behavior 53a76b0e03 Only dirty fields when the entity is new. 9 years ago
Exception 05305555a3 Remove superfluous whitespace in doc blocks 9 years ago
Locator 7c23c08600 Fix up doblocks to be of more concrete type arrays 9 years ago
Rule bcd7a34f08 Merge branch 'master' into 3.next 9 years ago
Association.php 7c23c08600 Fix up doblocks to be of more concrete type arrays 9 years ago
AssociationCollection.php 6cf9f114c7 Fix up yellow warnings in IDE by proper doc block typehinting. 9 years ago
AssociationsNormalizerTrait.php d26d198483 Cast values before iterating. 11 years ago
Behavior.php 6cf9f114c7 Fix up yellow warnings in IDE by proper doc block typehinting. 9 years ago
BehaviorRegistry.php 855c4d114b Fix CS error. 9 years ago
EagerLoadable.php 6cf9f114c7 Fix up yellow warnings in IDE by proper doc block typehinting. 9 years ago
EagerLoader.php 80c123b9db Do fewer allocations for simple default values. 9 years ago
Entity.php 855c4d114b Fix CS error. 9 years ago
LICENSE.txt 68488d9982 Adds license file for ORM subtree split 10 years ago
LazyEagerLoader.php 855c4d114b Fix CS error. 9 years ago
Marshaller.php 36052a633b Don't mark null values as dirty when they are still null. 9 years ago
PropertyMarshalInterface.php 5803eefc36 Fix typo in method name and interface. 9 years ago
Query.php 032c154b6f Merge branch 'master' into 3.next 9 years ago
README.md c214edea13 Fix option name in ORM package's README 9 years ago
ResultSet.php e084a14488 Don't set associated record to null when autofields is disabled. 9 years ago
RulesChecker.php e0d5c905ba Merge pull request #9090 from ionas/fixExistsIn-New 9 years ago
SaveOptionsBuilder.php 16b1316f6e phpcs fixes 9 years ago
Table.php a6a37601c9 Merge branch 'master' into 3.next 9 years ago
TableRegistry.php 05305555a3 Remove superfluous whitespace in doc blocks 9 years ago
composer.json cb9a32eb55 update suggest with better explanation for i18n 9 years ago

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.