Symfony2 Catchable fatal error: Argument 1 passed to ... must be an instance of ... integer given, called in

Symfony2 Catchable fatal error: Argument 1 passed to ... must be an instance of ... integer given, called in

我在编码方面遇到问题。当我向您发送表格并保存到数据库时,会出现这样的消息:

Catchable fatal error: Argument 1 passed to ITTBundle\Entity\Student::setFormClass() must be an instance of ITTBundle\Entity\FormClass, integer given, called in C:\Users\Rivan\Documents\DigitalSchoolBase\Symfony2\src\ITTBundle\Controller\ConfigureController.php on line 601 and defined in C:\Users\Rivan\Documents\DigitalSchoolBase\Symfony2\src\ITTBundle\Entity\Student.php on line 901

这是我的代码:

  $findclass = $this->getDoctrine()
    ->getRepository('ITTBundle:FormClass')
    ->findOneBy(array('class_level' => $classlevel->getId(), 'letter' => $letter, 'class_major' => $classmajor->getId()));

    //print_r($findclass->getId()); exit;
    if( empty($error_message) )    
    {
      If ($findclass)
      {
        $em = $this->getDoctrine()->getManager();
        $students->setFormClass($findclass->getId());
        $em->persist($students);
        $em->flush();
      }

    }

不知道是哪个问题。我的另一个编码,这个方法很好用,但是当这个方法不能用在我的编码中时,我很困惑。

根据您提供的示例,我假设这是行 601,它触发了致命错误:

$students->setFormClass($findclass->getId());

我假设方法调用 \ITTBundle\Entity\Student::setFormClass() 需要类型 \ITTBundle\Entity\FormClass 的对象。您正在从数据库返回一个实体,但随后您明确传递了对象的 id - 这是一个整数 - 而不是 FormClass 对象本身。

试试这个:

$students->setFormClass($findclass);

我们只能确定您是否向我们展示了 \ITTBundle\Entity\Student::setFormClass() 的方法签名,但这是我半知半解的假设。鉴于方法调用本身为意外参数类型抛出错误,我假设方法参数是类型化的,所以我猜它可能看起来像这样:

public function setFormClass(\ITTBundle\Entity\FormClass $formClass)
{
    $this->formClass = $formClass;
}

希望对您有所帮助:)