如何在Symfony2中使用OneToMany和ManyToOne关系的集合?

时间:2013-08-19 07:03:01

标签: symfony doctrine-orm symfony-2.3

我有一个数据库,其中包含联系人(联系人实体),联系人亲属( ContactRelative 实体)和各种场合( AnniBirth 实体)。我首先要做的是在Contact实体中保持Contact的详细信息。其次,我输入所有联系人的亲属(配偶,孩子等),我希望能够进入最多4次(生日,周年纪念日等)。我一直在研究Symfony2和doctrine的关系/协会,以实现这一点,我决定使用“集合”。

除了AnniBirths到ContactRelatives之外,一切都很清楚,我必须展示一系列嵌入的AnniBirth表格并将它们保存回数据库。

到目前为止,关系是:

  • 1 联系可以有多个亲戚
  • 1 联系可以有多个 AnniBirth
  • 1 相对可以有多个 AnniBirth

要求:

  • 我将能够查看所有联系人的分娩,亲属和他们的 AnniBirths。

Contact.php

 <?php
    // src/********/***Bundle/Entity/Contact.php

    namespace ********\***Bundle\Entity;

    use Doctrine\ORM\Mapping as ORM;
    use Doctrine\Common\Collections\ArrayCollection;
    use Symfony\Component\Validator\Constraints as Assert;

    /**
     * @ORM\Entity
     * @ORM\Table(name="contact")
     */
    class Contact
    {    
        /**
         * @ORM\OneToMany(targetEntity="********\***Bundle\Entity\AnniBirth", mappedBy="contact", cascade={"all"})
         */
        protected $annibirths;

        /**
         * @ORM\OneToMany(targetEntity="********\***Bundle\Entity\ContactRelative", mappedBy="contact", cascade={"all"})
         */
        protected $contactRelatives;

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

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $firstname;

        /**
         * @ORM\Column(type="string", length=100)
         */
        protected $lastname;


        /**
         * Constructor
         */
        public function __construct()
        {
            $this->annibirths = new ArrayCollection();
            $this->contactRelatives = new ArrayCollection();
        }

        public function __toString()
        {
            return $this->getFirstName();
        }

        /**
         * Get id
         *
         * @return integer 
         */
        public function getId()
        {
            return $this->id;
        }

        /**
         * Set firstname
         *
         * @param string $firstname
         * @return Contact
         */
        public function setFirstname($firstname)
        {
            $this->firstname = $firstname;

            return $this;
        }

        /**
         * Get firstname
         *
         * @return string 
         */
        public function getFirstname()
        {
            return $this->firstname;
        }

        /**
         * Set lastname
         *
         * @param string $lastname
         * @return Contact
         */
        public function setLastname($lastname)
        {
            $this->lastname = $lastname;

            return $this;
        }

        /**
         * Get lastname
         *
         * @return string 
         */
        public function getLastname()
        {
            return $this->lastname;
        }

        /**
         * Add annibirths
         *
         * @param \********\***Bundle\Entity\AnniBirth $annibirths
         * @return Contact
         */
        public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
        {
            $this->annibirths[] = $annibirths;

            return $this;
        }

        /**
         * Remove annibirths
         *
         * @param \********\***Bundle\Entity\AnniBirth $annibirths
         */
        public function removeAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
        {
            $this->annibirths->removeElement($annibirths);
        }

        /**
         * Get annibirths
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getAnnibirths()
        {
            return $this->annibirths;
        }

        /**
         * Get contactRelatives
         *
         * @return \Doctrine\Common\Collections\Collection 
         */
        public function getContactRelatives()
        {
            return $this->contactRelatives;
        }

        /**
         * Add contactRelatives
         *
         * @param \********\***Bundle\Entity\ContactRelative $contactRelatives
         * @return Contact
         */
        public function addContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelatives)
        {
            $this->contactRelatives[] = $contactRelatives;

            return $this;
        }

        /**
         * Set contactRelatives
         *
         * @param \********\***Bundle\Entity\ContactRelative $contactRelative
         * @return Contact
         */
        public function setContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelative = null) {

            $this->contactRelative = $contactRelative;

            return $this;
        }

        /**
         * Remove contactRelatives
         *
         * @param \********\***Bundle\Entity\ContactRelative $contactRelatives
         */
        public function removeContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelatives)
        {
            $this->contactRelatives->removeElement($contactRelatives);
        }
    }

