English 中文(简体)
CakePHP - Delete a Record
  • 时间:2024-12-22

CakePHP - Delete a Record


Previous Page Next Page  

To delete a record in database, we first need to get hold of a table using the TableRegistry class. We can fetch the instance out of registry using the get() method. The get() method will take the name of the database table as an argument. Now, this new instance is used to get particular record that we want to delete.

Call the get() method with this new instance and pass the primary key to find a record which will be saved in another instance. Use the TableRegistry class’s instance to call the delete method to delete record from database.

Example

Make changes in the config/routes.php file as shown in the following code.

config/routes.php

<?php
use CakeHttpMiddlewareCsrfProtectionMiddleware;
use CakeRoutingRouteDashedRoute;
use CakeRoutingRouteBuilder;
$routes->setRouteClass(DashedRoute::class);
$routes->scope( / , function (RouteBuilder $builder) {
   $builder->registerMiddleware( csrf , new CsrfProtectionMiddleware([
       httpOnly  => true,
   ]));
   $builder->applyMiddleware( csrf );
   //$builder->connect( /pages ,[ controller => Pages , action => display ,  home ]);
   $builder->connect( /users/delete , [ controller  =>  Users ,  action  =>  delete ]);
   $builder->fallbacks();
});

Create a UsersController.php file at src/Controller/UsersController.php. Copy the following code in the controller file.

src/controller/UsersController.php

<?php
   namespace AppController;
   use AppControllerAppController;
   use CakeORMTableRegistry;
   use CakeDatasourceConnectionManager;
   class UsersController extends AppController{
      pubpc function index(){
         $users = TableRegistry::get( users );
         $query = $users->find();
         $this->set( results ,$query);
      }
      pubpc function delete($id){
         $users_table = TableRegistry::get( users );
         $users = $users_table->get($id);
         $users_table->delete($users);
         echo "User deleted successfully.";
         $this->setAction( index );
      }
   }
?>

Just create an empty View file under Users directory called delete.ctp.

src/Template/Users/delete.ctp

Create a directory Users at src/Template, ignore if already created, and under that directory create a Viewfile called index.ctp. Copy the following code in that file.

src/Template/Users/index.ctp

<a href="add">Add User</a>
<table>
   <tr>
   <td>ID</td>
   <td>Username</td>
   <td>Password</td>
   <td>Edit</td>
   <td>Delete</td>
   </tr>
   <?php
      foreach ($results as $row):
      echo "<tr><td>".$row->id."</td>";
      echo "<td>".$row->username."</td>";
      echo "<td>".$row->password."</td>";
      echo "<td><a href= ".$this->Url->build(["controller" => "Users","action" => "edit",$row->id])." >Edit</a></td>";
      echo "<td><a href= ".$this->Url->build(["controller" => "Users","action" => "delete",$row->id])." >Delete</a></td></tr>";
      endforeach;
   ?>
</table>

Execute the above example by visiting the following URL and cpck on Delete pnk to delete record.

http://localhost:85/CakePHP/users

Output

After visiting the above URL and cpcking on the Delete pnk, you will receive the following output where you can delete record.

Delete Link

Cpck on Delete pnk to delete the record.

User Deleted Advertisements