4

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!

7
  • Is your Broker object $broker already known by doctrine (i.e. being fetched from a repository) before you add $accountType? Commented Oct 26, 2013 at 11:02
  • What happens if you persist the newly created $accountType before using the setter to add to broker? $em->persist($accountType); Commented Oct 26, 2013 at 11:12
  • That's what the cascade option is for basically - he doesn't want to explicitly persist the related entity. Commented Oct 26, 2013 at 11:21
  • $broker is an object known by doctrine, fetched from database. Commented Oct 26, 2013 at 16:10
  • And yes, I don't want to persist the newly created accountType before. That's why I want to use cascaded persist. Commented Oct 26, 2013 at 16:11

1 Answer 1

7
class Broker { public function addAccountType($accountType) { $this->accountTypes[] = $accountType; // *** This is what you are missing *** $accountType->setBroker($this); 
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you! I generated Entity classes with doctrine and I didn't think of this small change in the modell classes. (Maybe there is an option in doctrine for this? It seems to be a quite common deal for me.)
One more thing, it works when i test it like the way i wrote in my question. But it doesn't work in Sonata Admin bundle when i add something to the collection. :((

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.