持续收集一对多/多对一关联

时间:2013-12-02 02:37:32

标签: symfony doctrine-orm symfony-2.3

我遇到了一个问题,要坚持一对多/多对一的关联。

我的目标是拥有一个表单来保存数据“结构”,其中包括作为集合呈现的联系人。

db如下:

结构

class Structure
{
    /**
     * @ORM\OneToMany(targetEntity="ContactStructure", mappedBy="structure_have_contact", cascade={"all"})
     */
     private $contacts;

     [...]

    /**
     * Add contacts
     *
     * @param \Acme\CoreBundle\Entity\ContactStructure $contacts
     * @return Structure
     */
     public function addContact(ContactStructure $contacts)
     {
         $this->contacts->add($contacts);
     }

class Contact
{
    /**
     * @ORM\OneToMany(targetEntity="ContactStructure", mappedBy="type_contact", cascade={"all"})
     */
     private $typology;

     [...]

     /**
      * Add typology
      *
      * @param \Acme\CoreBundle\Entity\ContactStructure $typology
      * @return Contact
      */
      public function addTypology(\Acme\CoreBundle\Entity\ContactStructure $typology)
      {
          $this->typology[] = $typology;

          return $this;
      }

ContactStructure

class ContactStructure
{
    /**
     * @var string
     *
     * @ORM\Column(name="contact", type="string", length=255)
     */
    private $contact;

    /**
     * @ORM\ManyToOne(targetEntity="Structure", inversedBy="contacts", cascade={"all"})
     * @ORM\JoinColumn(name="structure_id", referencedColumnName="id")
     */
    private $structure_have_contact;

    /**
     * @ORM\ManyToOne(targetEntity="Contact", inversedBy="typology", cascade={"all"})
     * @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
     */
    private $type_contact;

    [...]

    /**
     * Set structure_have_contact
     *
     * @param \Acme\CoreBundle\Entity\Structure $structureHaveContact
     * @return ContactStructure
     */
    public function setStructureHaveContact(\Acme\CoreBundle\Entity\Structure $structureHaveContact = null)
    {
        $this->structure_have_contact = $structureHaveContact;

        return $this;
    }

    /**
     * Set type_contact
     *
     * @param  $typeContact
     * @return ContactStructure
     */
    public function setTypeContact($typeContact = null)
    {
        $this->type_contact = $typeContact;

        return $this;
    }

StructureType

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder->add('contacts', 'collection', array(
            'type' => new ContactStructureType(),
            'label' => false,
            'allow_add'    => true,
            'allow_delete' => true,
        ))
        [...]

ContactStructureType

$builder
        ->add('type_contact', 'entity', array(
            'class'    => 'AcmeCoreBundle:Contact' ,
            'property' => 'type'
        ))
        ->add('contact')
    ;

控制器

if ($form->handleRequest($request)->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($structure);

        foreach ($structure->getContacts() as $contact) {
            $contact->setTypeContact($structure);
            $em->persist($contact);
        }

        $em->flush();

        return $this->redirect($this->generateUrl('structure_show', array('id' => $structure->getId())));
}

当我保存表单时,我收到此错误:

Found entity of type Acme\CoreBundle\Entity\Structure on association Acme\CoreBundle\Entity\ContactStructure#type_contact, but expecting Acme\CoreBundle\Entity\Contact

知道我哪里错了吗?

1 个答案:

答案 0 :(得分:0)

ContactStructure 实体,

setTypeContact()方法需要 $ typeContact 联系类的对象

/**
 * Set type_contact
 *
 * @param  \Acme\CoreBundle\Entity\Contact $typeContact
 * @return ContactStructure
 */
public function setTypeContact(\Acme\CoreBundle\Entity\Contact $typeContact = null)
{
    $this->type_contact = $typeContact;

    return $this;
}