I started working with symfony a few months ago, and I get bored all the time. This is when I have a one-to-many relationship in Doctrine, and I'm trying to insert something into the database. Here is 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 try to save it in a database like this.
$accountType = new AccountType(); $accountType->setName("blabla"); // additional data to accountType $broker->addAccountType($accountType); $em->persist($broker); $em->flush();
It is strange that it works correctly with one tiny problem. The broker is updated, and the AccountType is added to the database, but the type accountType has no relationship with the broker. In other words, when I check the database, the broker_id fields look untouched and contain NULL .
If I add $accountType->setBroker($broker) manually, it will work. But I started using Sonata Admin Bundle, where it is much more difficult to do this, and I do not need a complicated administration system. Therefore, I just want to develop rapidly and without this "function" it is almost impossible.
And anyway, if I add something to the collection of an object, it should know which object is its parent, right? :)
Thanks for your help in advance!