doumei7420 2010-09-03 20:30
浏览 21
已采纳

too long

I'm using a preDqlSelect() callback to add a "virtual field". But the validation of my query must be happening before the callback get fired because I can't order by that new field when I query that model.

Here's my callback:

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);
        }
    }
// ...

And here's my query:

$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();

Here's the error I'm getting:

Fatal error: Uncaught exception 'Doctrine_Query_Exception' with message 'Unknown column title' in /[REMOVED]/lib/doctrine/Doctrine/Query/Orderby.php:94 Stack trace: #0 /[REMOVED]/lib/doctrine/Doctrine/Query/Abstract.php(2077): Doctrine_Query_Orderby->parse('a.title') #1 /[REMOVED]/lib/doctrine/Doctrine/Query.php(1160): Doctrine_Query_Abstract->_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->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.php on line 94

  • 写回答

1条回答 默认 最新

  • drmy1050 2010-09-04 17:51
    关注

    Something similar to this should work:

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

    This should allow you using you ordering clause as you do it now. To make it nicer, you could create the query inside a Table class and pass the values from your constructed query from above so the code is easy to maintain.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 BP神经网络控制倒立摆
  • ¥20 要这个数学建模编程的代码 并且能完整允许出来结果 完整的过程和数据的结果
  • ¥15 html5+css和javascript有人可以帮吗?图片要怎么插入代码里面啊
  • ¥30 Unity接入微信SDK 无法开启摄像头
  • ¥20 有偿 写代码 要用特定的软件anaconda 里的jvpyter 用python3写
  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算