Cakephp 如何在另一个表中添加用户id以获取有关该用户的其他更新信息
Controller/userscocontroller.phpCakephp 如何在另一个表中添加用户id以获取有关该用户的其他更新信息,cakephp,Cakephp,Controller/userscocontroller.php <?php class UsersController extends AppController { public function beforeFilter() { parent::beforeFilter(); /* allow add action so user can register */ $this->Auth->allow('add'
<?php
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
/* allow add action so user can register */
$this->Auth->allow('add');
}
public function login() {
if ($this->request->is('post')) {
/* login and redirect to url set in app controller */
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
/* logout and redirect to url set in app controller */
return $this->redirect($this->Auth->logout());
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
//
// If the user was saved, Now we add this information to the data
// and save the Profile.
$this->Session->setFlash(__('The user has been saved'));
return $this->redirect(array('controller' => 'pages','action' => 'home'));
}
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
//
?>
在这里,我又添加了一个表配置文件(当用户在函数add()中创建时在表中插入Userid)
为此,我做了更多更改,因此最终找到了解决方案,因此请指导我在用户中保存数据时可能需要加载配置文件模型
if ($this->User->save($this->request->data)) {
$this->loadModel('Profile'); // it may be different as per your table
$this->Profile->saveField('field_name', 'value');
希望它能帮助您添加Molude名称:DrProfile,并将代码更改为
$this->loadModel('DrProfile');$this->User->Drprofile->saveField('User_id','$this->User->id')代码>仍显示此行中的错误尝试类似于$this->Drprofile->saveField('user\u id',$this->user->id);您现在正在工作,但现在$this->Drprofile->saveField('user\u id',$this->user->id)代码>每次取零;如果你得到的是零,这意味着你在$this->User->Id中没有任何值。我觉得你应该尝试将最后一个insert Id获取为$this->User->getInsertID();您是否在以下模型之间建立了关系:用户只有一个配置文件
?感谢您的回复…否@Skywalker您看过这篇文章了吗?