我希望将特定实体进行软删除(而不是所有实体)。我已经安装了StofDoctrineExtensionsBundle扩展包,这应该给我提供Softdeleteable功能。
所以我更新了我的实体:
我创建了迁移并运行了迁移。我的表
根据文档,现在可以运行此代码以软删除记录:
所以我更新了我的实体:
User.php
<?php
namespace App\Entity;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\SoftDeleteable\Traits\SoftDeleteableEntity;
/**
* @ORM\Entity(repositoryClass="App\Repository\UserRepository")
* @Gedmo\SoftDeleteable(fieldName="deletedAt", timeAware=false, hardDelete=false)
*/
class User implements UserInterface
{
use SoftDeleteableEntity;
我创建了迁移并运行了迁移。我的表
User
现在有一个额外的列deleted_at
。根据文档,现在可以运行此代码以软删除记录:
public function delete(User $user, EntityManagerInterface $em)
{
$em->remove($user);
$em->flush();
然而,这会导致错误,因为用户实体具有关系,用户本身无法被删除。当然,这就像我编程时所做的一样。但我并不真正想删除记录,我想软删除记录。
An exception occurred while executing 'DELETE FROM user WHERE id = ?' with params [79]:
SQLSTATE[23000]: Integrity constraint violation: 1451 Cannot delete or update a parent row: a foreign key constraint fails (`thedatabase`.`shoppingcart`, CONSTRAINT `FK_932C7444A76ED395` FOREIGN KEY (`user_id`) REFERENCES `user` (`id`))
阅读文档时,提到了设置软删除的内容。但说实话,我不知道如何解决这个问题。
我该如何在Symfony 4中使用软删除?