TableAlias__ColumnAlias虚拟字段中的语法未按文档进行分析



Docs 声明我们可以使用像 Timelog__TotalHours 这样的语法来命名计算字段,PHP 会将其解析为:

[0] => Array
(
    [ProductsItems] => Array
        (
            [Total] => 50
    )
)

在我的 CakePHP/2.9.4 设置中,我正在几个地方尝试它,例如分页,但我的列名像任何其他字符串一样处理:

[0] => Array
(
    [ProductsItems__Total] => 50
)

我在控制器中的测试代码是:

$this->Paginator = $this->Components->load(
    'Paginator',
    array(
        'fields' => array(
            "CONCAT_WS(' ', Usuario.nombre, Usuario.apellidos) AS Usuario__Empleado",
        ),
        'contain' => array(
            'Usuario',
        ),
        'limit' => 2,
    )
);
debug($this->Paginator->paginate());

。和打印:

array(
    (int) 0 => array(
        (int) 0 => array(
            'Usuario__Empleado' => 'Juan García'
        ),
        'Usuario' => array(
            'id' => '56'
        )
    ),
    (int) 1 => array(
        (int) 0 => array(
            'Usuario__Empleado' => 'María López'
        ),
        'Usuario' => array(
            'id' => '385'
        )
    )
)

我是否需要在某处显式启用此功能?

首先,您应该在查询之前定义虚拟字段(可以是模型或控制器(。

$this->CurrentModel->Usuario->virtualFields['Empleado'] = 0;
$this->Paginator = $this->Components->load(
    'Paginator',
    array(
        'fields' => array(
            "CONCAT_WS(' ', Usuario.nombre, Usuario.apellidos) AS Usuario__Empleado",
        ),
        'contain' => array(
            'Usuario',
        ),
        'limit' => 2,
    )
);
debug($this->Paginator->paginate());

它应该有效。

最新更新