Symfony2可捕获的致命错误:传递给...的第一个参数必须是一个实例,但传递了一个整数,在...中调用。

5

我在编码方面遇到了问题。当我发送表单并保存到数据库时,会出现以下类似的消息:

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();
      }

    }

我想知道这个问题属于哪类。我的其他代码可以正常运行,但是当我在这段代码中使用这种方法时出现了困惑。

1个回答

5

从您提供的示例中,我推测这是触发致命错误的第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;
}

希望这有所帮助 :)

没问题 :) 很高兴能帮忙。不知道谁给你点了踩,看起来有点不合理,因为我觉得你的问题很好。 - Darragh Enright
没问题!我理解了你的问题。 - Darragh Enright

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接