사용자가 링크를 클릭하여 게시물을 좋아할 수있는 간단한 메커니즘을 구축 중입니다. 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에 새로운 행을 저장하고 있습니다.
안녕하세요. 그래, 나는 이미 모든 것을 끝냈다. (OP 참조). 문제는 일반적으로 요청 데이터를 저장하는 것처럼 GET에 데이터를 저장하는 것이지만 POST 및 GET에 존재하지 않습니다. – Cameron
좋아, 미안하지만,이 질문에 대해 잘 모르겠다. www.myWebSite.com/like/add?post=1&whatever=blabla $ postId = $ this-> params [ 'url'] [ ' 게시하다']; $ whatever = $ this-> params [ 'url'] [ 'whatever']; 그게 네가 묻고있는거야? – guignol
아니요 데이터가 제대로 전달됩니다. 문제는 일반적으로 POST 메서드를 통해 DB 레코드를 만들고이를 GET 메서드에 저장하는 것입니다. 나는 창조를 시도하고 저장한다 그러나 그것을 좋아하지 않는다. – Cameron