unbindModel() and paginate() gotcha in CakePHP 1.2

A small note to myself regarding a gotcha which I ran into today morning with the built in unbindModel method which lets you disassociate model relations on the fly.

#1 Model->unbindModel() works only for the very next next operation of the model, well nothing new here it is written in the Book.

#2 $this->paginate() runs more than one operation on the model

So if you do something like

  1. $this->Product->unbindModel(array('hasMany' => array('Download')));

and then run a findAll() it works fine but if you do a $this->paginate() it gets all the associated models because after the first operation the bindings are again automagically put in place for you.

Of course there is a way to keep the unbind in place for the rest of the request – this is done by sending the second param to the unbindModel() as false. So your code would now look like

  1. $this->Product->unbindModel(array('hasMany' => array('Download')),false);

running paginate now works as expected.

The catch is that the book. mentions that the second param should be set to true for the unbind to persist through the rest of the request – so I am not sure if this is a bug in the code or a typo in to book, have edited the book as per the above, if the edit is accepted then I guess it was a typo…

Whatever the case – the thing to remember is that unbindModel will always need the second param for it to work with paginate()

About Tarique Sani

Dr. Tarique Sani is a pediatrician and forensic expert by education. He is a PHP programmer of 'wrote the book' caliber and has to his credit several very popular open source as well as commercial PHP projects. He leads a team of dynamic programmers at SANIsoft who have in-depth understanding of Web development tools and usability practices with strong developmental skills in PHP, MySQL/PostgreSQL, HTML, DHTML, Javascript, and Linux/Apache

15 Responses to unbindModel() and paginate() gotcha in CakePHP 1.2

  1. Guillaume March 3, 2008 at 1:44 pm #

    That’s funny, I ran into the same issue yesterday!

  2. keymaster March 3, 2008 at 5:40 pm #

    Just remember that if after the paginate() you want your associated models back, you will need to manually rebind them, since setting the $reset parameter to false in unbindModel() makes the unbinding permanent (not just for the paginate()).

  3. Tarique Sani March 3, 2008 at 6:06 pm #

    @keymaster – nice tip but in my experience usually not much is done after the paginate call in a given request. In the next request the binding will be again there.

  4. w0arz March 24, 2008 at 6:42 pm #

    Thank you it’s was really helpfull :)

  5. Sam S April 25, 2008 at 8:08 am #

    Thanks this information is much appreciated.

    works perfectly

  6. Russel June 6, 2008 at 6:30 pm #

    Thanks, I was finding this solutions.

  7. Mohammadreza August 26, 2008 at 12:52 am #

    that’s great ;)

  8. labanux September 14, 2008 at 1:25 pm #

    Thank’s a lot.. Believe or not, I’ve been looking for this solution for about 1 week..

    oufff… finally i found it here..

    You save my life..

    :)

  9. John February 18, 2009 at 5:00 pm #

    Cheers – I really apprecaite you posting this up. Been searching high and low for a solution.

  10. aditia February 27, 2009 at 7:55 pm #

    Thanks, even your post almost a year ago, it’s very useful for me

  11. Paweł Rabinek April 24, 2009 at 1:15 pm #

    Thanks, works fine! ;)

  12. sava September 29, 2010 at 1:42 am #

    Thanks a lot! You saved my day!

  13. vipin April 13, 2011 at 4:37 pm #

    Thanks a lot! You saved my day!

  14. Rafael F P Viana May 1, 2011 at 8:29 am #

    Very nice tip…thanks a lot dude, you saved my life

  15. Tom Rose November 11, 2011 at 3:28 am #

    Thank you thank you thank you thank you thank you

Leave a Reply