I have this relation; Property hasMany Chargerate.
Property has column 'default_price'. Chargerate has 'start_date', 'end_date' and 'price' columns.
My query is like this:
$q = PropertyTable::getInstance()->createQuery('p')
->select('p.*, ROUND(r.price) as cr_price)
->groupBy('p.id')
->leftJoin('p.Chargerate r WITH r.price <= ?', $maxPrice)
->addWhere('p.default_price <= ? OR cr_price <=?, array($maxPrice, $maxPrice) ;
The idea: if $maxPrice= 100, the query should return Propertis whose 'default_price' is less than 100, or properties whose at least one Chargerate has 'price' column less than 100.
Although this query work good under unit tests, when I send it to Doctrine_Pager it fails. So, something is wrong with it. I was thinking of using HAVING() clause, but it must be leftJoin() because most properties will not have related Chargerates.
More details explanation: Some property (house, hotel) has default price of $100 per night. But during some parts of year, they can define special discount Chargerate of ie. $50 per night.
So, when hotel doesn't have discount price, it should be returned for search $maxPrice = 150. If $maxPrice = 90, that hotel will not get fetched. But; if there is Chargerate.price = 50, this hotel will be fetched.
I think the problem is because I used aggregate function in addWhere(), but I don't know how else to do it.