2012-07-11 47 views
1

传递参数,这是我的控制器(未完明显,但“做一些神奇的”我首先要使其运行):CakePHP的:缺少参数错误,通过URL

public function add($customer_id, $question_set_id, $order_value) { 

     $customer_id = $this->params['url']['customer_id']; 
     $question_set_id = $this->params['url']['question_set_id']; 
     $order_value = $this->params['url']['order_value']; 

     $possible_answer_model = ClassRegistry::init('PossibleAnswer'); 
     $question_model = ClassRegistry::init('Question'); 
     $order_model = ClassRegistry::init('Order'); 

     $order = $order_model -> find('first', array(
     'Order.question_set_id' => $question_set_id, 
     'Order.value' => $order_value)); 

     $question = $question_model -> find('first', array(
     'Question.id' => $order['Order']['question_id'])); 

     $this -> set('question', $question); 

     if ($question['Question']['kind'] != "o") { 
      $this -> set('possible_answers', $possible_answer_model -> find('all', array(
      'PossibleAnswer.question_id' => $question['Question']['id']))); 
     } 
. 
. 
. 

我得到的是:

警告(2):缺少参数1 AnswersController ::添加()[APP \控制器\ AnswersController.php,线10]

警告(2):缺少参数2 AnswersController ::添加() [APP \ Controller \ AnswersController.php,第10行]

警告(2):缺少参数3 AnswersController ::添加()[APP \控制器\ AnswersController.php,线10]

我的链接是:

/答案/添加/?CUSTOMER_ID = 1 & question_set_id = 1 & ORDER_VALUE = 1

回答

3

的链接必须是

/answers/add/1/1/1 

,以映射到

AnswersController::add($customer_id, $question_set_id, $order_value) 

定期查询参数已经通过$this->params控制器内部进行访问,他们不是作为参数传递给操作传递。

+0

哦,我发现“我的方式”在食谱的某个地方,所以我可能搞砸了 - 链接正在工作,但我得到“未定义索引”错误$ customer_id = $ this-> params ['url' ] ['customer_id']和另外两个$ this-> params ... – smsware 2012-07-11 06:25:43

+0

这是或者。如果你没有在URL中传递它们作为'?customer_id = 1',它们在'$ this-> params'中不可用。 – deceze 2012-07-11 06:29:15

+0

哦,现在工作 - 谢谢! – smsware 2012-07-11 06:30:58

0
public function add($customer_id=null, $question_set_id=null, $order_value=null) { 
    .... 
} 

大概是在需要的时候不传递你需要的参数,你可以做上面的事情,它不需要它。

+0

对不起,现在我编辑了我的问题,我显示了巫婆的链接,我试图传递这些参数(这是必要的)。 – smsware 2012-07-11 06:19:26

0

我正在使用自定义路线,这为我解决。

Router::connect('/me/edit/:id', array('controller' => 'users', 'action' => 'edit'), 
     array('pass' => array('id'), 'id' => '[0-9]+') 
    ); 

感谢@ david-yell在https://stackoverflow.com/a/17989140/1424473