2012-10-19 2 views
1

$ id 등록 정보 및 getId() 메소드가있는 질문 클래스가 있습니다. 나는 또한 그 질문에 대한 답변의 수를 표시하고자하는 컨트롤러에 인덱스 액션을 가지고있다. 내 indexSuccess 템플릿에서조치의 메소드 호출

class questionActions extends sfActions 
{ 
    public function executeIndex(sfWebRequest $request) 
    {   
     $q_id = $this->getQuestion()->getId(); 

     $this->answers = Doctrine_Core::getTable('answer') 
               ->createQuery('u') 
               ->where('u.question_id = ?', $q_id) 
               ->execute(); 
    } 

:

<?php if ($answers) : ?> 
    <p><?php echo count($answers) ?> answers to this request.</p> 
<?php endif; ?> 

그러나,이 오류 결과

가 : 정의되지 않은 메서드를 호출합니다.

$ q_id 값을 수동으로 할당하면 모든 것이 완벽하게 작동합니다.

조치에서 getId() 메소드 호출을 지정하려면 어떻게해야합니까? 그 호출이 컨트롤러에 있어야할까요?

+0

'getQuestion()'메서드를 볼 수 있습니까? – j0k

답변

2

getQuestion()이 컨트롤러에 구현되어 있지 않기 때문에 오류가 발생합니다.

질문 ID를 GET 매개 변수로 전달한다고 가정합니다.

class questionActions extends sfActions { 

    public function executeIndex(sfWebRequest $request) { 
     $q_id = $request->getParameter('question_id'); 

     $question = Doctrine_Core::getTable('question')->find($q_id); 

     $this->answers = Doctrine_Core::getTable('answer') 
     ->createQuery('u') 
     ->where('u.question_id = ?', $question->getId()) 
     ->execute(); 
    } 

또는 더 나은

class questionActions extends sfActions { 

    public function executeIndex(sfWebRequest $request) { 
    $q_id = $request->getParameter('question_id'); 
    $question = Doctrine_Core::getTable('question')->find($q_id); 
    $this->answers = $question->getAnswers(); 
    } 
+0

도움 주셔서 감사합니다. –

2

가 잘 나는 fatest 방법은 당신의 매개 변수 경우 (질문 id 매개 변수와 쿼리를 직접 호출하는 것입니다 생각 : 당신이 뭔가를 시도 할 수 있습니다이 경우

URL에 id입니다 :

class questionActions extends sfActions 
{ 
    public function executeIndex(sfWebRequest $request) 
    { 
    // redirect to 404 automatically if the question doesn't exist for this id 
    $this->question = $this->getRoute()->getObject(); 

    $this->answers = $this->question->getAnswers(); 
    } 

그런 다음 당신은 012,368,287을 정의 할 수 있습니다, 그래서 당신은 질문이 주어진 id에 존재 하는지를 점검 할 필요가 없으며, 심포니 자체의 일이 될 것입니다. 당신이 URL을 /question/23를 호출 할 때

question_index: 
    url:  /question/:id 
    class: sfDoctrineRoute 
    options: { model: Question, type: object } 
    param: { module: question, action: index } 
    requirements: 
    id: \d+ 
    sf_method: [get] 

그런 다음, 자동으로 ID 23으로 질문을 검색하려고합니다. 이 질문이 없으면 404로 리디렉션됩니다.