madhukar
madhukar

Reputation: 61

Self join in doctrine-orm: Symfony

I have an entity for logs where I save application error records with a field for inserted timestamp. My requirement is to purge entries which are older than 10 days from last log record. My entity structure is like:

class MyacuvueLogs
{
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue()
     * @ORM\Column(type="integer")
     */
    private $id;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $mobile;

    /**
     * @ORM\Column(type="integer", nullable=true)
     */
    private $created;

    /**
     * @ORM\Column(type="string", length=255)
     */
    private $apiName;

    /**
     * @ORM\Column(type="string", length=255, nullable=true)
     */
    private $apiReqParams;
}

Please help.

My code to delete old log records is as below:

public function getOldLog($max_time)
    {
        $remove_rows = $this->createQueryBuilder('p')
                            ->delete()
                            ->where('p.created < :createdBeforeLimitDate')
                            ->setParameter('createdBeforeLimitDate',$max_time-10*86400);
        return "deleted rows";
    }

However this is not working.

Upvotes: 0

Views: 132

Answers (1)

Jakumi
Jakumi

Reputation: 8374

you have the following code:

public function getOldLog($max_time)
{
    $remove_rows = $this->createQueryBuilder('p')
                        ->delete()
                        ->where('p.created < :createdBeforeLimitDate')
                        ->setParameter('createdBeforeLimitDate',$max_time-10*86400);
    return "deleted rows";
}

that code doesn't execute anything, because you just built a query and stored it in a variable. and then: nothing.

you have to call getQuery and getResult for it to be executed:

public function getOldLog($max_time)
{
    $remove_rows = $this->createQueryBuilder('p')
                        ->delete()
                        ->where('p.created < :createdBeforeLimitDate')
                        ->setParameter('createdBeforeLimitDate', $max_time-10*86400)
                        ->getQuery()     // <- new
                        ->getResult();   // <- new
    return "deleted rows";
}

Upvotes: 1

Related Questions