-
-
Save jeremymarc/2705639 to your computer and use it in GitHub Desktop.
USE INDEX / FORCE INDEX in a Doctrine2 DQL query
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use UseIndexWalker; | |
use Doctrine\ORM\Query | |
$query = $em->createQuery("SELECT f FROM Foo f WHERE f.a = 1 and f.b = 2"); | |
$query->setHint(Query::HINT_CUSTOM_OUTPUT_WALKER, 'UseIndexWalker'); | |
$query->setHint(UseIndexWalker::HINT_USE_INDEX, 'some_index_name'); | |
$query->getResult(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
use Doctrine\ORM\Query\SqlWalker; | |
/** | |
* Quick hack to allow adding a USE INDEX on the query | |
*/ | |
class UseIndexWalker extends SqlWalker | |
{ | |
const HINT_USE_INDEX = 'UseIndexWalker.UseIndex'; | |
public function walkFromClause($fromClause) | |
{ | |
$result = parent::walkFromClause($fromClause); | |
if ($index = $this->getQuery()->getHint(self::HINT_USE_INDEX)) { | |
$result = preg_replace('#(\bFROM\s*\w+\s*\w+)#', '\1 USE INDEX (' . $index . ')', $result); | |
} | |
return $result; | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment