I have the following code in my model:
var $name = 'Product';
var $hasMany = array(
'AttachedProduct' => array(
'className' => 'AttachedProduct',
'foreignKey' => 'product_id'
),
'Sale' => array(
'className' => 'Sale',
'foreignKey' => 'item_id',
'fields' => 'Sale.peso_value'
)
);
I want to use the Sale part only. Currently when I query my Products model the ‘AttachedProduct’ is also returned. I want only the Sale records to be returned.
Thanks for any help! 🙂
The relationships in a model like
hasManyandbelongsToare just possible definitions. You can choose which ones you want to return when doing afind()with something called the Containable behavior. Check out the docs for details.You start by adding the behavior to your
Productmodel:Then when you want to query for your Products, you put the
hasManyrelationships that you want to return in thecontain()method before thefind().Then you’ll only have the Products and associated Sales.