所以,我在我的 CakePHP 项目中添加了一个“Like”系统,但我遇到了一个小问题。当我想添加一个赞时......它根本不会发生。这是我所拥有的:
<?php
echo $this->Form->create("Like", array("url" => array("controller" => "likes", "action" => "like")));
echo $this->Form->hidden("user_id",array(
"value" => AuthComponent::user("id")
));
echo $this->Form->hidden("post_id",array(
"value" => $post["Post"]["id"]
));
echo $this->Form->end();
echo $this->Html->link(
"<li><i class='icon-heart icon-large'></i> ".count($post["Like"])." likes</li>",
"",
array("escape" => false, "onclick" => "document.getElementById('LikeViewForm').submit();")
);
?>
这是控制器:
<?php class LikesController extends AppController{
function like() {
$user_id = $this->Auth->user("id");
if(!$user_id){
$this->redirect("/");
die();
}
if ($this->request->is("post")) {
$d = $this->request->data;
$d["Like"]["id"] = null;
if($this->Like->save($d,true,array("post_id","user_id"))){
$this->redirect($this->referer());
}
}
}
}
当我单击链接时,它会正确刷新页面,所以我认为表单已发送,但数据库中没有任何新内容......
有什么猜测吗?