2015-12-22 42 views
0

我想在这里创建基于类型字段的条件选择。为什么$exp->addCase()呼叫无效,它说,addCase()是未知方法?Cakephp 3子查询表达式

$query->select(function ($exp) use ($query) { 
    $concatPerson = $query->func()->concat(['lastname' => 'literal', ', ', 'firstnames' => 'literal']); 
    return $exp->addCase(
     [ 
      $query->newExpr()->eq('type', 1), 
      $query->newExpr()->eq('type', 2), 
     ], 
     [$concatPerson, 'business_name'], 
     ['string', 'string'] 
    ); 
}); 

回答

2

因为对于select()可调用不工作的方式可调用的where()相同。使用传递给它们的当前查询调用select()的可调参数(例如,您在查询对象上调用addCase()),并且它们需要返回要选择的字段列表。

什么你正在尝试做的,需要直接传递表达反对select(),像

$concatPerson = $query->func()->concat([ 
    'lastname' => 'literal', ', ', 'firstnames' => 'literal' 
]); 
$exp = $query->newExpr()->addCase(
    [ 
     $query->newExpr()->eq('type', 1), 
     $query->newExpr()->eq('type', 2), 
    ], 
    [$concatPerson, 'business_name'], 
    ['string', 'string'] 
); 

$query->select(['field_alias' => $exp]); 

参见

+0

感谢您的。这是有用的信息。 – makallio85