Yasser1984
Yasser1984

Reputation: 2451

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:

Here is my attempt:

class Item{

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

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

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

Now

How should I change my code to accomplish that?

Upvotes: 4

Views: 365

Answers (2)

Lusitanian
Lusitanian

Reputation: 11132

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.

Upvotes: 1

Ziumin
Ziumin

Reputation: 4860

U have to add @JoinColumn with onDelete="CASCADE" for $rssfeed and onDelete="SET NULL" for foreign keys in Location and Tag entities.

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

Upvotes: 1

Related Questions