CakePHP ORM in your app
We all know that CakePHP is a Full Stack framework, but you may not know that you can pick some of the components and use it in your app when you need. The ORM is not the only one available - you could also have filesystem, validation, utility, core, collection, database, cache, event, form, log, datasource, console and i18n. In this article, we are going to show how to use the ORM component in your slim framework app. You can find the component at: https://github.com/cakephp/?q=read&type=&language=
Why should we use the componentes
You may be asking why to use as component instead of using the framework as it is, and one of the reasons would be that you already have an application, perhaps using slim framework, and now you need to retrieve data from database. This would be a good moment to use a lib to help you, and here is where cakephp/orm comes in handy.
With the componentes you can now solve things with your knowledge in CakePHP, even when your application was not built with CakePHP.
Setting up cakephp/orm in a slim application
The code used in this article can be found at: https://github.com/CakeDC/slim-cakephp4-packages
We could use the package in any PHP application, but for this article we are going to use a slim application skeleton https://github.com/slimphp/Slim-Skeleton. In this app we’re going to change the routes /users
and /users/[id]
to fetch data from database instead of in-memory data.
Install the package with composer
We are going to install the version 4.0 of the ORM package:
composer require cakephp/orm:~4.0
Update config
The app uses the file app/settings.php to set up the main configs, so we need to add a config for database and set cakephp’s App.namespace
config.
First: add this line after the use
declaration. Don’t worry about this config now. We’re setting this to avoid error with PHP 7.4.
Configure::write('App.namespace', 'App');
Then add your database config key inside settings
array - in this case we’re using mysql database from a docker service:
'database' => [
'className' => \Cake\Database\Connection::class,
'driver' => \Cake\Database\Driver\Mysql::class,
'database' => 'my_db',
'username' => 'root',
'password' => 'secret',
'host' => 'mysql',
]
See https://github.com/CakeDC/slim-cakephp4-packages/blob/main/app/settings.php
Add TableLocator to app dependencies
The app uses the file app/dependencies.php to configure the container of dependencies, and this is a good place for us to define TableLocator as dependency since we normally would use it
In my places. Let’s add this inside $containerBuilder->addDefinitions
:
\Cake\ORM\Locator\TableLocator::class => function(ContainerInterface $c) {
$settings = $c->get('settings');
\Cake\Datasource\ConnectionManager::setConfig('default', $settings['database']);
return new \Cake\ORM\Locator\TableLocator();
}
See https://github.com/CakeDC/slim-cakephp4-packages/blob/main/app/dependencies.php
This service gets the settings we defined before,sets the configuration to CakePHP, and returns an instance of TableLocator.
We could use this service in any route action
with:
$locator = $this->get(\Cake\ORM\Locator\TableLocator::class);
$users = $locator->get('Users')->find()->all()->toArray();
But for this app it will make sense to use the TableLocator in a custom persistence class.
Using the ORM to fetch data
Now, we are finally going to replace the current way of retrieving data to be able to use CakePHP OR. Since our idea is to have a minimum impact in the app, the best way for this app is to replace the current UserRepository. Keep in mind that you could use the following steps with any other class you have.
Create a new persistence class DatabaseUserRepository:
Create the class at src/Infrastructure/Persistence/User/DatabaseUserRepository.php
Define a constructor method with tableLocator parameter
public function __construct(\Cake\ORM\Locator\TableLocator $tableLocator)
{
$this->tableLocator = $tableLocator;
}
Now we can use the tableLocator to fetch data:
$this->tableLocator->get('Users')->find()->all();
$this->tableLocator->get('Users')->get(10);
////
To make the app work correctly, we need to add the required method findAll and findUserOfId
Now that the repository persistence class was created, we need to connect
using dependency injection. Update the UserRepository::class
entry in repositories
with:
UserRepository::class => \DI\autowire(\App\Infrastructure\Persistence\User\DatabaseUserRepository::class),
https://github.com/CakeDC/slim-cakephp4-packages/blob/main/app/repositories.php
That’s it! Now we can access /users and see the users stored in our database.
I hope you’ve found this information to be helpful! Let us know!
For more information check out: https://github.com/cakephp/orm.