Doctrine: Howto bypass the SoftDelete behaviour

For one of my projects I have to check if a record already exists in my database, deleted or not. The preDqlSelect looks like this :

/**
     * Implement preDqlSelect() hook and add the deleted flag to all queries for which this model 
     * is being used in.
     *
     * @param Doctrine_Event $event 
     * @return void
     */
    public function preDqlSelect(Doctrine_Event $event)
    {
        $params = $event->getParams();
        $field = $params['alias'] . '.' . $this->_options['name'];
        $query = $event->getQuery();
        if ( ! $query->contains($field)) {
            $query->addWhere(
                $field . ' = ' . $query->getConnection()->convertBooleans(false) . ' OR ' . $field . ' IS NULL'
            );
        }
    }

The method is adding the statement only if !$query->contains($field), and contains is just doing an stripos on the DQL. At this time, the DQL is the part between the FROM to the end. It doesn't include the SELECT part. So to bypass the SoftDelete behaviour, the field deleted have to be in the DQL. A simple solution is to add a dummy statement like this :

$results = Doctrine_Query::create()
    ->from('Table t')
    ->where('t.deleted = t.deleted')
    ->execute()
    ;

Thanks to vjousse for the tip

Haut de page