2012-12-15 4 views
0

사용자가 링크를 클릭하여 게시물을 좋아할 수있는 간단한 메커니즘을 구축 중입니다. POST를 사용하지 않고 GET을 사용하고 있습니다. URL을 통해 메소드가 실행되도록하고 싶습니다.CakePHP에서 GET 요청에 대한 데이터 저장

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의 DB에 새로운 행을 저장하고 있습니다.

답변

1

안녕하세요 당신은 컨트롤러 동작에 대한 링크를 만들고 URL에 변수를 전달해야합니다.

게시자의 게시물에있는 링크가 분명해야합니다 : $ this-> Html-> link ('this like post', array ('controller'=> 'like', 'action' => '추가'그것은이 같은 링크를 렌더링해야

, $ postId)) : www.yourWebSite/좋아 이/

변수 액션 후 postId (1), (추가를 좋아하는/1 추가)이다 귀하의 컨트롤러 동작에 대한 변수로 해석됩니다.

만약 귀하의 추가 기능이

public function add($postId, $wathever){ 

} 

URL은 www.yourWebSite/likes/add/1/blabla와 같아야합니다. 여기서 1은 추가 작업의 첫 번째 var이고 두 번째 것은 blabla입니다.

이가 아닌 재 작성 URL의 해당 : postId = 1 & 어떤 = 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!'); 
       } 

}

+0

안녕하세요. 그래, 나는 이미 모든 것을 끝냈다. (OP 참조). 문제는 일반적으로 요청 데이터를 저장하는 것처럼 GET에 데이터를 저장하는 것이지만 POST 및 GET에 존재하지 않습니다. – Cameron

+0

좋아, 미안하지만,이 질문에 대해 잘 모르겠다. www.myWebSite.com/like/add?post=1&whatever=blabla $ postId = $ this-> params [ 'url'] [ ' 게시하다']; $ whatever = $ this-> params [ 'url'] [ 'whatever']; 그게 네가 묻고있는거야? – guignol

+0

아니요 데이터가 제대로 전달됩니다. 문제는 일반적으로 POST 메서드를 통해 DB 레코드를 만들고이를 GET 메서드에 저장하는 것입니다. 나는 창조를 시도하고 저장한다 그러나 그것을 좋아하지 않는다. – Cameron

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'])); 


?> 
관련 문제