In my CakePHP 1.2.5 application, I have a model Profilethat belongs to the model User. The user model has a field username, and when executed find()in the profile model, I always want to automatically get the value User.username. I believe that it would be advisable to change my model model method beforeFind()to automatically contain the desired field.
Here I tried to do:
public function beforeFind($queryData) {
$hasUserData = isset($queryData['contain']) && in_array("User.{$this->User->displayField}", $queryData['contain']);
$hasUserData |= isset($queryData['contain']['User']) && in_array($this->User->displayField, $queryData['contain']['User']);
if (!$hasUserData) {
$queryData['contain']['User'][] = $this->User->displayField;
}
return $queryData;
}
I see that the value $queryData['contain']is being updated correctly, but the username data is not retrieved. I looked at the main CakePHP code for the method find(), and I found that the callback beforeFind()is called after all the Behaviors callbacks, which means that Containable already did what it needed to do with the value $queryData['contain']before I was able to change it.
How can I get around this without cracking the kernel?
source
share