Cake does this because it relies on PDO::getColumnMeta() to provide
the alias key -- in this case 'Person' -- under which to put the data
in the result array. But the database's column meta info doesn't
include the table for "calculated" fields so Cake puts it under an
"anonymous" key.
In your case, the simplest solution is to use a virtualField. Put this
in your Person model:
public $virtualFields = array(
'full_name' => 'CONCAT(Person.first_name, " ", Person.last_name)'
);
It's still a calculated field but now Cake knows that it's a
virtualField and will put the value under the Person key.
http://book.cakephp.org/2.0/en/models/virtual-fields.html
For other situations, you can have AppModel automatically transfer
values from under numeric keys to where you want them using this
clever trick:
http://teknoid.wordpress.com/2008/09/29/dealing-with-calculated-fields-in-cakephps-find/
On Thu, Aug 2, 2012 at 6:42 PM, Mariano C. <mariano.calandra@gmail.com> wrote:
> I have this function write in a CakePHP model:
>
>> public function getPeopleByName($name){
>> $this->unbindModel(array('hasMany' =>
>> array('OfficePersonTask')));
>>
>> $options['fields'] = array("Person.id",
>> "CONCAT(Person.first_name, ' ', Person.last_name) AS full_name");
>>
>> return $this->find('all', $options);
>> }
>
>
> This gave me the following json:
>
>> {
>> People:[
>> {
>> 0:{
>> full_name:"Groucho Marx"
>> },
>> Person:{
>> id:"1"
>> }
>> },
>> {
>> 0:{
>> full_name:"Giovanni Ferretti"
>> },
>> Person:{
>> id:"2"
>> }
>> }
>> ]
>> }
>
>
> I would that *full_name* will be part of *Person* group (actually is in a
> group called 0, all alone). How I can do that?
>
> --
> Our newest site for the community: CakePHP Video Tutorials
> http://tv.cakephp.org
> Check out the new CakePHP Questions site http://ask.cakephp.org and help
> others with their CakePHP related questions.
>
>
> To unsubscribe from this group, send email to
> cake-php+unsubscribe@googlegroups.com For more options, visit this group at
> http://groups.google.com/group/cake-php
--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.
To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php
Thursday, August 2, 2012
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment