【问题标题】:CakePHP 3 new fields won't save correctlyCakePHP 3 新字段无法正确保存
【发布时间】:2015-10-01 19:54:35
【问题描述】:

我有一个带有电子邮件和密码的用户模型。我将字段 first_name 和 last_name 添加到我的数据库以及添加视图的表单,如下所示:

     9  <div class="users form large-12 medium-9 columns">
     10     <?= $this->Form->create($user) ?>
     11     <fieldset>
     12         <legend><?= __('New Account') ?></legend>
     13         <?php
     14             echo $this->Form->input('email');
     15             echo $this->Form->input('first_name');
     16             echo $this->Form->input('last_name');
     17             echo $this->Form->input('password');
     18
     19         ?>
     20     </fieldset>
     21     <?= $this->Form->button(__('Submit')) ?>
     22     <?= $this->Form->end() ?>
     23 </div>                            

电子邮件和密码保存没有问题,但 first_name 和 last_name 永远不会。这是控制器功能。添加注释行会导致 first_name 字段被保存,但很明显我不应该这样做。

     46     public function add()
 47     {
 48         $user = $this->Users->newEntity();
 49         if ($this->request->is('post')) {
 50             $user = $this->Users->patchEntity($user, $this->request->data);
 51             //$user->first_name = $this->request->data['first_name'];
 52             if ($this->Users->save($user)) {
 53                 $this->Flash->success(__('The user has been saved.'));
 54                 return $this->redirect(['action' => 'index']);
 55             } else {
 56                 $this->Flash->error(__('The user could not be saved. Please, try again.'));
 57             }
 58         }
 59         $books = $this->Users->Books->find('list', ['limit' => 200]);
 60         $this->set(compact('user', 'books'));
 61         $this->set('_serialize', ['user']);
 62     }

有人知道为什么会这样吗?我尝试清除模型缓存,但没有任何改变。

谢谢!

【问题讨论】:

  • debug 设置为12 在您的core.php 文件中使用Configure::write('debug', 2);
  • @Holt 什么 core.php 文件?我有一个 /config/app.php 文件,但据我所知 debug 只能设置为 true 或 false,并且设置为 true。
  • 你更新实体了吗?
  • @xPfqHZ 对不起,我看错了你的标题,虽然你使用的是CakePHP 2.0
  • 问题是将批量分配的新属性列入白名单book.cakephp.org/3.0/en/orm/entities.html#mass-assignment

标签: php cakephp cakephp-3.0


【解决方案1】:

使用大量分配新属性

$this->Model->patchEntity($entity, $this->request->data);

您必须将它们列入白名单。在这种情况下,在 /src/Model/Entity/User.php 文件中:

 1     protected $_accessible = [
 2         'email' => true,
 3         'password' => true,
 4         'first_name' => true, //add this
 5         'last_name' => true,  //add this
 6     ];

另一方面,直接分配属性(如$user-&gt;first_name = $this-&gt;request-&gt;data['first_name'];)总是可行的。

更多信息:http://book.cakephp.org/3.0/en/orm/entities.html#mass-assignment

【讨论】:

    【解决方案2】:

    另一种方法是在创建实体时设置可访问字段。

    $user = $this->Users->newEntity($this->request->data, [
        'accessibleFields' => [
              'email' => true,
              'password' => true,
              'first_name' => true,
              'last_name' => true, //or you could just use '*' => true
        ] 
    ]);
    

    也不需要先调用 newEntity 再调用 patchEntity 数据,你可以像我的例子一样首先将数据提供给 newEntity

    【讨论】:

    • 是的,但是 Cake 会自动烘焙 new/patch 方法(这就是我的代码的来源)。
    猜你喜欢
    • 2011-07-20
    • 1970-01-01
    • 2017-07-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-01-29
    • 2012-01-04
    • 1970-01-01
    相关资源
    最近更新 更多