开发者

CakePHP - Column aliases in SQL queries

Is there a good way to specify column aliases when performing find() operations on a model?

$this->User->find('first', array(
    'fields' => array(
        'CONCAT(firstname, ' ', surname) AS fullname',
        'email',
        'tel'
    )
);

At the moment, if I do it like this it returns the data like this:

Array
(
    [0] => Array
        (
            [fullname] => John Smith
        )

    [User] => Array
        (
            [email] => jsmith@example.co开发者_JS百科m
            [tel] => 0123456789
        )

)

Is there a way to have it return the column aliases like normal columns?

Array
(
    [User] => Array
        (
            [fullname] => John Smith
            [email] => jsmith@example.com
            [tel] => 0123456789
        )

)


For this you're supposed to use Virtual Fields.


For cakephp 2.x in query:

'CONCAT(firstname, ' ', surname) AS User__fullname',

http://book.cakephp.org/2.0/en/models/virtual-fields.html#virtual-fields-in-sql-queries

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