Button cakephp在控制器中设置值不工作
我正在用cakephp做一个小项目。我做了一个用户列表,在每个用户旁边都有一个按钮,上面有文字“addasfriend”。在数据库中,我有一个名为friends的表,其中包含一个ID、user_ID(单击add as friend的用户)和friend_ID(原始用户希望作为朋友的人)。现在,我在UsersController中执行了以下方法:Button cakephp在控制器中设置值不工作,button,cakephp,add,Button,Cakephp,Add,我正在用cakephp做一个小项目。我做了一个用户列表,在每个用户旁边都有一个按钮,上面有文字“addasfriend”。在数据库中,我有一个名为friends的表,其中包含一个ID、user_ID(单击add as friend的用户)和friend_ID(原始用户希望作为朋友的人)。现在,我在UsersController中执行了以下方法: public function addFriend($id) { $usersFriendsTable = TableRegistry::get
public function addFriend($id)
{
$usersFriendsTable = TableRegistry::getTableLocator()->get('Friends');
$uid = $this->Auth->user('id'); //Id of the currently logged in user
$userAndFriend = $usersFriendsTable->newEntity();
if($this->request->is('post'))
{
$userAndFriend->user_id = $uid; //trying to set the user_id field to the id of the user that logged in
$userAndFriend->friend_id = $id; //trying to set the friend_id to the parameter id
$userAndFriend = $usersFriendsTable->patchEntity($userAndFriend, $this->request->getData());
if($usersFriendsTable->save($userAndFriend))
{
$this->Flash->success('User Added as Friend!');
}
else
{
$this->Flash->error("User not Added as Friend!");
}
}
}
唯一的问题是,单击按钮时,它甚至不会输入:
if($this->request->is('post'))
我知道这一点,因为如果我在帖子之前设置值,我会得到正确的值,但是当我在帖子之后设置值时,我什么也得不到,只会被重定向到主页
这是我的按钮的代码:
echo "<td>".$this->Html->link('Add as Friend',
[
'controller' => 'Users',
'action' => 'addFriend',
$u->id
],
[
'class' => 'btn btn-success col-md-12'
])."</td>";
echo'.$this->Html->link('addasfriend',
[
“控制器”=>“用户”,
“操作”=>“添加朋友”,
$u->id
],
[
“类”=>“btn btn成功col-md-12”
])."";
我做错了什么?检查使用了什么请求方法:
debug($this->request->method())代码>请不要回显html标记!在实体中允许用户id和朋友id