📜 ⬆️ ⬇️

Cakephp and cunning pagination

My most painful place in CakePHP is pagination. Especially when it comes to tricks. Banal -> find ('all') always works (well, almost always) well and well, but pagination ...
Given:

  1. There is a sign of real estate (Immovable) with a specified price (Immovable.price) and type of currency (Immovable.currency_id)
  2. There is a currency plate (Currency) with fields: id, name, coeff (coefficient)

Accordingly, in the currency table there are coefficients USD coeff = 1, for the currency Euro coeff = 1.34.
Task:

Sort the issue (paginate) by price, taking into account the currency. Those. a house for 100,000 euros is more expensive than a house for 110,000 dollars (with a rate of 1 to 1.34)
Decision

$this->paginate = array('order' => array('Count' => 'ASC'),
'fields' => array('*','((`Immovable`.`price`*`Currency`.`coeff`)) as Count'));


Yes, it is in double brackets. Then the query will be correct with the LEFT JOIN set to Currency and properly sorted by the calculated field.

Yes, I tested it with MySQL, because it was not possible to work with other databases. I hope someone will help this thread :)

')

Source: https://habr.com/ru/post/57946/


All Articles