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.

Reply via email to