开发者

Strange cascade behaviour Doctrine2

开发者 https://www.devze.com 2023-03-08 03:48 出处:网络
I have two entities TB\\Entity\\UserProfile /** * @OneToMany(targetEntity=\"TB\\Entity\\ShopVideo\", mappedBy=\"shop\",

I have two entities

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

and TB\Entity\ShopVideo

开发者_运维知识库
/**
 * @var UserProfile
 *
 * @ManyToOne(targetEntity="TB\Entity\UserProfile")
 * @JoinColumns({
 *   @JoinColumn(name="shop", referencedColumnName="id")
 * })
 */
private $shop;

If I create a UserProfile instance and a ShopVideo instance as so

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

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

I would expect the 'shop' column to be populated with the id of the 'UserProfile' (and other models this has worked)... However I get the error (due to foreign key restraints)

PDOException: SQLSTATE[23000]: Integrity constraint violation: 1048 Column 'shop' cannot be null

/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

I just don't understand why the ShopVideo is not cascading the operation and setting the shop column with the id of the USerProfile? any help would be appreciated!


On your @ManyToOne try adding inversedBy="video". This may or may not solve this issue, but it is a required property in the case of bidirectional associations.

0

精彩评论

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