AnniBirth.php

<?php
// src/********/***Bundle/Entity/AnniBirth.php

namespace ********\***Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="annibirth")
 */
class AnniBirth
{    
    /**
     * @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\Contact", inversedBy="annibirths")
     * @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
     */
    protected $contact;

    /**
     * @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\ContactRelative", inversedBy="annibirths")
     * @ORM\JoinColumn(name="contactRelative_id", referencedColumnName="id")
     */
    protected $contactRelative;

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

    /**
     * @ORM\Column(type="date")
     */
    protected $celebrationDate;

    /**
     * @ORM\Column(type="boolean", nullable=false)
     */
    protected $repeating;

    /**
     * @ORM\Column(type="string", length=30)
     */
    protected $type;

    public function __toString()
    {
        return $this->getType();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set celebrationDate
     *
     * @param \DateTime $celebrationDate
     * @return AnniBirth
     */
    public function setCelebrationDate($celebrationDate)
    {
        $this->celebrationDate = $celebrationDate;

        return $this;
    }

    /**
     * Get celebrationDate
     *
     * @return \DateTime 
     */
    public function getCelebrationDate()
    {
        return $this->celebrationDate;
    }

    /**
     * Set contact
     *
     * @param \********\***Bundle\Entity\Contact $contact
     * @return AnniBirth
     */
    public function setContact(\********\***Bundle\Entity\Contact $contact = null)
    {
        $this->contact = $contact;

        return $this;
    }

    /**
     * Get contact
     *
     * @return \********\***Bundle\Entity\Contact 
     */
    public function getContact()
    {
        return $this->contact;
    }

    /**
     * Set repeating
     *
     * @param boolean $repeating
     * @return AnniBirth
     */
    public function setRepeating($repeating)
    {
        $this->repeating = $repeating;

        return $this;
    }

    /**
     * Get repeating
     *
     * @return boolean 
     */
    public function getRepeating()
    {
        return $this->repeating;
    }

    /**
     * Set type
     *
     * @param string $type
     * @return AnniBirth
     */
    public function setType($type)
    {
        $this->type = $type;

        return $this;
    }

    /**
     * Get type
     *
     * @return string 
     */
    public function getType()
    {
        return $this->type;
    }

    /**
     * Set ContactRelative
     *
     * @param \********\***Bundle\Entity\ContactRelative $contactRelative
     * @return AnniBirth
     */
    public function setContactRelative(\********\***Bundle\Entity\ContactRelative $contactRelative = null)
    {
        $this->contactRelative = $contactRelative;

        return $this;
    }

    /**
     * Get contactRelative
     *
     * @return \********\***Bundle\Entity\ContactRelative 
     */
    public function getContactRelative()
    {
        return $this->contactRelative;
    }
}

ContactRelative.php

<?php
// src/********/***Bundle/Entity/ContactRelative.php

namespace ********\***Bundle\Entity;

use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
use Symfony\Component\Validator\Constraints as Assert;

/**
 * @ORM\Entity
 * @ORM\Table(name="contactRelative")
 */
class ContactRelative
{    
    /**
     * @ORM\ManyToOne(targetEntity="********\***Bundle\Entity\Contact", inversedBy="contactRelatives", cascade={"all"})
     * @ORM\JoinColumn(name="contact_id", referencedColumnName="id")
     */
    protected $contact;

    /**
     * @ORM\OneToMany(targetEntity="********\***Bundle\Entity\AnniBirth", mappedBy="contactRelative", cascade={"all"})
     */
    protected $annibirths;

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

    /**
     * @ORM\Column(type="string", length=100)
     */
    protected $firstname;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $created_at;

    /**
     * @ORM\Column(type="datetime")
     */
    protected $modified_at;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    protected $avatar;

    /**
     * @ORM\Column(type="string", length=1, nullable=true)
     */
    protected $gender;

