ORM is a way that helps us to query and manipulate data in a database using an object-oriented paradigm.ORM refers to a library that implements the Object-Relational Mapping technique. An ORM library is a simple library written in any language of choice that encapsulates the code needed to manipulate the data so that we don’t write SQL anymore instead communicate directly with an object with the same language we’re using.
The CakePHP ORM provides a powerful and flexible way to work with relational databases. Using a data mapper pattern, ORM allows us to manipulate data as entities allowing us to create expressive domain layers in our applications.
Connecting to the Database
The first thing we need to do when using ORM is to register a connection object. Before performing any operations with the connection, we 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.
For example, if we wanted to load some data from our articles table we could do:
$articles = TableRegistry::getTableLocator()->get('Articles');
$query = $articles->find();
foreach ($query as $row) {
echo $row->title;
}
As you can see we didn’t have to create any code or do any configuration up. The conventions in CakePHP allow us to skip some procedure code and allow the framework to insert base classes when your application has not created a concrete class. If we wanted to customize our ArticlesTable class adding some associations or defining some additional methods we would add the following to src/Model/Table/ArticlesTable.php after the <?php opening tag.
Creating Associations
In your table classes, we can define the relations between our tables. CakePHP’s ORM supports 4 association types:
- 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.
Reading Data
Once we’ve defined some table classes we can read existing data in our tables:
use Cake\ORM\Locator\LocatorAwareTrait;
$articles = $this->getTableLocator()->get('Articles');
foreach ($articles->find() as $article) {
echo $article->title;
}
We can use the query builder to create complex queries, and a variety of methods to access our 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 code shows how we can easily save an entity and its associations in a simple & powerful way.
Deleting Data
Once we have a reference to an entity, we can use it to delete data:
$articles = $this->getTableLocator()->get('Articles');
$article = $articles->get(2);
$articles->delete($article);
The English language is the most widely used language as a medium of communication around the world. Having a certification for the English language can be an advantage. StudySection provides an English Certification Exam that tests English language proficiency in English grammar, reading, and writing.