级联不坚持(Doctrine ORM + Symfony 2) [英] Cascaded persist not working (Doctrine ORM + Symfony 2)

查看:166
本文介绍了级联不坚持(Doctrine ORM + Symfony 2)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

几个月前我开始和symfony合作,有一件事情一直困扰着我。当我在Doctrine中有一对多的关系时,我尝试在数据库中插入一些东西。以下是一个例子:



Broker.orm.yml

 code> Acme\DemoBundle\Entity\Broker:
类型:实体
表:代理
repositoryClass:BrokerRepository
id:
id:
类型:整数
生成器:{策略:AUTO}
字段:
名称:
类型:字符串
长度:255
slug:
type:string
length:64
oneToMany:
accountTypes:
targetEntity:Acme\DemoBundle\Entity\AccountType
mappedBy:broker
cascade:[persist]

AccountType.orm.yml

  Acme\DemoBundle\Entity\AccountType:
类型:实体
表:account_types
repositoryClass:AccountTypeRepository
id:
id:
类型:整数
生成器:{策略:AUTO}
字段:
名称:
类型:字符串
长度:255
slug:
类型:string
length:64
manyToOne:
broker:
targetEntity:Acme\DemoBundle\Entity\Broker
inversedBy:accountTypes
joinColumn :
name:broker_id
referencedColumn:id

然后尝试将其保存到数据库中。

  $ accountType = new AccountType(); 
$ accountType-> setName(blabla);
// accountType

$ broker-> addAccountType($ accountType)的附加数据;

$ em-> persist($ broker);
$ em-> flush();

奇怪的是,它的工作原理只有一个小问题。 Broker被更新,AccountType被插入到数据库中,但是 accountType与Broker没有任何关系。换句话说,当我检入数据库时​​, broker_id fields reamains untouched并包含 NULL



如果我添加 $ accountType-> setBroker($ broker)手动,它工作。但是我开始使用Sonata管理软件包,这样做更复杂,我真的不需要一个复杂的管理系统。所以我只想要一个快速的开发,没有这个功能这几乎是不可能的。



无论如何,如果我添加一些对象的集合,应该知道哪个对象是其父对象,对吧?感谢您的提前协助!

解决方案

  class Broker 
{
public function addAccountType($ accountType)
{
$ this-> accountTypes [] = $ accountType;

// ***这是你失踪的***
$ accountType-> setBroker($ this);


I started working with symfony several months ago and there is one thing keeps bothering me all the time. It is when I have a one-to-many relationship in Doctrine and I try to insert something into the database. Here's an example:

Broker.orm.yml

Acme\DemoBundle\Entity\Broker:
    type: entity
    table: brokers
    repositoryClass: BrokerRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
        slug:
            type: string
            length: 64
    oneToMany:
        accountTypes:
            targetEntity: Acme\DemoBundle\Entity\AccountType
            mappedBy: broker
            cascade: ["persist"]

AccountType.orm.yml

Acme\DemoBundle\Entity\AccountType:
    type: entity
    table: account_types
    repositoryClass: AccountTypeRepository
    id:
        id:
            type: integer
            generator: { strategy: AUTO }
    fields:
        name:
            type: string
            length: 255
        slug:
            type: string
            length: 64
    manyToOne:
        broker:
            targetEntity: Acme\DemoBundle\Entity\Broker
            inversedBy: accountTypes
            joinColumn:
                name: broker_id
                referencedColumn: id

Then is try to save it to the database like this.

$accountType = new AccountType();
$accountType->setName("blabla");
// additional data to accountType

$broker->addAccountType($accountType);

$em->persist($broker);
$em->flush();

The strange thing is that it works prefrectly with only one tiny problem. Broker is updated, and AccountType is inserted into the database but the accountType doesn't have any relations with the Broker. In other words, when I check in the database the broker_id fields reamains untouched and contains NULL.

If I add $accountType->setBroker($broker) manually, it works. But I started to use Sonata Admin Bundle where there is much more complicated to do this and I don't really need a complex admin system. So I just want a fast development on it and without this "feature" it's nearly impossible.

And anyways, if I add something to a collection of an Object, it should know which object is its parent, right? :)

Thanks for your help in advance!

解决方案

class Broker
{
    public function addAccountType($accountType)
    {
        $this->accountTypes[] = $accountType;

        // *** This is what you are missing ***
        $accountType->setBroker($this);

这篇关于级联不坚持(Doctrine ORM + Symfony 2)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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