添加数据以存储在 cakephp3 控制器中
Add data to store in cakephp3 controller
我正在使用 cakephp-3.0 但我的 cakephp 版本有问题 2.x 我可以在控制器中添加数据,我不需要从视图发送它。
示例:
public function add()
{
$this->request->data['User']['id']=$this->Auth->user('id');//and save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and save date
if ($this->request->is('post')) {
if ($this->Users->save($user)) {
$this->Flash->success('The user has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The user could not be saved. Please, try again.');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
在上面,向已经收到的数据添加数据,收到一条信息,该数据添加了 id 和日期,例如。只是一个例子。如果我保存此信息,则该信息会使用 ID 和日期保存。 id 和 date 不一定在视图中。
现在我想在 cakephp 3 中做一些类似的事情但是不工作。
public function add()
{
$user = $this->Users->newEntity();
$this->request->data['User']['id']=$this->Auth->user('id');//and don't save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and don't save date
if ($this->request->is('post')) {
$this->request['date'] = date("d-m-Y H:i:s");
$user = $this->Users->patchEntity($user, $this->request->data);
debug($user);
if ($this->Users->save($user)) {
$this->Flash->success('The user has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The user could not be saved. Please, try again.');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
搜索文档并找到一些我解释得更清楚的内容
因为这不再有效了:
$this->request->data['User']['id']
现在
$this->request->data['id']
在瞎搞之前你应该read the migration guide并且也许可以阅读 CakePHP 3.0 的博客教程来了解差异,这会节省你很多时间。
旧:
$this->request->data['User']['id']=$this->Auth->user('id');//and don't save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and don't save date
新:
$user = $this->Users->patchEntity($user, $this->request->data);
$user->id=$this->Auth->user('id');
$user->date=$date('Y-m-d H:m:s');
我正在使用 cakephp-3.0 但我的 cakephp 版本有问题 2.x 我可以在控制器中添加数据,我不需要从视图发送它。
示例:
public function add()
{
$this->request->data['User']['id']=$this->Auth->user('id');//and save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and save date
if ($this->request->is('post')) {
if ($this->Users->save($user)) {
$this->Flash->success('The user has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The user could not be saved. Please, try again.');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
在上面,向已经收到的数据添加数据,收到一条信息,该数据添加了 id 和日期,例如。只是一个例子。如果我保存此信息,则该信息会使用 ID 和日期保存。 id 和 date 不一定在视图中。
现在我想在 cakephp 3 中做一些类似的事情但是不工作。
public function add()
{
$user = $this->Users->newEntity();
$this->request->data['User']['id']=$this->Auth->user('id');//and don't save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and don't save date
if ($this->request->is('post')) {
$this->request['date'] = date("d-m-Y H:i:s");
$user = $this->Users->patchEntity($user, $this->request->data);
debug($user);
if ($this->Users->save($user)) {
$this->Flash->success('The user has been saved.');
return $this->redirect(['action' => 'index']);
} else {
$this->Flash->error('The user could not be saved. Please, try again.');
}
}
$this->set(compact('user'));
$this->set('_serialize', ['user']);
}
搜索文档并找到一些我解释得更清楚的内容
因为这不再有效了:
$this->request->data['User']['id']
现在
$this->request->data['id']
在瞎搞之前你应该read the migration guide并且也许可以阅读 CakePHP 3.0 的博客教程来了解差异,这会节省你很多时间。
旧:
$this->request->data['User']['id']=$this->Auth->user('id');//and don't save id
$this->request->data['User']['date']=$date('Y-m-d H:m:s');// and don't save date
新:
$user = $this->Users->patchEntity($user, $this->request->data);
$user->id=$this->Auth->user('id');
$user->date=$date('Y-m-d H:m:s');