Symfony2创造和坚持实体关系 [英] Symfony2 creating and persisting entity relationships

查看:118
本文介绍了Symfony2创造和坚持实体关系的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个实体皮肤电子邮件。我想要电子邮件成为 Skin 实体的一部分,但是我无法使用控制台自动更新模式现在,这可能是为什么我无法使关系发挥作用。

I have two entities Skin and Email. I want Email to be a part of the Skin entity, however I can't use the console to update schema automatically right now, and that is probably why I can't get the relationship to work.

所以我想将所有的电子邮件存储在电子邮件实体中,我想将电子邮件分配给所需的皮肤。所以关系应该是我的OneToOne。

So I want to store all the Emails in the Email entity, and I want to assign an email to a desired Skin. So the relationship should me OneToOne.

我不是MySQL关系中的专家,所以这是我自己做的:

I am not an expert in MySQL relationships so this is what I made by myself:

在我的主要 Skin 类中,我创建了一个我要存储电子邮件ID的字段:

In my main Skin class I created a field in which I want to store the emails id:

/**
 *
 * @ORM\OneToOne(targetEntity="MediaparkLt\UserBundle\Entity\Email", inversedBy="skin")
 * @ORM\JoinColumn(name="email_id", referencedColumnName="id")
 */
protected $email_id;

在电子邮件类中,我创建了我要显示皮肤的皮肤字段Id: p>

In the Email class I created the skin field in which I want to show the skins Id:

/**
 *
 * @ORM\OneToOne(targetEntity="MediaparkLt\SkinBundle\Entity\Skin", mappedBy="email_id")
 * @ORM\JoinColumn(name="skin", referencedColumnName="id")
 */
protected $skin;

现在在我的CMS中,我创建了这样的新邮件:

Now in my CMS I create the new email like this:

public function saveAction(Request $request) {
    $item = new Email();
    $type = new EmailType($this->container->getParameter("langs"));
    $form = $this->createForm($type, $item);
    $form->handleRequest($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);
}

这是表单:

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'));
}

现在在mysql中我创建了这样的关系:

Now in mysql I create the relationship like this:

ALTER TABLE `skin` ADD CONSTRAINT `emailId` FOREIGN KEY (`email_id`) REFERENCES `lottery`.`email`(`id`) ON DELETE RESTRICT ON UPDATE RESTRICT;

现在,当我创建一个新的电子邮件时,我仍然在两个功能上都为NULL。

Now when I create a new email I still get NULL on both entitites.

推荐答案

当您创建具有一对一关系的两个实体时,两个实体都需要被显式地持久化,或者通过在的关系。您还需要明确设置关系的双方。

When you create two entities with a one-to-one relationship, both entities need to be persisted either explicitly or by using cascade persist on one side of the relationship. You also need to explicitly set both sides of the relationship.

Doctrine - 使用协会 - 传递持久性/级联操作

状态:


即使您持有包含新评论的新用户,此代码
将失败,如果您删除调用
EntityManager#persist($ myFirstComment)。 Doctrine 2不会将所有嵌套实体的持久性操作级别
也是新的。

Even if you persist a new User that contains our new Comment this code would fail if you removed the call to EntityManager#persist($myFirstComment). Doctrine 2 does not cascade the persist operation to all nested entities that are new as well.

主义 - 使用协会 - 建立协会

州:


在双向关联的情况下,您必须更新
两边的字段

In the case of bi-directional associations you have to update the fields on both sides

没有级联持久,你需要这样的东西:

With no cascade persist you need something like this:

$skin = new Skin();
$email = new Email();
$skin->setEmail($email);
$email->setSkin($skin);
$em->persist($email);
$em->persist($skin);
$em->flush();

在关系的皮肤侧有级联持续,可以省略 $ em - >坚持($皮肤)。注意,如果你级联持续,你通常也会级联删除:

With cascade persist on the Skin side of the relationship you can omit $em->persist($skin). Note if you cascade persist you would usually also cascade remove:

 * @ORM\OneToOne(targetEntity="MediaparkLt\UserBundle\Entity\Email", inversedBy="skin", cascade={"persist", "remove"})

这篇关于Symfony2创造和坚持实体关系的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