    /**
     * @ORM\Column(type="string", length=20, nullable=false)
     */
    protected $relation;

    /**
     * Constructor
     */
    public function __construct()
    {
        $this->annibirths = new ArrayCollection();
    }

    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }

    /**
     * Set firstname
     *
     * @param string $firstname
     * @return ContactRelative
     */
    public function setFirstname($firstname)
    {
        $this->firstname = $firstname;

        return $this;
    }

    /**
     * Get firstname
     *
     * @return string 
     */
    public function getFirstname()
    {
        return $this->firstname;
    }

    /**
     * Set created_at
     *
     * @param \DateTime $createdAt
     * @return ContactRelative
     */
    public function setCreatedAt($createdAt)
    {
        if(!$this->getCreatedAt())
          {
            $this->created_at = new \DateTime();
          }
    }

    /**
     * Get created_at
     *
     * @return \DateTime 
     */
    public function getCreatedAt()
    {
        return $this->created_at;
    }

    /**
     * Set modified_at
     *
     * @param \DateTime $modifiedAt
     * @return ContactRelative
     */
    public function setModifiedAt($modifiedAt)
    {
        $this->modified_at = new \DateTime();
    }

    /**
     * Get modified_at
     *
     * @return \DateTime 
     */
    public function getModifiedAt()
    {
        return $this->modified_at;
    }

    /**
     * Set avatar
     *
     * @param string $avatar
     * @return ContactRelative
     */
    public function setAvatar($avatar)
    {
        $this->avatar = $avatar;

        return $this;
    }

    /**
     * Get avatar
     *
     * @return string 
     */
    public function getAvatar()
    {
        return $this->avatar;
    }

    /**
     * Set gender
     *
     * @param string $gender
     * @return ContactRelative
     */
    public function setGender($gender)
    {
        $this->gender = $gender;

        return $this;
    }

    /**
     * Get gender
     *
     * @return string 
     */
    public function getGender()
    {
        return $this->gender;
    }

    /**
     * Set contact
     *
     * @param \********\***Bundle\Entity\Contact $contact
     * @return ContactRelative
     */
    public function setContact(\********\***Bundle\Entity\Contact $contact = null)
    {
        $this->contact = $contact;

        return $this;
    }

    /**
     * Get contact
     *
     * @return \********\***Bundle\Entity\Contact 
     */
    public function getContact()
    {
        return $this->contact;
    }

    /**
     * Set relation
     *
     * @param string $relation
     * @return ContactRelative
     */
    public function setRelation($relation)
    {
        $this->relation = $relation;

        return $this;
    }

    /**
     * Get relation
     *
     * @return string 
     */
    public function getRelation()
    {
        return $this->relation;
    }

    /**
     * Add annibirths
     *
     * @param \********\***Bundle\Entity\AnniBirth $annibirths
     * @return ContactRelative
     */
    public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
    {
        $this->annibirths[] = $annibirths;

        return $this;
    }

    /**
     * Remove annibirths
     *
     * @param \********\***Bundle\Entity\AnniBirth $annibirths
     */
    public function removeAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
    {
        $this->annibirths->removeElement($annibirths);
    }

    /**
     * Get annibirths
     *
     * @return \Doctrine\Common\Collections\Collection 
     */
    public function getAnnibirths()
    {
        return $this->annibirths;
    }
}

ContactRelativeType.php

<?php

namespace ********\***Bundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class ContactRelativeType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('firstname')
            ->add('created_at', 'datetime', array(
            'input' => 'datetime',
            'widget' => 'single_text',
            ))
            ->add('modified_at', 'datetime', array(
            'input' => 'datetime',
            'widget' => 'single_text',
            ))
            ->add('avatar')
            //->add('contact')
            ->add('relation', 'choice', array(
                'choices'     => array(
                    'Mother'  => 'Mother', 
                    'Father'  => 'Father',
                    'Sister'  => 'Sister',
                    'Brother' => 'Brother',
                    'Child'   => 'Child',
                    ),
                'required'  => true,
                'empty_value' => 'Which is the relation?',
            ))
            ->add('gender', 'choice', array(
                'choices'   => array(
                    'M' => 'Male', 
                    'F' => 'Female'
                    ),
                'required'  => true,
                'empty_value' => 'Choose the gender',
            ))

            ->add('annibirths', 'collection', array(
                 'label'        => 'AnniBirths',
                 'type'         => new AnniBirthType(),
                 'allow_add' => true,
                 'by_reference' => false,
            ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '********\***Bundle\Entity\ContactRelative'
        ));
    }

    public function getName()
    {
        return '********_***Bundle_contactrelativetype';
    }
}

