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
1 De Prasad -
merci!
2 De NicolasEC -
Merci beaucoup pour ce tip très efficace !!
J'en étais venu pour cette même solution à faire un :
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, false);
puis un
Doctrine_Manager::getInstance()->setAttribute(Doctrine_Core::ATTR_USE_DQL_CALLBACKS, true);
lorsque je souhaitais intégrer tous mes résultats à mes requêtes. La solution que tu proposes est bien plus efficace à mettre en oeuvre !!