CakePHP: Paginate using multiple tables/associations
Posted (Amit) in General on 27-01-2010
I was stuck with a scenario while working on one of the CakePHP application where i have the following association:
City belongsTo => State
State belongsTo => country
In case of Ruby on Rail we can use ‘through’ to create a relationship between the City and Country, but in case of CakePHP we have two things 1) recursive 2) Contain, but in my case both were not working so i searched and found an alternate way of establishing relationship between City and Country so when i run a find on City i also get the Country data in the resultset. Here is the function that i used.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | function unbindAndBind(){ // unbind State model for entire request $this->City->unbindModel(array('belongsTo' =>array('State')), false); // unbind State->Country model for entire request $this->City->State->unbindModel(array('belongsTo' =>array('Country')), false); // rebind Site and Company models with custom condition for Company model forcing join $this->City->bindModel(array ( 'belongsTo' => array( 'State' => array(), 'Country' => array( 'foreignKey' => false, 'conditions' => array('State.country_id = Country.id') ) ) ), false ); } |















