0

我正在构建一个简单的机制,用户可以通过单击链接来喜欢帖子。我使用的是 GET 而不是 POST,因为我想允许该方法通过 URL 触发。

有人说过如何使用 GET 保存数据?由于在这种情况下不存在请求数据......我的模型看起来像:

class Like extends AppModel
{
    public $name = 'Like';

    public $belongsTo = array('User','Post');

}

添加方法如下:

public function add( $id )
{
    $post = $this->Post->find('first', array( 
                'conditions' => array('Post.id'=>Tiny::reverseTiny($id)) 
            ));

    if (!$post)
    {
        throw new NotFoundException('404');
    }

    if($post['Post']['user_id'] == $this->Auth->user('id'))
    {
        $this->Session->setFlash('You can\'t like your own post... That\'s just silly!');
    }

    if ($this->Like->create())
    {
            $liked = $this->Like->find('first', array( 
                'conditions' => array('Like.id'=>Tiny::reverseTiny($id), 'Like.user_id'=>$this->Auth->user('id') ) 
            ));

            if(!$liked){
                $this->Like->saveField('user_id', $this->Auth->user('id'));
                $this->Like->saveField('post_id', $post['Post']['id']);

                $this->redirect(array('controller'=>'posts','action'=>'view','id'=>Tiny::toTiny($post['Post']['id']),'slug'=>$post['Post']['slug']));
            } else {
                $this->Session->setFlash('You already like this post!');
            }
    else
    {
        $this->Session->setFlash('Server broke!');
    }   
}

任何人都可以帮忙吗?

<?php echo $this->Html->link('1', array('controller'=>'followers','action'=>'add','id'=>Tiny::toTiny($post['Post']['id'])), array('title'=>'Follow','class'=>'follow')); ?>

这部分一切正常。它在我正在努力解决的 GET 上的数据库中保存了一个新行。

4

2 回答 2

1

嗨,您只需要链接到您的控制器操作并在 url 中传递您的变量。

要清除帖子上要喜欢的链接在您的帖子视图中: $this->Html->link('like this post', array('controller' => 'like', 'action' => 'add' , $postId))

它应该呈现这样的链接:www.yourWebSite/likes/add/1 喜欢 postId 1,

您的操作(添加)之后的变量被解释为您的控制器操作的变量

如果你的函数添加是

public function add($postId, $wathever){

}

url 应该看起来像 www.yourWebSite/likes/add/1/blabla ,其中 1 是添加操作的第一个变量,blabla 是第二个变量,依此类推。

这相当于非重写 url:?postId=1&whatever=blabla

编辑 :

if(!$liked){
                //simulate the post behaviour
                $this->request->data['Like']['user_id'] = $this->Auth->user('id');
                $this->request->data['Like']['post_id'] = $post['Post']['id'];

                //save the data
                if ($this->Like->save($this->request->data)) {
                    $this->Session->setFlash(__('Thanks for your support !'));
                    $this->redirect(array('controller'=>'posts','action'=>'view','id'=>Tiny::toTiny($post['Post']['id']),'slug'=>$post['Post']['slug']));
                } else {
                    $this->Session->setFlash('Server broke!');
                }

}

于 2012-12-15T16:01:58.393 回答
1

使用save with id=0而不是怎么样create

<?php

    $like = array(
        "Like" => array
        (
            "id" => 0,
            "user_id" => $this->Auth->user("id"),
            "post_id" => $post['Post']['id']
        )
    );
    $result = $this->Like->save($like);
    if(!$result){$this->Session->setFlash('Server broke!');}

    $this->redirect(array('controller'=>'posts','action'=>'view','id'=>Tiny::toTiny($post['Post']['id']),'slug'=>$post['Post']['slug']));


?>
于 2012-12-15T16:13:08.030 回答