I'm currently attaching a virtual field in beforeFind()
because I need to insert the current user's id into the query. Calling find on a related model and containing this model includes statically defined virtual fields but not the one in beforeFind()
.. Calling find directly on the model includes the dynamically attached virtual field.
Here is my beforeFind callback:
public function beforeFind($query = array()) {
$user_id = $this->getCurrentUser()['id'];
$this->virtualFields = array_merge($this->virtualFields, array(
'cost_for_user' => sprintf('CASE WHEN Inventory.user_id = %s THEN Inventory.cost ELSE Inventory.cost_for_team END', $user_id),
));
return $query;
}
Since cost_for_user
is dynamically attached in beforeFind I can't copy the virtual fields over at runtime with like suggested in the cookbook. Is there a better callback for dynamically attaching virtual fields so there are included in contain results?