我正在使用 gedmo 扩展和 dotric2 来应用软删除和时间戳行为(因为它们在默认情况下在 dotric1 中,但对于 dotric2 它在扩展中),删除记录总是很困难。因为它应该只在“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 - 使用 gedmo 扩展名的 Doction2 软删除功能,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/28562547/

10-11 12:51