按照Doctrine 1.2中的生成字段排序

时间:2010-09-03 20:30:06

标签: php doctrine doctrine-query

我正在使用preDqlSelect()回调来添加“虚拟字段”。但是我的查询验证必须在回调被触发之前发生,因为当我查询该模型时,我无法通过该新字段进行排序。

这是我的回调:

class Artist extends BaseArtist
{
    public function preDqlSelect(Doctrine_Event $event)
    {

        // Add title field (concatenation of first_name, last_name, and company fields)
        $params = $event->getParams();
        $q = $event->getQuery();
        $a = $params['alias'];
        if (
        $q->contains($a.'.first_name')
        && $q->contains($a.'.last_name')
        && $q->contains($a.'.company')
        ) {
            $exists = '!ISNULL(NULLIF('.$a.'.%s, \'\'))';
            $value = 'IFNULL('.$a.'.%1$s, \'\')';
            $if = sprintf($exists, 'first_name').' OR '.sprintf($exists, 'last_name');
            $thenPiece1 = sprintf($value, 'first_name').', \' \', '.sprintf($value, 'last_name');
            $thenPiece2 = 'IF('.sprintf($exists, 'company').', CONCAT(\' (\', '.sprintf($value, 'company').', \')\'), \'\')';
            $then = 'TRIM(CONCAT('.$thenPiece1.', '.$thenPiece2 .'))';
            $else = sprintf($value, 'company');
            $select = 'IF('.$if.', '.$then.', '.$else.') AS title';
            $q->addSelect($select);
        }
    }
// ...

这是我的疑问:

$artists = Doctrine_Query::create()
    ->select('a.id, a.first_name, a.last_name, a.company')
    ->from('Artist a')
    ->innerJoin('a.Products p')
    ->where('a.active <> 0')
    ->andWhere('p.active <> 0')
    ->orderBy('a.title')
    ->execute();

这是我得到的错误:

致命错误:未捕获的异常'Doctrine_Query_Exception',并在/[REMOVED]/lib/doctrine/Doctrine/Query/Orderby.php:94中显示消息'未知列标题'堆栈跟踪:#0 / [REMOVED] / lib / doctrine /Doctrine/Query/Abstract.php(2077):Doctrine_Query_Orderby-&gt; parse('a.title')#1 /[REMOVED]/lib/doctrine/Doctrine/Query.php(1160):Doctrine_Query_Abstract-&gt; _processDqlQueryPart( 'orderby',Array)#2 /[REMOVED]/lib/doctrine/Doctrine/Query.php(1126):Doctrine_Query-> buildSqlQuery(false)#3 / [REMOVED] / lib / doctrine / Doctrine / Query / Abstract .php(1137):Doctrine_Query-&gt; getSqlQuery(Array,false)#4 /[REMOVED]/lib/doctrine/Doctrine/Query/Abstract.php(1106):Doctrine_Query_Abstract-> _getDqlCallbackComponents(Array)#5 / [ REMOVED] /lib/doctrine/Doctrine/Query/Abstract.php(1001):Doctrine_Query_Abstract-> _preQuery(Array)#6 / srv / web / museumfounda in / [REMOVED] / lib / doctrine / Doctrine / Query / Orderby。第94行的PHP

1 个答案:

答案 0 :(得分:1)

类似的东西应该有效:

->select('a.id, a.first_name, a.last_name, a.company, IF(your constructed query from above) AS title')

这应该允许您现在使用您的订购条款。为了更好,您可以在Table类中创建查询并传递your constructed query from above中的值,以便代码易于维护。