【发布时间】: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设置为1或2在您的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