简体   繁体   English

与教义2的关联

[英]Associations with Doctrine 2

I'm trying make an entity with doctrine that has three associations with other entities 我正在尝试创建一个具有学说的实体,该实体与其他实体具有三个关联

So an Item is associated with: 因此,某项与以下内容相关联:

  • Must be associated with One Rssfeed, which it originates from 必须与它来自的一个Rssfeed关联
  • Can be associated with One or more Locations 可以与一个或多个位置相关联
  • Can be associated with one or more Tags 可以与一个或多个标签关联

Here is my attempt: 这是我的尝试:

class Item{

    /**
     * @ManyToOne(targetEntity="Rssfeed")
     */
    protected $rssfeed;

    /**
     *
     * @ManyToMany(targetEntity="Location")
     */
    protected $locations;

    /**
     *
     * @ManyToMany(targetEntity="Tag")
     */
    protected $tags;
}

Now 现在

  • If an Rssfeed is removed, associated items must be removed too 如果删除了Rssfeed,则也必须删除关联的项目
  • If an item is removed, Rssfeeds, and Locations, and Tags attached to that item should be detached 如果删除了某个项目,则应分离该项目的Rssfeed和“位置”以及“标签”
  • If a Location, or Tag is removed, the associated items should just be detached, because they are optional associations. 如果删除了位置或标记,则应仅分离关联的项,因为它们是可选的关联。

How should I change my code to accomplish that? 我应该如何更改我的代码来实现这一目标?

U have to add @JoinColumn with onDelete="CASCADE" for $rssfeed and onDelete="SET NULL" for foreign keys in Location and Tag entities. 您必须为$ rssfeed添加@JoinColumn并为其onDelete =“ CASCADE”以及为Location和Tag实体中的外键添加onDelete =“ SET NULL”。

    /**
     * @ManyToOne(targetEntity="Rssfeed")
     * @JoinColumn(name="rssfeed_id", referencedColumnName="id", onDelete="CASCADE")
     */
    protected $rssfeed;

For each association in your Item entity, add onDelete="SET NULL" to the @JoinColumn annotation. 对于Item实体中的每个关联,将onDelete =“ SET NULL”添加到@JoinColumn批注。 Inside your location and tag entities, find the JoinColumn annotations and add onDelete="SET NULL" for the association with "Item". 在您的位置和标记实体中,找到JoinColumn批注,并为与“ Item”的关联添加onDelete =“ SET NULL”。 Under the RssFeed entity, find each @JoinColumn annotation and add onDelete="SET NULL". 在RssFeed实体下,找到每个@JoinColumn注解并添加onDelete =“ SET NULL”。 Note that you can also use Doctrine cascade operations to achieve this (ie cascade={"remove"}, etc; however, it will likely be significantly slower as the operating is not performed at the RDBMS level. 请注意,您也可以使用Doctrine级联操作来实现此目的(例如,级联= {“删除”}等;但是,由于操作不是在RDBMS级别执行的,因此它的运行速度可能会大大降低。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM