3

I am using gedmo extension with doctrine2 to apply soft delete and timestampable behaviours(As they were in doctrine1 by default but for doctrine2 it comes in an extension), it is always hard deleting the record. Where as it should only set datetime in "deleted_at" column.

My entity

<?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;

Method I am using to remove record :

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;
    }

The loading of gedmo extension lib :

$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);

I am sure that gedmo lib has been integrated properly because timestamplable features are working.

Jaskaran Singh
  • 2,392
  • 24
  • 39

1 Answers1

4

Wow! I found the answer :

just need to add following annotations on the entity class.

/**
 * @ORM\Entity
 * @ORM\Table(name="ilook_user")
 * @Gedmo\SoftDeleteable(fieldName="deleted_at")
 */
class ilook_user
{
Jaskaran Singh
  • 2,392
  • 24
  • 39
  • This information is easy to miss. I was looking for it aswell ! But no complains about the documentation though... – A.D. Apr 25 '16 at 13:31
  • Yes, timestampable columns (created_at and updated_at) works without this but soft delete column needs to be defined in annotation just above the class. – Jaskaran Singh Apr 28 '16 at 06:06