0

我正在尝试根据类型字段在此处创建条件选择。为什么$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']
    );
});
4

1 回答 1

2

因为 callables 的select()工作方式与 callables 不同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]);

也可以看看

于 2015-12-23T17:58:03.093 回答