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
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?