I’m wanting to find the maximum and minimum values in some associated data (via a one to many relationship). I thought it might be possible to use the max() and min() functions in the view eg
$min_price = $thing->associated_things->min('price');
But I discovered that the associated data is arriving as an array rather than an entity on which I can use max() or min().
The association is set-up in the model as -
$this->hasMany('AssociatedThings', [
'foreignKey' => 'thing_id',
]);
I’m then using contain to pass on the association to the view -
$things = $this->$Things->find('all',['contain' => 'AssociatedThings']);
I’m getting round this by iterating on the array in the view to find the max and min but it’s a bit clunky. Is there a better way of doing this and is associated data defined in this way always an array?