cakephp/orm

Name: orm

Owner: CakePHP

Description: [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

Created: 2015-02-12 16:57:52.0

Updated: 2018-05-13 23:30:18.0

Pushed: 2018-05-21 01:40:57.0

Homepage: null

Size: 2198

Language: PHP

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

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:

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:

Cake\Datasource\ConnectionManager;

ectionManager::setConfig('default', [
'className' => 'Cake\Database\Connection',
'driver' => 'Cake\Database\Driver\Mysql',
'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.

Cake\ORM\Locator\TableLocator;

ator = new TableLocator();
icles = $locator->get('Articles');

You can also use a trait for easy access to the locator instance:

Cake\ORM\Locator\LocatorAwareTrait;

icles = $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:

Cake\ORM\Locator\TableLocator;
Cake\ORM\Locator\LocatorAwareTrait;

ator = new TableLocator();
s->setTableLocator($locator);

icles = $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:

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:

Cake\ORM\Locator\LocatorAwareTrait;

icles = $this->getTableLocator()->get('Articles');
ach ($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:

Cake\ORM\Locator\LocatorAwareTrait;

a = [
'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'],
]


icles = $this->getTableLocator()->get('Articles');
icle = $articles->newEntity($data, [
'associated' => ['Tags', 'Comments']

icles->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:

icles = $this->getTableLocator()->get('Articles');
icle = $articles->get(2);
icles->delete($article);
Meta Data Cache

It is recommended to enable meta data cache for production systems to avoid performance issues. For e.g. file system strategy your bootstrap file could look like this:

Cake\Cache\Engine\FileEngine;

heConfig = [
className' => FileEngine::class,
duration' => '+1 year',
serialize' => true,
prefix'    => 'orm_',

e::setConfig('_cake_model_', $cacheConfig);
Additional Documentation

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


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.