Do not remove associated Project BOM entries if part is deleted

Instead the part name is put into the name field. This fixes issue #1068
This commit is contained in:
Jan Böhmer 2025-10-17 21:30:40 +02:00
parent e06d9da186
commit 6e4ae15438
2 changed files with 60 additions and 1 deletions

View file

@ -15,7 +15,7 @@ trait ProjectTrait
/** /**
* @var Collection<ProjectBOMEntry> $project_bom_entries * @var Collection<ProjectBOMEntry> $project_bom_entries
*/ */
#[ORM\OneToMany(mappedBy: 'part', targetEntity: ProjectBOMEntry::class, cascade: ['remove'], orphanRemoval: true)] #[ORM\OneToMany(targetEntity: ProjectBOMEntry::class, mappedBy: 'part')]
protected Collection $project_bom_entries; protected Collection $project_bom_entries;
/** /**

View file

@ -0,0 +1,59 @@
<?php
/*
* This file is part of Part-DB (https://github.com/Part-DB/Part-DB-symfony).
*
* Copyright (C) 2019 - 2025 Jan Böhmer (https://github.com/jbtronics)
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
declare(strict_types=1);
namespace App\EntityListeners;
use App\Entity\Parts\Part;
use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener;
use Doctrine\ORM\Event\PreRemoveEventArgs;
/**
* If an part is deleted, this listener makes sure that all ProjectBOMEntries that reference this part, are updated
* to not reference the part anymore, but instead store the part name in the name field.
*/
#[AsEntityListener(event: "preRemove", entity: Part::class)]
class PartProjectBOMEntryUnlinkListener
{
public function preRemove(Part $part, PreRemoveEventArgs $event): void
{
// Iterate over all ProjectBOMEntries that use this part and put the part name into the name field
foreach ($part->getProjectBomEntries() as $bom_entry) {
$old_name = $bom_entry->getName();
if ($old_name === null || trim($old_name) === '') {
$bom_entry->setName($part->getName());
} else {
$bom_entry->setName($old_name . ' (' . $part->getName() . ')');
}
$old_comment = $bom_entry->getComment();
if ($old_comment === null || trim($old_comment) === '') {
$bom_entry->setComment('Part was deleted: ' . $part->getName());
} else {
$bom_entry->setComment($old_comment . "\n\n Part was deleted: " . $part->getName());
}
//Remove the part reference
$bom_entry->setPart(null);
}
}
}