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
- 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
- If an item is removed, Rssfeeds, and Locations, and Tags attached to that item should be detached
- 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?
For each association in your Item entity, add onDelete=”SET NULL” to the @JoinColumn annotation. Inside your location and tag entities, find the JoinColumn annotations and add onDelete=”SET NULL” for the association with “Item”. Under the RssFeed entity, find each @JoinColumn annotation and add onDelete=”SET NULL”.
Note that you can also use Doctrine cascade operations to achieve this (i.e. cascade={“remove”}, etc; however, it will likely be significantly slower as the operating is not performed at the RDBMS level.