CakePHP - 更新记录



要更新数据库中的记录,我们首先需要使用TableRegistry类获取表。我们可以使用get()方法从注册表中获取实例。get()方法将接收数据库表的名称作为参数。现在,这个新实例用于获取我们要更新的特定记录。

使用此新实例调用get()方法,并传递主键以查找要保存到另一个实例中的记录。使用此实例,设置要更新的新值,然后最终调用TableRegistry类的实例的save()方法来更新记录。

示例

按照以下代码所示修改config/routes.php文件。

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();
});

src/Controller/UsersController.php处创建一个UsersController.php文件。将以下代码复制到控制器文件中。

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);
         }
      }
   }
?>

在src/Template处创建一个名为Users的目录,如果已创建则忽略,并在该目录下创建一个名为index.php的视图。将以下代码复制到该文件中。

src/Template/Users/index.php

<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>

在Users目录下创建另一个名为edit.php视图文件,并将以下代码复制到其中。

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();
?>

通过访问以下URL并点击编辑链接来编辑记录,执行以上示例。

https://127.0.0.1/cakephp4/users

输出

访问上述URL后,它将显示users表中的记录,如下所示:

After Visiting

点击编辑按钮,它将显示以下屏幕:

Edit Button

现在,我们将把名称Virat更新为Virat123并提交详细信息。接下来显示的屏幕如下所示:

Virat
广告