如何解决奇怪的级联行为Doctrine2
| 我有两个实体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();
我希望\'shop \'列中填充\'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设置shop列?任何帮助,将不胜感激!
解决方法
在您的@ManyToOne上,尝试添加inversedBy = \“ video \”。这可能会或可能不会解决此问题,但是在双向关联的情况下,这是必需的属性。
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。