Php 按原则1.2中生成的字段排序
我正在使用preDqlSelect回调来添加虚拟字段。但是我的查询的验证必须在调用回调之前进行,因为我在查询该模型时不能按新字段排序 这是我的回电:Php 按原则1.2中生成的字段排序,php,doctrine,doctrine-query,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 compan
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/doctor/doctor/Query/Orderby.php:94堆栈跟踪:0/[REMOVED]/lib/doctor/doctor/Query/Abstract.php2077:doctor\u Query\u Orderby->解析“a.title”1/[REMOVED]/lib/doctrine/doctrine/Query.php1160:doctrine\u Query\u Abstract->u processDqlQueryPart'orderby',Array 2/[REMOVED]/lib/doctrine/doctrine/Query.php1126:doctrine\u Query->buildSqlQueryfalse 3/[REMOVED]/lib/doctrine/doctrine/Query/Abstract.php1137:doctrine\u Query->getsqlquery->false 4/[REMOVED]/lib/doctrine/doctrine/Query/Abstract.php1106:doctrine\u Query\u Abstract->\u getDqlCallbackComponentsArray 5/[REMOVED]/lib/doctrine/doctrine/Query/Abstract.php1001:doctrine\u Query\u Abstract->\u preQueryArray 6/srv/web/museumfounda in/[REMOVED]/lib/doctrine/doctrine/Query/Orderby.php第94行的类似内容应该可以:
->select('a.id, a.first_name, a.last_name, a.company, IF(your constructed query from above) AS title')
这应该允许您像现在这样使用排序子句。为了使其更好,您可以在表类中创建查询,并从上面传递构造的查询中的值,以便代码易于维护