views:

85

answers:

2

Hi there. I've got a model Employee which belongsTo an Address-model. When I fetch data from the Employees model, the associated Address record gets fetched too. Additionally, the Address model has a virtualField full_name. This looks like this:

Array 
(
[0] => Array
       (
        [Employee] => Array
            (
                [id] => 1
                [address_id] => 33
                [username] => ...
                ...
            )

        [Address] => Array
            (
                [id] => 33
                [firstname] => Blah
                [full_name] => Blah Blubb
                ...
            )

    )

[1] => Array  (
        [Employee] => Array   (
                [id] => 2
                ...

I want to have included this virtualField in the Employee part of the data array too, like

Array (
[0] => Array (
        [Employee] => Array
            (
                [id] => 1
                [full_name] => Blah Blubb
                ...
            )

Tis isn't possible to solve by just adding

$virtualFields = array(
    'full_name' => 'CONCAT_WS(" ", Address.firstname, Address.surname)',
);

to the Employees model, as the Cookbook states There is a solution proposed ("copy virtualFields from one model to another at runtime when you need to access them"), but I don't understand this solution. Where do I have to place this? In the controller? In the model in the find function?

Thanks for Help

A: 

Add the following to your model:

public function __construct($id=false,$table=null,$ds=null){
    parent::__construct($id,$table,$ds);
    $this->virtualFields = array(
        'full_name'=>"CONCAT(`Address.firstname`,' ',`Address.surname`)"
    );
}
ABailiss
Regrettably, this didn't work for my, and I've found that I've got to use a completely different approach for this. Inside the Employee controller, I now use the containable behavior to fetch the address data, and then I have to manually loop through the records and insert the full_name field into the 'Employee' part of the records array. This may be ugly, but it works..
joni
A: 

I'm doing this by hand now in the afterFind model callback function:

array_walk($results,function(&$a){
        if(isset($a['Address']['full_name'])) {
            $a['Employee']['full_name'] = $a['Address']['full_name'];
            unset($a['Address']);
        }
    });

Might be it's not nice, but it works.

joni