-1

I want to check whether my syntax is correct for cascading removal of the following entities, I want when I delete a Subject or a User it cascades and deletes the Teacher entity related to them.

Teachers Entity

class Teacher{

    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     **/
    protected $id;

    /** @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="Subject")
     * @ORM\JoinColumn(name="subjectId", referencedColumnName="id",  onDelete="CASCADE")
     *  */
    protected $subjectId;

    /** @ORM\Column(type="integer")
     * @ORM\ManyToOne(targetEntity="User")
     * @ORM\JoinColumn(name="userId", referencedColumnName="id",  onDelete="CASCADE")
     *  */
    protected $userId;

}

Subject Entity

/** @ORM\Entity */
class Subject{
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** @ORM\Column(type="integer") */
    protected $sectionId;

    /** @ORM\Column(type="string") */
    protected $subjectName;

Users Entity

/** @ORM\Entity */
class User {
    /**
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer")
     */
    protected $id;

    /** @ORM\Column(type="string") */
    protected $username;

    /** @ORM\Column(type="string") */
    protected $password;

    /** @ORM\Column(type="string") */
    protected $first_name;

    /** @ORM\Column(type="string") */
    protected $middle_name;

    /** @ORM\Column(type="string") */
    protected $last_name;

    /** @ORM\Column(type="string") */
    protected $sex;

    /** @ORM\Column(type="date") */
    protected $dob;

    /** @ORM\Column(type="boolean") */
    protected $is_active;

    /** @ORM\Column(type="boolean") */
    protected $is_admin;

    /** @ORM\Column(type="string") */
    protected $email;

    /** @ORM\Column(type="string") */
    protected $address;

    /** @ORM\Column(type="string") */
    protected $bloodTypeId;

    /** @ORM\Column(type="string") */
    protected $photo;

    /** @ORM\Column(type="integer") */
    protected $userTypeId;

Because it doesn't seem to work when I delete a User or a Subject, I am expecting the teacher related to their fields be deleted as Well.

f0unix
  • 301
  • 1
  • 3
  • 10
  • Checkout [this answer](http://stackoverflow.com/questions/6328535/on-delete-cascade-with-doctrine2). You are using the database level cascade within the `@joinColumn` rather than the `cascade={"remove"}` for ORM level deletes. If you DO want DB level cascading, then you would need to also run the create/update schema cli tool for them to be added to the DB. – AlexP Nov 07 '15 at 13:19

2 Answers2

0

If you search on stackoverlflow your answer is already here.

There are two kinds of cascades in Doctrine:

1) ORM level - uses cascade={"remove"} in the association - this is a calculation that is done in the UnitOfWork and does not affect the database structure. When you remove an object, the UnitOfWork will iterate over all objects in the association and remove them.

2) Database level - uses onDelete="CASCADE" on the association's joinColumn - this will add On Delete Cascade to the foreign key column in the database:


Your onDelete="CASCADE" on subject and user inside teacher will have the following result: When you delete a teacher the operation will cascade to both the subject and the user (so they are deleted too). I think this is not what you want...

Community
  • 1
  • 1
Wilt
  • 37,062
  • 11
  • 138
  • 192
0

No, I want when I delete a User it deletes the teacher, and when I delete a Subject is deletes the teacher as well

f0unix
  • 301
  • 1
  • 3
  • 10