ContactController.php (新/创建类)

/**
     * Creates a new ContactRelative entity.
     *
     */
    public function createAction(Request $request)
    {
        $contactRelative  = new ContactRelative();
        //$ab1 = new AnniBirth();
        //$contactRelative->annibirth = $ab1;

        $form = $this->createForm(new ContactRelativeType(), $contactRelative);

        ini_set('display_errors', 1);

        if ('POST' === $request->getMethod()) {
            $form->bind($request);

            if ($form->isValid()) {
                foreach ( $contactRelative->getAnnibirths() as $anni ) 
                { 
                    $anni-> setContactRelative($contactRelative); 
                }
                $rela = $form->getData();

                $em = $this->getDoctrine()->getManager();
                $em->persist($contactRelative);
                $em->flush();

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

        return $this->render('***********Bundle:ContactRelative:new.html.twig', array(
            'entity' => $contactRelative,
            'form'   => $form->createView(),
        ));
    }

    /**
     * Displays a form to create a new ContactRelative entity.
     *
     */
    public function newAction()
    {
        $entity = new ContactRelative();

        $form = $this->createForm(new ContactRelativeType(), $entity);

        return $this->render('***********Bundle:ContactRelative:new.html.twig', array(
            'entity' => $entity,
            'form'   => $form->createView(),
        ));
    }

ContactRelative new.twig.html

{% extends '***********Bundle::layout.html.twig' %}

{% block body -%}
    <h1>ContactRelative creation</h1>

    <form action="{{ path('contactrelative_create') }}" method="post" {{ form_enctype(form) }}>

        <!---------------- firstname ------------------>
        <fieldset class="control-group">
            <label class="control-label" for="inputInfo">Firstname</label>
            <div class="controls">
                {{ form_widget(form.firstname,{'id' : 'inputFirstname', 'attr': { 'class': 'span5' }}) }}
                {% if form_errors(form.firstname) %}
                <div class="alert alert-error fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <p>{{ form_errors(form.firstname) }}</p>
                </div>
                {% endif %}
            </div>
        </fieldset>

        <!---------------- avatar ------------------>
        <fieldset class="control-group">
            <label class="control-label" for="inputInfo">Avatar</label>
            <div class="controls">
                {{ form_widget(form.avatar,{'id' : 'inputAvatar', 'attr': { 'class': 'span5' }}) }}
                {% if form_errors(form.avatar) %}
                <div class="alert alert-error fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <p>{{ form_errors(form.avatar) }}</p>
                </div>
                {% endif %}
            </div>
        </fieldset>

        <!---------------- gender ------------------>
        <fieldset class="control-group">
            <label class="control-label" for="inputInfo">Gender</label>
            <div class="controls">
                {{ form_widget(form.gender,{'id' : 'inputGender', 'attr': { 'class': 'span5' }}) }}
                {% if form_errors(form.gender) %}
                <div class="alert alert-error fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <p>{{ form_errors(form.gender) }}</p>
                </div>
                {% endif %}
            </div>
        </fieldset>





        <!---------------- relation ------------------>
        <fieldset class="control-group">
            <label class="control-label" for="inputInfo">Relation</label>
            <div class="controls">

                {{ form_widget(form.relation,{'id' : 'inputRelation', 'attr': { 'class': 'span5' }}) }}
                {% if form_errors(form.relation) %}
                <div class="alert alert-error fade in">
                    <button type="button" class="close" data-dismiss="alert">&times;</button>
                    <p>{{ form_errors(form.relation) }}</p>
                </div>
                {% endif %}
            </div>
        </fieldset>


        {{ form_widget(form.annibirths.vars.prototype)}}


        <hr />
        {{ form_row(form._token) }}
        <p>
            <button type="submit">Create</button>
        </p>
    </form>

        <ul class="record_actions">
    <li>
        <a href="{{ path('contactrelative') }}">
            Back to the list
        </a>
    </li>
</ul>
{% endblock %}

AnniBirthType.php     

namespace *******\***Bundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Doctrine\ORM\EntityRepository;

class AnniBirthType extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('celebrationDate','date', array(
                            'input'  => 'datetime',
                            'widget' => 'choice',
                            'years'  => range(1900,2013),
                            ))
            ->add('repeating')
            ->add('type', 'choice', array(
                'choices'   => array(
                    'Anniversary' => 'Anniversary', 
                    'Birthday' => 'Birthday',
                    'Other' => 'Other',
                    ),
                'required'  => true,
                'empty_value' => 'What is the occasion?',
            ))
            ->add('contact','entity', array(
                                        'class' => '*******\***Bundle\Entity\Contact',
                                        'property' => 'lastname',
                                        'query_builder' => function(EntityRepository $er)
                                            {
                                                return $er->createQueryBuilder('u')
                                                          ->orderby('u.lastname','ASC');
                                            },
                                        ))
        ;
    }

    public function setDefaultOptions(OptionsResolverInterface $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => '*******\***Bundle\Entity\AnniBirth'
        ));
    }

    public function getName()
    {
        return '*******_***Bundle_annibirthtype';
    }
}

