CakePHP Update a Record

PHPz
Release: 2024-09-10 17:25:51
Original
816 people have browsed it

To update a record in database, we first need to get hold of a table using 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 update.

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 this instance, to set new values that you want to update and then, finally call the save() method with the TableRegistry class’s instance to update record.

Example

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

config/routes.php

<?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('/pages',['controller'=>'Pages','action'=>'display', 'home']);
   $builder->connect('/users/edit', ['controller' => 'Users', 'action' => 'edit']);
   $builder->fallbacks();
});
Copy after login

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

src/controller/UsersController.php

<?php namespace App\Controller;
   use App\Controller\AppController;
   use Cake\ORM\TableRegistry;
   use Cake\Datasource\ConnectionManager;
   class UsersController extends AppController{
      public function index(){
         $users = TableRegistry::get('users');
         $query = $users->find();
         $this->set('results',$query);
      }
      public function edit($id){
         if($this->request->is('post')){
            $username = $this->request->getData('username');
            $password = $this->request->getData('password');
            $users_table = TableRegistry::get('users');
            $users = $users_table->get($id);
            $users->username = $username;
            $users->password = $password;
            if($users_table->save($users))
            echo "User is udpated";
            $this->setAction('index');
         } else {
            $users_table = TableRegistry::get('users')->find();
            $users = $users_table->where(['id'=>$id])->first();
            $this->set('username',$users->username);
            $this->set('password',$users->password);
            $this->set('id',$id);
         }
      }
   }
?>
Copy after login

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

src/Template/Users/index.php

<a href="add">Add User</a>
Copy after login
"; echo ""; echo ""; echo ""; echo ""; endforeach; ?>
ID Username Password Edit Delete
".$row->id."".$row->username."".$row->password."EditDelete

Create another View file under the Users directory called edit.php and copy the following code in it.

src/Template/Users/edit.php

<?php echo $this->Form->create(NULL,array('url'=>'/users/edit/'.$id));
   echo $this->Form->control('username',['value'=>$username]);
   echo $this->Form->control('password',['value'=>$password]);
   echo $this->Form->button('Submit');
   echo $this->Form->end();
?>
Copy after login

Execute the above example by visiting the following URL and click on Edit link to edit record.

http://localhost/cakephp4/users

Output

After visiting the above URL, it will display the records in users table as shown below −

After Visiting

Click on Edit button and it will display you following screen −

Edit Button

Now, we will update the name CakePHP Update a Record to CakePHP Update a Record123 and submit the details. The next screen displayed will be as follows −

CakePHP Update a Record

The above is the detailed content of CakePHP Update a Record. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!