Hi Readers,
Welcome to FindNerd, today we are going to Discussion on Database Access and ORM in CakePHP 3.
The new version of CakePHP comes with lots of new features and changes in it.
To access the database in CakePHP 3 there are two main objects.
1-Table objects.
2- Entities.
Let me explain one by one
1-Table objects : It provides the access to the collection of data.
2- Entities : It provides the access to the row record level data.
ORM is also an important feature for making relation in the database.
For the using ORM in cakephp3 for conventions the database then you have to the following the process.
Load data from table articles.
<?php
use Cake\ORM\TableRegistry;
$articles = TableRegistry::get('Articles');
$query = $articles->find();
foreach ($query as $row)
{
echo $row->title;
}
?>
If you want to apply method you can direct go to the model of that table and code there.
namespace App\Model\Table;
Now when you have concrete table classes you can create concrete entity class.
<?php
use Cake\ORM\Table;
class ArticlesTable extends Table {
}
?>
Now let define accessor and mutator method.
namespace App\Model\Entity;
Now you can load the entities
<?php
use Cake\ORM\Entity;
class Article extends Entity {
}
?>
Now an instance of ArticlesTable.
<?php
$articles = TableRegistry::get('Articles');
$query = $articles->find();
foreach ($query as $row) {
// Each row is now an instance of our Article class.
echo $row->title; }
?>
0 Comment(s)