开发者

Need help with Doctrine 2 Relations (inversedBy)

class Affiliate
{
    /**
     * @var integer $id
     *
     * @Column(name="id", type="integer", length=4)
     * @Id
     * @GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var Entities\AdminUser $admin_user
     *
     * @OneToOne(targetEntity="AdminUser", inversedBy="affiliate")
     */
    private $admin_user;    
}


class AdminUser
{

    /**
    * @var integer $id
    *
    * @Column(name="id", type="integer", length=4)
    * @Id
    * @GeneratedValue(strategy="IDENTITY")
    */
    private $id;

 开发者_JS百科  /**
   * @var Entities\Affiliate $affiliate
   *
   * @OneToOne(targetEntity="Affiliate", mappedBy="admin_user", cascade={"persist", "remove"}, fetch="EAGER")
   */
   private $affiliate;
}



$admin_user4 = new Entities\AdminUser();

$affiliate = new Entities\Affiliate();
$admin_user4->setAffiliate($affiliate);

$this->em->persist($admin_user4);

...

mysql> select * from affiliate;
+----+---------------+
| id | admin_user_id |
+----+---------------+
|  1 |          NULL |
+----+---------------+
1 row in set (0.00 sec)

WHY IS THIS NULL??????


What does setAffiliate() look like?

Since it's the inverse side of the association, it should look something like this:

public function setAffiliate($affiliate){
    $affiliate->admin = $this;
    $this->affiliate = $affiliate;
    return $this;
}

Your cascade annotations are ineffective because you've made Affiliate the owning side.

So another solution, which may or may not make more sense, would be to flip the ownership on the association.


Try to do something like:

$admin_user4 = new Entities\AdminUser();

$affiliate = new Entities\Affiliate();
$admin_user4->setAffiliate($affiliate);

$this->em->persist($admin_user4);

$this->em->flush();
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