Home >Backend Development >PHP Tutorial >CakePHP Delete
Basically, CakePHP is a framework used to perform the delete is used to delete the records from the database identified by the $id. Normally the delete command is dependent on the record which means we can say that the relationship of the user is one-to-many or we can have belongs. We know that PHP is a scripting server-side language to make dynamic interactions between the different web pages. In another word, we can say that we can delete records from the MySQL database with the help of the CakePHP framework as per our requirement as well as it is easy to implement.
Start Your Free Software Development Course
Web development, programming languages, Software testing & others
To delete a record in the data set, we first need to get hold of a table utilizing the Table Registry class. We can bring the occasion out of the library utilizing the get() technique. The get() technique will take the name of the data set table as a contention. Presently, this new example is utilized to get a specific record that we need to delete.
Call the get() strategy with this new occurrence and pass the essential key to observe a record that will be saved in another case. Utilize the Table Registry class’ example to call the delete technique to delete records from the information base.
The delete rules will be applied. Assuming the standards fall flat, erasure will be forestalled.
The Model.before delete occasion is set off. Assuming that this occasion is halted, the delete will be cut short and the occasion’s outcome will be returned.
The element will be deleted.
All reliant affiliations will be deleted. On the off chance that affiliations are being deleted as substances, extra occasions will be dispatched.
Any intersection table records for Belongs to Many affiliations will be eliminated.
The Model. after delete occasion will be set off.
Now let’s see how we can perform the delete in the CakePHP framework as follows.
To delete a record in the information base, we first need to keep a work area utilizing the TableRegistry superbness. we can get the occasion out of the library by utilizing the get() method. The get() approach will accept the call of the information base work area as an issue. Presently, this new occasion is utilized to get an interesting document that we need to delete.
Call the get() procedure with this new model and skirt the main key to view a report so as saved in each and every other example. Utilize the TableRegistry tastefulness guide to call the delete way to deal with delete records from a data set.
While erasing elements, related information can likewise be erased. In the event that your HasOne and has many affiliations are designed as reliant, erase tasks will ‘course’ to those substances also. Of course elements in related tables are eliminated utilizing CakeORMTable::deleteAll(). You can choose to have the ORM load-related elements and erase them independently by setting the cascadeCallbacks choice to valid. An example HasMany relationship with both these choices empowered would be:
Now let’s see the syntax as follows.
delete(integer $specified id of table= null, required boolean value$cascade = true);
Explanation
By using the above syntax we can implement delete in CakePHP, here we use the delete command with different parameters as follows.
Specified Id of the table is a unique identifier of that table and it is an integer, initially, it is null as per our requirement we can change the value of Id.
In this syntax, we also use Boolean value to set the cascade implementation of delete operation as shown in the above syntax.
CakePHP delete bulk
Now let’s see how we can perform bulk delete in CakePHP as follows.
There might be times when erasing lines individually isn’t effective or helpful. In these cases, it is more efficient to utilize a mass erase to eliminate many lines without a moment’s delay. A mass erase will be thought of as effective in the event that at least 1 line is erased. The capacity returns the number of erased records as a whole number.
Now let’s see the syntax of bulk delete as follows.
function deletespam() { return $this->deleteAll(['Specified statement that is spam' => true]); }
Explanation
In the above syntax, we declared a function and inside the function, we called deleteAll method as shown. In this syntax, we need to set the Boolean value of the specified statement that we want and it depends on the user requirement.
Now let’s see the different examples of delete operation for better understanding as follows.
First, we need to create a new table and put some records into the table as follows.
CREATE TABLE IF NOT EXISTS `sampledemo` ( `id` char(30) NOT NULL, `EmpName` varchar(250) DEFAULT NULL, `EmpPass` varchar(40) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=MyISAM DEFAULT CHARSET=latin1;
Now insert records in the newly created table as follows.
INSERT INTO `sampledemo` (`id`, `EmpName`, `EmpPass`) VALUES ('3', 'Siya','$2y$10$HKLH3YiZE'), ('4', 'Rohan','$2y$10$bZcoCTW'), ('5', 'Tanya','$2y$10$SnGQV8O');
Explanation
After Execution of the above query, we will get the following result as shown in the following screenshot as follows.
Now we need to make the changes in route.php as shown below.
<?php use Cake\Http\Middleware\CsrfProtectionMiddleware; use Cake\Routing\Route\DashedRoute; use Cake\Routing\RouteBuilder; $routes->setRouteClass(DashedRoute::class); $routes->scope('/', function (RouteBuilder $builder) { $builder->registerMiddleware('csrf', new CsrfProtectionMiddleware([ 'httpOnly' => true, ])); $builder->applyMiddleware('csrf'); $builder->connect('/users/delete', ['controller' => 'sam, 'action' => 'delete']); $builder->fallbacks(); }); Now we need to create a usercontroller.php file and write the following code as follows. ?php namespace App\Controller; use App\Controller\AppController; use Cake\ORM\TableRegistry; use Cake\Datasource\ConnectionManager; class UsersController extends AppController{ public function sequence (){ $users = TableRegistry::get('users'); $query = $users->find(); $this->set('output',$query); } public function delete($id){ $users_table = TableRegistry::get('users'); $users = $users_table->get($id); $users_table->delete($users); echo "deleted successfully."; $this->setAction('sequence'); } } ?>
Now we need to create a directory for the user and that file we call a ctp file either sequence or index as per our requirement we can change the name of the file and write the following code as follows.
<a href="add"> User</a> <table> <tr> <td>Id</td> <td>EmpNamee</td> <td>EmpPass</td> <td>Edit</td> <td>Delete</td> </tr> <?php foreach ($Output as $row): echo "<tr><td>".$row->id."</td>"; echo "<td>".$row->Empname."</td>"; echo "<td>".$rows->EmpPass."</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>
Now run the script in localhost and see the output, here is the end result of the above implementation we illustrated by using a screenshot as follows.
Now suppose we need to delete the 3 number records, so we need to provide the id of that row and the after delete operation result as shown in the following screenshot.
Similarly, we can delete the 4th number row and we can see the result in the following screenshot as follows.
We hope from this article you learn more about the CakePHP delete. From the above article, we have taken in the essential idea of the CakePHP delete and we also see the representation and example of the CakePHP delete. From this article, we learned how and when we use the CakePHP delete.
The above is the detailed content of CakePHP Delete. For more information, please follow other related articles on the PHP Chinese website!