我有以下表格用户(id,name,email),文章(id,user_id,title,body)。我似乎无法使用登录的user_id填充articles表中的user_id。这是我的代码 //文章控制器
public function add() {
if ($this->request->is('post')) {
$this->Article->create();
$this->request->data['Article']['user_id'] = $user_id;
if ($this->Article->save($this->request->data)) {
$this->Session->setFlash(__('The article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
}
}
$users = $this->Article->User->find('list');
$this->set(compact('users'));
}
上述方法在文章
中返回user_id的空值我也尝试从视图中检索:
echo $this->Form->hidden('user_id', arrray('value' => $userAuth['id']));
我收到以下错误 语法错误,意外'=>' (T_DOUBLE_ARROW)
答案 0 :(得分:0)
echo $this->Form->hidden('user_id', arrray('value' => $userAuth['id']));
应该是,
echo $this->Form->hidden('user_id', array('value' => $userAuth['id']));
阵列中的错字(R的3倍)。
答案 1 :(得分:0)
试试这个:
public function add() {
if ($this->request->is('post')) {
$this->Article->create();
if ($this->Article->save($this->request->data)) {
$this->Session->setFlash(__('The article has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The article could not be saved. Please, try again.'));
}
else{
$this->set('user_id', $userAuth['id']);
$users = $this->Article->User->find('list');
$this->set(compact('users'));
}
}
视图中的
echo $this->Form->hidden('Article.user_id', arrray('value' => $user_id));
但是我不知道你在哪里设置$userAuth
,在获得它的值之前分配这个变量是安全的