$ this-> getRoute() - > getObject()失去上下文的问题。

时间:2011-11-14 21:28:02

标签: symfony1

请在下面的模板部分查看我的问题:

表格(showSuccess):

<?php echo form_tag('job/salarySubmit') ?>
<input type="hidden" name="job_id" value="<?php echo $job->getId(); ?>">
<input type="submit" value="View Salary">
</form> 

动作:

public function executeSalarySubmit(sfWebRequest $request)
{
    $this->forward404Unless($request->isMethod('post'));
    $param = array('job_id' => $request->getParameter('job_id'), ); 
    $this->redirect('job/salary?'.http_build_query($param)); 
}

public function executeSalary(sfWebRequest $request)
{
    $this->object_id = $request->getParameter('job_id');
    $this->salary = $this->getRoute()->getObject();          
}

模板(salarySuccess.php):

<?php echo $object_id; ?>  // returns correct job_id: 6100, but when I try to access the object's other getters (example: echo $object_id->getName(), I get "Fatal error: Call to a member function getName() on a non-object"
<?php echo $salary->getName(); ?> //works, but it gets the wrong name. It's returning the  first job in my DB with an ID of "1" which is not correct... it needs to be 6100

路由:

job_salary:
  url:      /job/salary/:job_id
  param:    { module: job, action: salary }
  class:   sfDoctrineRoute
  options: { model: Job, type: object }
  requirements:
    id: \d+
    sf_method: [GET]    

1 个答案:

答案 0 :(得分:1)

在模板变量上使用var_dump()。 您将看到$object_id不是对象(它是非对象,如错误消息所示)。 $object_id很可能是字符串或整数,其值必须为“6100”。 $salary不是您要查找的对象,因为sfDoctrineRoute查找名为id的参数,并在routing.yml中将其命名为job_id(这很奇怪,因为您将其命名为{{1}在这条路线的要求中。