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;
/**
* @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)
为什么这是空的??????
答案 0 :(得分:1)
setAffiliate()是什么样的?
因为它是关联的反面,所以看起来应该是这样的:
public function setAffiliate($affiliate){
$affiliate->admin = $this;
$this->affiliate = $affiliate;
return $this;
}
您的级联注释无效,因为您已将Affiliate设为拥有方。
因此,另一种解决方案,可能会或可能不会更有意义,将颠覆协会的所有权。
答案 1 :(得分:1)
尝试做类似的事情:
$admin_user4 = new Entities\AdminUser();
$affiliate = new Entities\Affiliate();
$admin_user4->setAffiliate($affiliate);
$this->em->persist($admin_user4);
$this->em->flush();