2013-10-30 67 views
3

我已经阅读thisthis和其他的,但没有人解决了我的问题。Symfony 2嵌入式表单:可捕获的致命错误:传递给Entity :: addProperty的参数1必须是XX MyClass的实例,给出的数组为

我已经删除了所有可以缩小到一个字段的地址:地址。 当我尝试按照this tutorial时,如果我遵循它,从一个新的新项目开始,一切正常。但是,当我在我的其他项目中手动执行该操作时,出现此错误: “Catchable致命错误:传递给BN \ Bundle \ MyBundle \ Entity \ PersonTeacher :: addAdresse()的参数1必须是BN \ Bundle \ MyBundle \ Entity \ Adresse,在C:\ Users \ Olivier \ PhpstormProjects \ My \ My \ src \ BN \ Bundle \ MyBundle \ Entity \ PersonTeacher.php第111行中给出的数组。

继承人的堆栈跟踪: stacktracke

这里是我的代码,在那里我已经删除该工作性质:


我的控制器:

<?php 

namespace BN\Bundle\MyBundle\Controller; 

use BN\Bundle\MyBundle\Entity\PersonTeacher; 
use BN\Bundle\MyBundle\Entity\Adresse; 
use BN\Bundle\MyBundle\Form\Type\AdresseType; 
use Symfony\Component\DependencyInjection\ContainerAware; 
use Symfony\Component\HttpFoundation\Request; 
use Symfony\Component\HttpFoundation\RedirectResponse; 
use Symfony\Bundle\FrameworkBundle\Controller\Controller; 

class RegistrationController extends Controller 
{ 
    public function registerTeacherAction(Request $request) 
    { 
     $person = new PersonTeacher(); 
     $form = $this->createFormBuilder($person) 
      ->add('adresses', 'collection', 
       array(
        'type' => new AdresseType(), 
        'allow_add' => true, 
        'by_reference' => false, 
       ) 
      ) 
     ->getForm(); 
     $form->handleRequest($request); 
     if ($form->isValid()) { 
      /**/ 
     } 
     return $this->render('BNMyBundle:Registration:person_teacher.form.html.twig', array(
      'form' => $form->createView(), 
     )); 
    } 
} 

我的实体PersonTeacher :

<?php 

namespace BN\Bundle\MyBundle\Entity; 
use Doctrine\ORM\Mapping as ORM; 

/** 
* @ORM\Entity 
* @ORM\Table(name="person_teacher") 
* @ORM\Entity(repositoryClass="BN\Bundle\MyBundle\Repository\PersonTeacherRepository") 
* @ORM\HasLifecycleCallbacks() 
*/ 
class PersonTeacher extends Person 
{ 
    /** 
    * @ORM\Id 
    * @ORM\Column(type="integer") 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    protected $id; 

    /*--------- snap ---------*/ 

    /** 
    * @ORM\ManyToMany(
    *  targetEntity="Adresse", 
    *  inversedBy="personsTeacher", 
    *  cascade={"persist"} 
    *) 
    * @ORM\JoinTable(name="person_teacher_adresse") 
    **/ 
    private $adresses; 

    /*--------- snap ---------*/ 

    public function addAdresse(\BN\Bundle\MyBundle\Entity\Adresse $adresse) { 
     $this->adresses[] = $adresse; 
     return $this; 
    } 
    public function removeAdresse(\BN\Bundle\MyBundle\Entity\Adresse $adresse) { 
     $this->adresses->removeElement($adresse); 
    } 
    public function getAdresses() { 
     return $this->adresses; 
    } 
    /*--------- snap ---------*/ 
    public function __construct() { 
     parent::__construct(); 
     $this->adresses = new \Doctrine\Common\Collections\ArrayCollection(); 
    } 
} 

提示

注:我已经签了形式正确发送,这些信息的格式正确,我也一步一步地与Xdebug的步调试,但它太复杂对我来说(但)看到我缺少的东西。

这是一个'collection'类型的问题:如果我删除'by_reference' => false,那么它的工作....只有当我不坚持它。

如果我尝试使用此代码坚持它:

$form->handleRequest($request); 
    if ($form->isValid()) { 
     $person=$form->getData(); 
     $em = $this->getDoctrine()->getManager(); 
     $em->persist($person); 
     $em->flush(); 
    } 

然后我得到这个错误:

警告:spl_object_hash()预计参数1是对象,在给定的数组(projectpath)\ vendor \ doctrine \ orm \ lib \ Doctrine \ ORM \ UnitOfWork.php line 1572

为什么?

+0

如果你把'ManyToMany'改成'OneToMany'作为测试怎么办? – cheesemacfly

+0

我试图改变另一个'ManyToMany'关系:课程(一位教师可能会给一个或多个课程,一个或多个教师可能会给一个课程)。有用。所以唯一的解释是有一些字段或一个声明丢失,但我真的不知道可能会丢失什么。 –

回答

9

我找到了。

问题出在我的FormTypeAddressType.php文件:功能setDefaultOptions()丢失。一切都很好,但是如果你想让学说能够把传入的字段转换成一个特定的类,这个函数必须是强制性的

public function setDefaultOptions(
    \Symfony\Component\OptionsResolver\OptionsResolverInterface $resolver 
) { 
    $resolver->setDefaults(array(
     'data_class' => 'MyBundle\Entity\Adresse', 
    )); 
} 

我很生气,因为我失去了那个时间。你必须知道整个Symfony 2流程能够快速解决这些问题,或者需要6个小时才能解决这些问题。 Symfony 2本身就是一个整体leaky abstraction:你感觉像获得时间,但你没有。Symfony应该简化你的生活,但最后引用JoëlSpolsky:

It's a leaky abstraction: it means that abstractions do not really simplify our lives as much as they were meant to. Code generation tools which pretend to abstract out something, like all abstractions, leak, and the only way to deal with the leaks competently is to learn about how the abstractions work and what they are abstracting.

+0

你救了我一天 – Nico

+0

我救了你,但是失去了一个**':^ /'** –

相关问题