Saturday, July 3, 2021

Getting associated data in CakePHP 3 loaded using 'contain'

 So you've loaded associated data using 'contain' eg:

$object_attributes = $object_attributes_table 
->find("all", ["contain" => ["attributes"]]) 
->where(["object_id = " => $object_id]);

So how do actually access the data? 

Well it's stored as an array. So in the above example to get the attribute name:

$name = $object_attribute->Attributes['name'];