Symfony 错误 - 无法访问私有属性 AppBundle\Entity\VacancyEntity::$jobDescription

Cha*_*dra -1 php doctrine symfony symfony-2.8

当我尝试访问 symfony 2.8 中的获取结果时,出现此错误。这是我的代码

 public function staticAction(Request $request)
    {   


      $jobid = $this->get('session')->get('jobid');
      $jobDetails = $this->getVacancyData($jobid);
      echo "<pre>";
      var_dump($jobDetails);
      $description = $jobDetails->jobDescription;


      return $this->render('FrontEnd/job.html.twig', array('jobdetails' => $jobDetails ));
    }

    public function getVacancyData($id){

        $vacancy = $this->getDoctrine()
        ->getRepository(VacancyEntity::class)
        ->findOneBy(array('id' => $id));


        if (!$vacancy) {
            throw $this->createNotFoundException();
        }else{
            return $vacancy;
        }  

    }
Run Code Online (Sandbox Code Playgroud)

以下行发生错误,

$description = $jobDetails->jobDescription;
Run Code Online (Sandbox Code Playgroud)

怎么解决这个问题呢?

Ale*_*eri 5

我认为你需要像这样调用 getter:

$description = $jobDetails->getJobDescription();
Run Code Online (Sandbox Code Playgroud)

因为在您的实体中属性是私有的,所以您需要使用 getter

我不知道您的实体中是否有一个名为 getJobDescription 的 getter,但它应该有一个返回属性的函数jobDescription