8

I have problem with soft delete item with form collection in Symfony framework

I want to add and remove items in relation with Symfony form collection, If I remove item from collection this item must soft deleted from relation records

with below image you can imagine relations

product relation generated with Mysql workbench

This is my form collections in Symfony forms

Main form
   +
   |
   ProductType+
   |          |
   |          |
   |          PackagesType+
   |                      |
   |                      |
   |                      PackageProductsType
   |
   |
   +

Package products add and removed with JS in frontend based on Symfony example for form collections

my problem is when I remove completely from package_product with activating orphanRemoval=true in annotation of entity every thing work well but record removed completely and I want record soft deleted based on my propose, But when I change below function in Package Entity

    public function removePackageProduct(PackageProduct $packageProduct): self
    {
        if ($this->packageProducts->contains($packageProduct)) {
            $this->packageProducts->removeElement($packageProduct);
            // set the owning side to null (unless already changed)
            if ($packageProduct->getPackage() === $this) {
                $packageProduct->setPackage(null);
            }
        }
        return $this;
    }

always in top function I get last record in packageProducts and when I remove this line $packageProduct->setPackage(null); to disable loosing data in record and replace it with $packageProduct->setDeletedAt(new \Datetime()); to set deleted item, In saved records removed record delete from database and a new record added filled with last record data and deletedAt datetime.

For example:

PackageProduct records before delete first item:

+----+------------+------------+--------+-----------+
| ID | Product_id | package_id | amount | deletedAt |
+----+------------+------------+--------+-----------+
|  1 |         10 |          1 |      3 |           |
|  2 |         11 |          1 |      4 |           |
|  3 |         12 |          1 |      5 |           |
+----+------------+------------+--------+-----------+

PackageProduct records after delete first item:

+----+------------+------------+--------+-----------+
| ID | Product_id | package_id | amount | deletedAt |
+----+------------+------------+--------+-----------+
|  2 |         11 |          1 |      4 |           |
|  3 |         12 |          1 |      5 |           |
|  4 |         12 |          1 |      5 |2020-1-20..|
+----+------------+------------+--------+-----------+

But I expected PackageProduct records after delete first item:

+----+------------+------------+--------+-----------+
| ID | Product_id | package_id | amount | deletedAt |
+----+------------+------------+--------+-----------+
|  1 |         10 |          1 |      3 |2020-1-20..|
|  2 |         11 |          1 |      4 |           |
|  3 |         12 |          1 |      5 |           |
+----+------------+------------+--------+-----------+

How can fix this to get expected result?

Morteza
  • 355
  • 8
  • 22
  • Do I correctly understand the problem: Instead of deleting doctrine collection entries, you only want to update their `deletedAt` field, but whenever you do that, the changed entry gets persisted as a new entity? – Barthy Jan 28 '20 at 01:56
  • @Barthy Yes, exactly – Morteza Jan 28 '20 at 05:47
  • The only change you made is changing the `setPackage()` to `setDeletedAt()`? Did you remove `removeElement()`? – msg Oct 25 '20 at 09:28

0 Answers0