On delete cascade with doctrine2
There are two kinds of cascades in Doctrine:
ORM level - uses
cascade={"remove"}
in the association - this is a calculation that is done in theUnitOfWork
and does not affect the database structure. When you remove an object, theUnitOfWork
will iterate over all objects in the association and remove them.Database level - uses
onDelete="CASCADE"
on the association's joinColumn - this will add On Delete Cascade to the foreign key column in the database:@ORM\JoinColumn(name="father_id", referencedColumnName="id", onDelete="CASCADE")
I also want to point out that the way you have your cascade={"remove"}
right now, if you delete a Child object, this cascade will remove the Parent object. Clearly not what you want.
Doctrine 2 cascade={''remove } doesn't seem to be working
Your relationship definition seems to be fine. What is the way the customer is deleted? I mean Doctrine doesn't set "ON DELETE CASCADE" directly in database. So, if you remove customer entity in other way than "doctrine's" one, comments won't be deleted.
You may tell doctrine to set this directly in database, by adding in annotation:
@ORM\JoinColumn(name="icustomer_id", referencedColumnName="icustomer_id", onDelete="CASCADE")
But if you're trying remove the entity in right-doctrine way ant this still doesn't work, try add "orphanRemoval" to true, it should help:
// Customer.php
/**
* @var string $addresses
* @ORM\OneToMany(targetEntity="MP\User\RegistrationBundle\Entity\Address", mappedBy="customer", cascade={"remove"}, orphanRemoval=true)
*/
protected $addresses;
Doctrine 2 : ManyToOne cascade remove causes the referenced entity to be deleted
Feed Model :
/**
* Class Feed represents a single feed as supplier by a supplier
* @package App\Model
* @Entity @Table(name="feeds")
*/
class Feed
{
/**
* @var int
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @var Feed
* @OneToMany(targetEntity="App\Model\Product", mappedBy="feed", orphanRemoval=true, cascade={"remove"})
*
*/
protected $products;
}
Product Model :
/**
* Class Product is the base for either supplied and current products
* @package App\Model
*/
class Product
{
/**
* @var int
* @Id @Column(type="integer") @GeneratedValue
*/
protected $id;
/**
* @var Feed
* @ManyToOne(targetEntity="App\Model\Feed", inversedBy="products")
* @JoinColumn(name="id_feed", referencedColumnName="id")
*/
protected $feed;
}
Now, if you delete a Feed object, Product objects linked to this Feed will be deleted too.
This is bidirectional relation.
More info :
cascade={"remove"}
- Entity on the inverse side will be deleted while the owning side (Feed) is deleted but only if the entity (Product) is not owned by another than Feed.
orphanRemoval="true"
- Same as above but ORM ignores if entity (Product) is owned by another Entity
Doctrine2 cascade remove with multiple parents
Looks like you forgot to add ON DELETE CASCADE
to the foreign key constraint. Try changing the following association in class StudentTestItem
:
/**
* @var StudentTest
* @ORM\ManyToOne(targetEntity="StudentTest", inversedBy="studentTestItems")
*/
protected $studentTest;
To this:
/**
* @var StudentTest
* @ORM\ManyToOne(targetEntity="StudentTest", inversedBy="studentTestItems")
* @ORM\JoinColumn(name="student_test_id", referencedColumnName="id", onDelete="CASCADE")
*/
protected $studentTest;
Doctrine2 cascade delete doesn't work with Abstract Class
I solved this problem by setting the ON DELETE
action to database level:
/**
* @ORM\Entity
* @ORM\Table
*/
class Entity
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
private $id;
}
/**
* @ORM\MappedSuperclass
*/
abstract class MappedSuperclass
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(name="id", type="integer")
*/
protected $id;
/**
* @ORM\ManyToOne(targetEntity="Entity")
* @ORM\JoinColumn(name="entity_id", referencedColumnName="id", nullable=false, onDelete="CASCADE")
*/
protected $entity;
}
Sources: [1] [2]
Many to many relation with ON DELETE CASCADE with Symfony and Doctrine
I've never used the YAML
format to define my entities and relations so I don't know if it is the same, but with annotations the onDelete
option belongs to the @ORM\JoinColumn
annotation:
/**
* @var \AppBundle\Entity\Actor $actor
*
* @ORM\ManyToOne(targetEntity="Actor", inversedBy="fields")
* @ORM\JoinColumn(name="actor_id", referencedColumnName="id", nullable=false, onDelete="cascade")
*/
protected $actor = null;
PS: After a quick search I've found your answer: https://stackoverflow.com/a/8330495/5192753
Related Topics
How to Get the Query Executed in Laravel 5? Db::Getquerylog() Returning Empty Array
Difference Between If () { } and If (): Endif;
Best Practice on PHP Singleton Classes
Get Everything Between ≪Tag≫ and ≪/Tag≫ With PHP
How to Insert HTML to PHP Domnode
How to Avoid Echoing Character 65279 in PHP
"Unknown Modifier 'G' In..." When Using Preg_Match in PHP
Difference Between JavaScript and PHP
PHP Script to Loop Through All of the Files in a Directory
Is It Safe to Trust $_Server['Remote_Addr']
Why Won't My PHP App Send a 404 Error
Codeigniter Htaccess and Url Rewrite Issues
How to Loop Through a MySQL Result Set More Than Once Using the MySQL_* Functions
How to Check File Types of Uploaded Files in PHP
Correct PHP Headers For Pdf File Download
PHP: Warning: Sort() Expects Parameter 1 to Be Array, Resource Given