2015-07-13 78 views
4

中调用我在编码时遇到了问题。当我送你的形式,并保存到数据库中,类似这样的消息:Symfony2可捕捉的致命错误:参数1传递给...必须是...整数的一个实例,在

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

    } 

我不知道与此问题。我的其他编码,这种方式工作正常,但我很困惑,当这种方法不能用于我的编码。

回答

3

从你提供的示例中,我想这是线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; 
} 

希望这有助于:)

+0

尼斯,有用。感谢您的帮助 。 –

+0

没问题:)高兴地帮忙。不知道谁低估了你,似乎有点不合理,因为你的问题对我来说似乎很好。 –

+0

对不起,我的英语不太好。 –

相关问题