问题:

  • 联系人在MySQL数据库中成功保存所有数据。同样的 ContactRelative和AnniBirth但保存最后两个数据时 (ContactRelative和AnniBirth), contact_id contactrelative_id 不会更新或保存在表格中。我知道我在收藏和持久数据方面犯了一个错误。我试过了 通过使用显示contact_id \Doctrine\Common\Util\Debug::dump($contactRelative->getAnniBirths()->contact); exit();但我得到的只是NULL。

  • 我希望能够显示AnniBirth表单4次,而且我会这样做 不知道如何实现这一点。这是我几天后发现的唯一一件事 搜索,如果我想显示没有javascript的嵌入表单, 我必须直接使用“原型”:{{ form_widget(form.annibirths.vars.prototype)}}但只有一个实例 显示表格。

3 个答案:

答案 0 :(得分:1)

部分问题是:

    public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirths)
    {
        $this->annibirths[] = $annibirths;

        return $this;
    }

你会注意到,虽然这表明接触/相对于annibirth,但是annibirth并没有指向。这也许是为什么另一张海报可能会推荐做一个循环。但它是一个强大而简单的解决方案:

    public function addAnnibirth(\********\***Bundle\Entity\AnniBirth $annibirth)
    {
        $this->annibirths[] = $annibirth;
        $annibirth->setContact($this);  // or setRelative for the Relative entity.
        return $this;
    }

这可能会解决您持续存在的问题。太多代码可以确定。但绝对需要以上的改变。如果你仔细观察大多数例子,你会看到同样的线条。容易被忽视。

一旦设置正确,就不需要在持久化之前循环。

答案 1 :(得分:0)

$contactRelative->annibirth = $ab1;这不应该是可能的,因为你将annibirth保存在ContactRelative中。这应该会抛出一个错误(如果您之前更改了错误)。你应该做的是$contactRelative->setAnnibirth($ab1);。但这是为了使php对象附加,数据库中的附加实际上是在Annibirth表中,它包含一个contactRelative_id所以你必须更新这个对象:$ab1->setContactRelative($contactRelative);

答案 2 :(得分:0)

找到解决方案:

在控制器中添加:

foreach ( $contactRelative->getAnnibirths() as $anni ) 
                    { 
                        $anni-> setContactRelative($contactRelative); 
                        $anni-> setContact($contactRelative->getContact());
                    }

并在树枝模板中:

{% for occasion in form.annibirths %}
     <div>
            {{ form_row(occasion.type) }}
            {{ form_row(occasion.repeating) }}
            {{ form_row(occasion.celebrationDate) }}
    </div>
{% endfor %}