【问题标题】:Strange cascade behaviour Doctrine2奇怪的级联行为学说2
【发布时间】:2011-05-25 16:02:33
【问题描述】:

我有两个实体

TB\Entity\UserProfile
/** 
 * @OneToMany(targetEntity="TB\Entity\ShopVideo", mappedBy="shop",
 * cascade={"persist", "remove"}
 * )
 */
private $video;    

和 TB\Entity\ShopVideo

/**
 * @var UserProfile
 *
 * @ManyToOne(targetEntity="TB\Entity\UserProfile")
 * @JoinColumns({
 *   @JoinColumn(name="shop", referencedColumnName="id")
 * })
 */
private $shop;

如果我这样创建 UserProfile 实例和 ShopVideo 实例

    $profile = new TB\Entity\UserProfile();
    $model = new TB\Entity\ShopVideo();

    $profile->getShopVideo()->add($model);
    $this->_em->persist($profile);
    $this->_em->flush();

我希望用“UserProfile”的 id 填充“商店”列(以及其他有效的模型)...但是我收到错误(由于外键限制)

PDOException:SQLSTATE[23000]:违反完整性约束:1048 列“shop”不能为空

/Users/ABCD/work/TB/TB/library/Doctrine/DBAL/Statement.php:131
/Users/ABCD/work/TB/TB/library/Doctrine/ORM/Persisters/BasicEntityPersister.php:226
/Users/ABCD/work/TB/TB/library/Doctrine/ORM/UnitOfWork.php:698
/Users/ABCD/work/TB/TB/library/Doctrine/ORM/UnitOfWork.php:280
/Users/ABCD/work/TB/TB/library/Doctrine/ORM/EntityManager.php:328

我只是不明白为什么 ShopVideo 没有级联操作并使用 USerProfile 的 id 设置商店列?任何帮助将不胜感激!

【问题讨论】:

    标签: doctrine-orm


    【解决方案1】:

    在您的 @ManyToOne 上尝试添加 inversedBy="video"。这可能会也可能不会解决这个问题,但在双向关联的情况下它是必需的属性。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-02-04
      • 2011-10-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-08-28
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多