Cakephp 3 子查询表达式

Cakephp 3 subquery expressions

我正在尝试根据类型字段在此处创建条件 select。为什么 $exp->addCase() 调用无效,它说 addCase() 是未知方法?

$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']
    );
});

因为 select() 的可调用项与 where() 的可调用项的工作方式不同。 select() 的可调用项是通过传递给它们的当前查询调用的(即您正在对查询对象调用 addCase()),并且它们应该 return 到 [=30] 的字段列表=].

您要执行的操作需要将表达式对象直接传递给 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]);

另见