mitchellvanw / laravel-doctrine

NO LONGER MAINTAINED! A Doctrine 2 implementation that melts with Laravel
MIT License
187 stars 74 forks source link

Soft delete cascade #52

Closed matiux closed 9 years ago

matiux commented 10 years ago

Hi guys and sorry for my bad English. I have create 2 entities, user and user_data, binded between them to "user_data_id" in user model. This is the code in user model:

/**
 * @ORM\Entity
 * @ORM\Table(name="user", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
 * @ORM\HasLifecycleCallbacks()
 */
class User implements UserInterface {
    use Timestamps;
    use SoftDeletes;
....
    /**
     * @ORM\ManyToOne(targetEntity="UserData", inversedBy="user")
     * @ORM\JoinColumn(name="user_data_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $user_data;
...

And this is the code in user user_data model:

/**
 * @ORM\Entity
 * @ORM\Table(name="user_data", options={"collate"="utf8_general_ci", "charset"="utf8", "engine"="InnoDB"})
 * @ORM\HasLifecycleCallbacks()
 */
class UserData {

    use Timestamps;
    use SoftDeletes;

    public function __construct() {

        $this->users = new ArrayCollection();
    }
...

So, when I delete a record in user_data table from phpmyadmin, the related records in user table will be deleted. But when I perform deleting from code, "soft delete" method, updates the deleted_at column fine, but not also in related tables:

        $userDataRepo = $this->em->getRepository('UserData');
        $userData = $userDataRepo->findOneById(1);

        $this->em->remove($userData);
        $this->em->flush();

Some suggestion?

kirkbushell commented 10 years ago

Yeah the soft delete trait doesn't deal with any cascading, it only sets the deleted_at field, as you've pointed out. I think this is something we could definitely support if you wanted to put something together, otherwise I feel it may not make it in for a while.

matiux commented 10 years ago

So, for now, I have to soft deleting all entities manually, right?

hhjcz commented 10 years ago

Well, actually, cascading soft delete works for me, when cascading from the OneToMany side:

**
 * @ORM\Entity
 * @ORM\Table(name = "device")
 */
class Device  {

    use Timestamps;
    use SoftDeletes;

    /**
     * @ORM\OneToMany(targetEntity="Port",
     *      mappedBy = "device", cascade = {"persist", "remove", "merge", "detach"})
     */
    protected $ports;
...
}

/**
 * @ORM\Entity
 * @ORM\Table(name = "port")
 */
class Port {

    use Timestamps;
    use SoftDeletes;

    /**
     * @ORM\ManyToOne(targetEntity = "Device", inversedBy="ports")
     * @ORM\JoinColumn(referencedColumnName = "id")
     */
    protected $device;
...
}

Then removing:

$this->em->remove($device);

Cascades soft deletes like this:

UPDATE port SET deleted_at = '2014-10-14 13:09:30' WHERE id = 322352;
UPDATE device SET deleted_at = '2014-10-14 13:09:30' WHERE id = 79219;
matiux commented 10 years ago

OK, I have decided to remove the foreign key to handle delete cascade with mysql and I have left only the cascade option in model property. So, now work correctly. Thanks for your suggestion and sorry for my english

mitchellvanw commented 9 years ago

@matiux Glad it works and @hhjcz thanks for clarifying.

Closing.

interludic commented 7 years ago

So no cascading with soft deletes?

kirkbushell commented 7 years ago

@interludic this project is no longer maintained.