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();
これが私が得ているエラーです:
致命的なエラー:/ [REMOVED] /lib/doctrine/Doctrine/Query/Orderby.php:94スタックトレース:#0 / [REMOVED] / lib / doctrine /Doctrine/のキャッチされない例外'Doctrine_Query_Exception'とメッセージ'Unknowncolumntitle' 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 /[削除済み]/lib/doctrine/Doctrine/Query.php(1126):Doctrine_Query-> buildSqlQuery(false)#3 /[削除済み]/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 / [REMOVED]/lib/doctrine/Doctrine/Query/Orderby.phpの94行目の/srv/web/museumfounda