CakePHP TableRegistry

It is similar to the connection manager through which we can perform crud operations in our database table. There is a small difference between the connection manager and the Table registry.

$this->connection = ConnectionManager::get('default');// It the default database name
$this->data = TableRegistry::get("employees"); // Here is the table name 

In connection manager, we have defined the default database name during the configuration of the database. And in the case of TableRegistry, we have to define the table name with the database name as we defined in the above syntax of the connection manager. Also, you have to use the given below package in the controller.

 use Cake\ORM\TableRegistry;

Now we will perform the CRUD operation in CakePHP by use of TableRegistry.

Create Data

In this method, we will know about how we can insert the values in our table. So, firstly, we have to create a UsersController.php file at src/Controller/UsersController and the write given below program.

This is a UsersController.php

 <?php
  namespace App\Controller;
  use App\Controller\AppController;
 use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
  class UsersController extends AppController
  {
  public $connection;
  private $data;
  public function initialize()
  {
  parent::initialize();
  //$this->loadComponent->('Flash');
  $this->connection = ConnectionManager::get('default');
  $this->data = TableRegistry::get("employees"); // here we passed the table name and store the TableRegistry in the object data. 
  }
  public function tableData()
  {
   //$this->autoRender = false;
  $this->Flash->set('Details saved',
    [ 'element' => 'success']); // this will show flash message
  $tableAdd = $this->data->newEntity(); // By defining this, it creates a reserves space for inserting the data items.
  $tableAdd->name ="Sachin";
  $tableAdd->company = "TCS";
  $tableAdd->email = "[email protected]";
  $tableAdd->city = "Noida";
  $this->data->save($tableAdd); // It is the save method in which //we pass the object to save in the table.
  }
 }
 ?> 

You have to create an action file name table_Data.ctp file in  src/Template/Users/ table_Data.ctp file and write the given below statement.

  Your Record has been saved.

Now run your program in local server like: localhost/my_app_name/users/tableData. Then it will show you this output on your screen.

CakePHP TableRegistry

Similarly, you can also insert more data into the database table. You can check in your database where you will see these values.

Output:

CakePHP TableRegistry 1

Read or Select Data

In this method we will know, how we can read the data from the database table by using the given below syntax.

$datas = $this->data->find("all")->toArray("assoc");

Example:

Now create a UsersController.php file at src/Controller/UsersController and the write given below program.

This is a UsersController.php

 <?php
 namespace App\Controller;
 use App\Controller\AppController;
 use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
  class UsersController extends AppController
  {
  public $connection;
  private $data;
  public function initialize()
  {
  parent::initialize();
  //$this->loadComponent->('Flash');
  $this->connection = ConnectionManager::get('default');
  $this->data = TableRegistry::get("employees"); 
  }
  public function showData()
  { 
  $datas = $this->data->find("all")->toArray("assoc");
  $this->Flash->set('Your Table Records',
  [ 'element' => 'success']);
  $this->set('collect',$datas);
  }
 }
 ?> 

You have to create an action file name show_Data.ctp file in  src/Template/Users/ show_Data.ctp file and write the given below statement.

 <?php 
 foreach($collect as $data)
   { 
  echo $data['name'].",".$data['company'].",".$data['email'].",".$data['city'].","."<br/>";
  } 
  ?> 

Now run your program in local server like: localhost/my_app_name/users/showData. Then it will show you this output in your screen.

CakePHP TableRegistry 2

You can also see the records in the database table.

Output:

CakePHP TableRegistry 3

Update Data

In this method, we will know how we can modify the existing data in the table. For this operation, we have to define the following terms-

  1. Firstly, we have to pass the particular Id of the records.
  2. Then pass the values of the table fields in which we want to change. 

Example:

Now create a UsersController.php file at src/Controller/UsersController and the write given below program.

This is a UsersController.php

 <?php
 namespace App\Controller;
 use App\Controller\AppController;
 use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
  class UsersController extends AppController
  {
  public $connection;
  private $data;
  public function initialize()
  {
  parent::initialize();
  //$this->loadComponent->('Flash');
  $this->connection = ConnectionManager::get('default');
  $this->data = TableRegistry::get("employees"); 
  }
  public function changeData()
  { //$this->autorender = false;
  $add = $this->data->get(12); // here we have defined the id in //which we want to change
  //print_r($add);
  $add->name = "Amit"; // now pass the parameter’s value which //you have defined in the table
  $add->email = "[email protected]";
  //echo $this->data->save($add);
  if( $this->data->save($add))
  {
  $this->Flash->set('Your Records have been updated',
   [ 'element' => 'success']);
  } else{
  $this->Flash->set('Already Updated',
   [ 'element' => 'error']);
  }
  }
 }
 ?> 

You have to create an action file name change_Data.ctp file in src/Template/Users/ change_Data.ctp file and write the given below statement.

Great Job.

Now run your program in local server like: localhost/my_app_name/users/changeData. Then it will show you this output on your screen.

CakePHP TableRegistry 4

You can also see the updated data in the database table.

Output:

CakePHP TableRegistry 5

Delete Data

This method is used in TableRegistry for deleting the data from the table. In this method, we have to define the particular id of the records through the given syntax.

$add = $this->data->get(12); // pass the id in which we want change
$this->data->delete($add); // use the delete method and pass the add variable 

Example:

Now create a UsersController.php file at src/Controller/UsersController and the write given below program.

This is a UsersController.php

 <?php
 namespace App\Controller;
 use App\Controller\AppController;
 use Cake\Datasource\ConnectionManager;
 use Cake\ORM\TableRegistry;
  class UsersController extends AppController
  {
  public $connection;
  private $data;
  public function initialize()
  {
  parent::initialize();
  //$this->loadComponent->('Flash');
  $this->connection = ConnectionManager::get('default');
  $this->data = TableRegistry::get("employees"); 
  }
  public function deleteData()
  {
  $add = $this->data->get(12);
  $this->data->delete($add);
  $this->Flash->set('Your Records has been deleted',
   [ 'element' => 'success']);
  } 

You have to create an action file name delete_Data.ctp file in src/Template/Users/ delete_Data.ctp file and write the given below message.

 Welldone!!

Now run your program in local server like: localhost/my_app_name/users/deleteData. Then it will show you this output on your screen.

CakePHP TableRegistry 6

You can also see the deleted data in the database table.

Output:

CakePHP TableRegistry 7