gpt4 book ai didi

php - doctrine2 中的软删除功能,使用 gedmo 扩展

转载 作者:可可西里 更新时间:2023-11-01 01:00:30 25 4
gpt4 key购买 nike

我正在使用带有 doctrine2 的 gedmo 扩展来应用软删除和时间戳行为(默认情况下它们在 doctrine1 中,但对于 doctrine2 它是扩展),它总是很难删除记录。它应该只在“deleted_at”列中设置日期时间。

我的实体

<?php
namespace Entities;
use Doctrine\ORM\Mapping AS ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
* @ORM\Entity
*/
class ilook_user
{
/**
* @ORM\Id
* @ORM\Column(type="integer", length=11)
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
.
.
.
/**
* @var datetime $created_at
*
* @Gedmo\Timestampable(on="create")
* @ORM\Column(type="datetime")
*/
private $created_at;

/**
* @var datetime $updated_at
*
* @Gedmo\Timestampable(on="update")
* @ORM\Column(type="datetime")
*/
private $updated_at;

/**
* @ORM\Column(name="deleted_at", type="datetime", nullable=true)
*/
private $deleted_at;

我用来删除记录的方法:

public static function softDeleteUser($user_id)
{
//Delete user
$em = \Zend_Registry::get('em');
$usr_obj = $em->find('\Entities\ilook_user', $user_id);
$em->remove($usr_obj);
$em->getConnection()->close();
$em->flush();
return TRUE;
}

gedmo 扩展库的加载:

$classLoader = new \Doctrine\Common\ClassLoader('Gedmo', realpath(__DIR__ . '/../library/'), 'loadClass');
$classLoader->register();
$autoloader->pushAutoloader(array($classLoader, 'loadClass'), 'Gedmo');


Gedmo\DoctrineExtensions::registerAbstractMappingIntoDriverChainORM(
$driverChain, // our metadata driver chain, to hook into
$cachedAnnotationReader // our cached annotation reader
);

// timestampable
$timestampableListener = new Gedmo\Timestampable\TimestampableListener;
$timestampableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($timestampableListener);

// SoftDeleteable
$softDeleteableListener = new Gedmo\SoftDeleteable\SoftDeleteableListener;
$softDeleteableListener->setAnnotationReader($cachedAnnotationReader);
$evm->addEventSubscriber($softDeleteableListener);

我确信 gedmo lib 已正确集成,因为可时间戳的功能正在运行。

最佳答案

哇!我找到了答案:

只需要在实体类上添加如下注解即可。

/**
* @ORM\Entity
* @ORM\Table(name="ilook_user")
* @Gedmo\SoftDeleteable(fieldName="deleted_at")
*/
class ilook_user
{

关于php - doctrine2 中的软删除功能,使用 gedmo 扩展,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28562547/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com