I have two models Products and Offers. Products hasOne Offer. Models are binded with
//Product model:
hasOne
Offer
foreignKey => false
conditions => array(Product.productnumber = Offer.productnumber) // Just normal fields
//Offer model:
belongsTo
Product
There can be many offers with same Offer.productnumber and different Offer.price. What I want to do is find all products so that there is only one offer present with cheapest Offer.price. But now when I trying to do that I get as many duplicate Products as there is Offers with same productnumber. How to avoid this?
I have also seen this: hasMany reduced to hasOne in CakePHP But no luck with containable.
Is only (easiest) option to get separate arrays and then combine them “manually”?
Product hasMany Offers. Product hasMany LowestOffer with an order clause in the relationship conditions array sorted on price ASC and a limit=>1 clause.
Then do a find with containable and pull the LowestOffer back when you want just the single cheapest offer or pull back Offers when you want them all.