Symfony2使用属性保留ManyToOne

时间:2014-07-23 12:59:31

标签: php entity-framework symfony doctrine-orm

当我尝试保留我的实体时会出现此错误:

  

警告:spl_object_hash()期望参数1为object,integer   给定的   C:\ WAMP \ WWW \谟\供应商\原则\ ORM \ LIB \原则\ ORM \ UnitOfWork.php   第1601行

这是我的实体网站:

/**
 * Etablissement
 *
 * @ORM\Table()
 * @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\SiteRepository")
 */
class Site {
 /**
  * @var integer
  *
  * @ORM\Column(name="id", type="integer")
  * @ORM\Id
  * @ORM\GeneratedValue(strategy="AUTO")
  */
 private $id; /**
  * @ORM\OneToMany(targetEntity="Intranet\RhBundle\Entity\SiteEtablissement",
 mappedBy="site", cascade={"persist", "remove"}, orphanRemoval=true)
  * */
 private $siteEtablissements;
 }

这是我的实体Etablissement:

/**
* Etablissement
*
* @ORM\Table()
* @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\EtablissementRepository")
*/
class Etablissement {

/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;
}

关系的实体:

/**
 * @ORM\Entity(repositoryClass="Intranet\RhBundle\Entity\SiteEtablissementRepository")
 */
class SiteEtablissement {

/**
 * @ORM\Id
 * @ORM\Column(type="integer")
 * @ORM\GeneratedValue(strategy="AUTO")
 */
protected $id;

/**
 * @ORM\ManyToOne(targetEntity="Intranet\RhBundle\Entity\Site", inversedBy="siteEtablissements")
 * @ORM\JoinColumn(name="site_id", referencedColumnName="id")
 */
private $site;

/**
 * @ORM\ManyToOne(targetEntity="Intranet\RhBundle\Entity\Etablissement")
 * @ORM\JoinColumn(name="etablissement_id", referencedColumnName="id", onDelete="CASCADE")
 */
private $etablissement;
}

所以我不明白为什么我有这个错误,我的映射似乎没问题。我没有任何实体错误,我之前没有错误在我的控制器中保留$ site。

这是我的控制者:

public function gererEtablissementAction(Request $request, Site $site) {
    $form = $this->createForm(new GererSiteEtabType(), $site);
    if ($request->getMethod() == 'POST') {
        $form->handleRequest($request);
        if ($form->isValid()) {
            $em = $this->getDoctrine()->getManager();
            foreach ($site->getSiteEtablissements() as $sitEtab)
                if (is_null($sitEtab->getAutor()))
                    $sitEtab->setAutor($this->getUser());
            $em->persist($site);
            $em->flush();
            $this->get('session')->getFlashBag()->add('success', "Les établissement du site " . $site->getNom() . " ont été modifiés avec succès !");
            return $this->redirect($this->generateUrl('intranet_rh_homepage_employes'));
        } else
            $this->get('session')->getFlashBag()->add('danger', "Erreur de formulaire !");
    }
    return $this->render('IntranetRhBundle:Site:gererEtablissement.html.twig', array('site' => $site, 'form' => $form->createView()));
}

当我打印$ site-> getSiteEtablissements时,每个对象的每一面都有Site和Etablissement ......

这是构建表单:

    public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('siteEtablissements', 'collection', array(
                'type' => new SiteEtablissementType(),
                'allow_add' => true,
                'allow_delete' => true,
                'by_reference' => false,
                'required' => false
            ))
    ;
}

子表单:

 public function buildForm(FormBuilderInterface $builder, array $options) {
    $builder
            ->add('etablissement', 'entity', array(
                'label' => 'Etablissement :',
                'class' => 'IntranetRhBundle:Etablissement',
                'property' => 'nom',
                'query_builder' => function(EtablissementRepository $er) {
            // récupère les établissements triés par ordre alpabétique  croissant de nom
            return $er->createQueryBuilder('e')
                    ->orderBy('e.nom', 'ASC');
        },
                'attr' => array('class' => 'form-control')
            ))
    ;
}

1 个答案:

答案 0 :(得分:0)

我发现了错误:Autor的错误初始化。 Autor在" 0"在循环中我测试is_null。所以0不是null,当它持续时,会出现错误,因为没有id为0的用户...