两个实体之间的Symfony2关系

时间:2015-06-18 07:59:31

标签: php mysql entity-framework symfony

我想在两个实体之间建立关系,但由于某种原因,我在其中一个值上得到NULL ...

所以在我的皮肤实体中,我有一个带有关系的email_registration_id字段:

    /**
     * @ORM\ManyToOne(targetEntity="Project\UserBundle\Entity\Email")
     * @ORM\JoinColumn(name="email_registration_id", referencedColumnName="id")
     */
    protected $email_registration_id;

 /**
     * Set email_registration_id
     *
     * @param \Project\UserBundle\Entity\Email $email_registration_id
     * @return Skin
     */
    public function setEmailRegistrationId(\Project\UserBundle\Entity\Email $email_registration_id = null)
    {
        $this->email_registration_id = $email_registration_id;

        return $this;
    }

    /**
     * Get email_registration_id
     *
     * @return \Project\UserBundle\Entity\Email
     */
    public function getEmailRegistrationId()
    {
        return $this->email_registration_id;
    }

这是电子邮件实体:

    /**
     * Project\UserBundle\Entity\Email
     *
     * @ORM\Entity
     * @ORM\Table(name="email")
     */
    class Email {

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

        /**
         * @var string $title
         *
         * @ORM\Column(name="title", type="string")
         */
        protected $title;

        /**
         * @var string $registration_content
         *
         * @ORM\Column(name="registration_content", type="string")
         */
        protected $registration_content;

        /**
         * @var string $confirmation_apuestas
         *
         * @ORM\Column(name="confirmation_apuestas", type="string")
         */
        protected $confirmation_apuestas;

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

        public function setId() {
            $this->id = null;
        }

        /**
         * Set title
         *
         * @param string $title
         * @return Email
         */
        public function setTitle($title)
        {
            $this->title = $title;

            return $this;
        }

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

        /**
         * Set registration_content
         *
         * @param string $registration_content
         * @return Email
         */
        public function setRegistrationContent($registration_content)
        {
            $this->registration_content = $registration_content;

            return $this;
        }

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

        /**
         * Set $confirmation_apuestas
         *
         * @param string $confirmation_apuestas
         * @return Email
         */
        public function setConfirmationApuestas($confirmation_apuestas)
        {
            $this->confirmation_apuestas = $confirmation_apuestas;

            return $this;
        }

        /**


 * Get $confirmation_apuestas
     *
     *

 @return string
     */
    public function getConfirmationApuestas()
    {
        return $this->confirmation_apuestas;
    }
} 

现在在我的cms中我创建了一个这样的新电子邮件:

/**
     * @Route("/new", name="cms_email_new")
     * @Method({"GET"})
     * @Template()
     */
    public function newAction() {
        $item = new Email();
        $form = $this->createForm(new EmailType($this->container->getParameter("langs")), $item);

        return array('form' => $form->createView(), 'item' => $item);
    }

表格:

public function buildForm(FormBuilderInterface $builder, array $option) {
    $builder->add('title', 'text', array('label' => 'cms.Title'));

    $builder->add('registration_content', 'textarea', array('label' => 'cms.registration.content'));

    $builder->add('confirmation_apuestas', 'textarea', array('label' => 'cms.confirmation.apuestas'));
}

public function getDefaultOptions(array $options) {
    return array(
        'data_class' => 'Project\UserBundle\Entity\Email',
    );
}

这是我将电子邮件持久保存到数据库的方式:

/**
     * @Route("/save", name="cms_email_save")
     * @Template("ProjectUserBundle:EmailAdmin:new.html.twig")
     * @Method({"POST"})
     */
    public function saveAction(Request $request) {

        $item = new Email();
        $type = new EmailType($this->container->getParameter("langs"));
        $form = $this->createForm($type, $item);
        $form->bind($request);
        $em = $this->getEntityManager();
        if ($form->isValid()) {

            $this->upload($form, $item);

            $em->persist($item);
            $em->flush();
            return $this->redirect($this->generateUrl('cms_skin_email_list', array('skin_id' => $item->getId())));
        }

        return array('form' => $form->createView(), 'item' => $item);
    }

问题是它创建了一个电子邮件,但在我的皮肤实体中,email_registration_id只是NULL而不是电子邮件ID ...也许我错过了什么?

更新

所以这就是我根据我得到的回答制作的:

类电子邮件{

/**
 * @ORM\OneToMany(targetEntity="Project\SkinBundle\Entity\Skin", mappedBy="email_registration_id")
 */
protected $skin;

/**
     * Set skin
     *
     * @param string $skin
     * @return Email
     */
    public function setSkin(\Project\SkinBundle\Entity\Skin $skin = null)
    {
        $this->skin = $skin;

        return $this;
    }

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

形式:

public function buildForm(FormBuilderInterface $builder, array $option) {
        $builder->add('title', 'text', array('label' => 'cms.Title'));

        $builder->add('registration_content', 'textarea', array('label' => 'cms.registration.content'));

        $builder->add('confirmation_apuestas', 'textarea', array('label' => 'cms.confirmation.apuestas'));

        $builder->add('skin', 'entity', array(
            'class' => 'ProjectSkinBundle:Skin',
            'property' => 'id',
        ));
    }

但是,当我尝试刷新数据库时,我收到此错误:

ContextErrorException: Catchable Fatal Error: Argument 1 passed to Doctrine\Common\Collections\ArrayCollection::__construct() must be of the type array, object given, called in C:\wamp\www\Company\front\vendor\doctrine\orm\lib\Doctrine\ORM\UnitOfWork.php on line 528 and defined in C:\wamp\www\Company\front\vendor\doctrine\collections\lib\Doctrine\Common\Collections\ArrayCollection.php line 48

1 个答案:

答案 0 :(得分:0)

新电子邮件的创建与皮肤对象无关,这是正常的。怎么可能呢?该关系是单向的,只有在您创建皮肤并且给它发送电子邮件时才有效。要创建电子邮件并为其提供外观,您必须使关系成为双向关系,并将其添加到您的电子邮件表单中。

为此,我要执行以下操作:

/*
* Class Project\UserBundle\Entity\Email
*/
class Email {
    ...
/**
* @ORM\OneToMany(targetEntity="Project\SkinBundle\Entity\Skin", mappedBy="email_registration_id")
*/
protected $skin;

并以您的形式:

public function buildForm(FormBuilderInterface $builder, array $option) {
$builder->add('title', 'text', array('label' => 'cms.Title'))
        ->add('registration_content', 'textarea', array('label' => 'cms.registration.content'))
        ->add('confirmation_apuestas', 'textarea', array('label' => 'cms.confirmation.apuestas'))
        ->add('skin')
        ;
}

和PS:你不应该使用bind()方法来处理请求,不推荐使用它。而是使用:

$form->handleRequest($request);

更新1:不要忘记更新你的ManyToOne注释添加inversedBy部分:

@ORM\ManyToOne(targetEntity="Project\UserBundle\Entity\Email", inversedBy="skin")

当然,还要更新您的数据库。