Doctrine only checks the owning side of an association when computing associations, therefore it is ignoring the collection (inverse side) in this case.
To be more precise, from a logical perspective both sides should be filled with details. It doesn't make sense to have only one of the directions of the association filled with data in a bidirectional association. See following example to have an idea of how the data should be always set on both sides: https://gist.github.com/Ocramius/3121916 You may also want to read http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/unitofwork-associations.html#association-updates-owning-side-and-inverse-sideand http://www.doctrine-project.org/jira/browse/DDC-2585 Cheers, Marco Pivetta http://twitter.com/Ocramius http://ocramius.github.com/ On 11 May 2014 06:57, Moon K <[email protected]> wrote: > Hello, > > I'm trying to make a has-many relations working (one to many in > doctrine2). > > I have two tables -- members and subscriptions. A member has many > subscriptions. > > My member entity looks like this > > > /** > * @Entity > * @Table(name="members") > */ > class Member { > > /** > * @Id @Column(type="integer") > * @GeneratedValue(strategy="AUTO") > */ > private $id; > > /** > * @OneToMany(targetEntity="Subscription", mappedBy="member", > cascade={"persist"}) > */ > private $subscriptions; > > > public function __construct() > { > > $this->subscriptions = new \Doctrine\Common\Collections\ArrayCollection(); > } > > public function getSubscriptions() > { > return $this->subscriptions; > } > > } > > and this is my Subscription entity > > <?php > > /** > * @Entity > * @Table(name="subscriptions") > */ > class Subscription { > > /** > * @Id @Column(type="integer") > * @GeneratedValue(strategy="AUTO") > */ > private $id; > > /** > * @Column(type="integer") > */ > private $member_id; > > /** > * @ManyToOne(targetEntity="Member", inversedBy="subscriptions") > */ > private $member; > > public function setMember($member) > { > $this->member = $member; > } > > } > > > I actually have a working example. > > $member = $em->find('Member', 2); > $subscription = new Subscription(); > > $subscription->setMember($member); > > $em->persist($subscription); > $em->flush(); > > > My question is...how do I make the following code working? > > $member = $em->find('Member', 2); > $subscription = new Subscription(); > > $member->getSubscriptions()->add($subscription); > $em->flush(); > > > When I run this, subscription table's member_id is null. > > Is there a way to make it work? > > -- > You received this message because you are subscribed to the Google Groups > "doctrine-user" group. > To unsubscribe from this group and stop receiving emails from it, send an > email to [email protected]. > To post to this group, send email to [email protected]. > Visit this group at http://groups.google.com/group/doctrine-user. > For more options, visit https://groups.google.com/d/optout. > -- You received this message because you are subscribed to the Google Groups "doctrine-user" group. To unsubscribe from this group and stop receiving emails from it, send an email to [email protected]. To post to this group, send email to [email protected]. Visit this group at http://groups.google.com/group/doctrine-user. For more options, visit https://groups.google.com/d/optout.
