我正在使用带有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已正确集成,因为时间戳功能正在运行。
答案 0 :(得分:4)
哇!我找到了答案:
只需要在实体类上添加以下注释。
/**
* @ORM\Entity
* @ORM\Table(name="ilook_user")
* @Gedmo\SoftDeleteable(fieldName="deleted_at")
*/
class ilook_user
{