Breaking News
You are here : Home / Web development / CakePHP / Using CakePHP containable Behavior with Paginator
cake-php-containable-w-paginator

Using CakePHP containable Behavior with Paginator

 CakePHP containable Behavior with Paginator:

Hola Guys ! CakePHP’s  containable Behaviour is very useful and robust way to achieve your programming goals in cakePHP. Some time we dont need recursive data as it produces lot of useless data and slows down the speed of our project.So to overcome this issue we have a cakephp containable behavior. In this post i’ll focus on how we use cakephp Containable Behavior with the Paginator . As other ways of using Containable behavior are well mentioned in book.cakephp.org .

Using containable Behavior with Paginator:

So when ever we wana use cakephp containable Behavior we need to include in it our model or just include it in our AppModel ,So it will automatically included in all our Models.Here is how we do it

Including cakephp Containable Behavior in AppModel:

app_model.php

class AppModel extends Model{
[highlight bg=”#DDFF99″ color=”#000000″]var $actsAs = array(‘Containable’);[/highlight]
}

 

Using containable Behavior with Paginator in Our Controller:

SO now we ave inluded Cakephp Containable behavior in our AppModel , Now we See how to use it in our Contoller,as i said before we look how to use  Containable behavior with the Paginator in cakephp .So we see how to do it:

Using Cakephp Containable behavior with paginator:

Now inside our controller we’ll  use cakephp Containable Behavior with the paginator and we gonna paginate results with the help of containable behavior.Lets see how we gonna do it:

Using Containable behavior with paginator in CakePHP

$this->paginate[‘Profile’][‘contain’]=array(“User”=>array(

“fields”=>array(“User.id”,”User.name”)  //will fetch user name and id only

),

“UserBiodata”=>array(

“UserEmployer”=>array(“fields”=>array(“UserEmployer.name”)

)

)

//the above code will fetch UserBiodata table and also the UserEmployer’s name field.

//The UserBiodata and UserEmployer must have a relation to perform this kind of fetch

);

Now that was easy.And also produces clean data.I thing more is that if  you are using Containable with paginator You need not to use recursive.So while using cakephp containable behavior in paginator we’ll not use  the recursive.

Another Important issue is that the limit of paginator will not work using the above containable with the Paginator in cakephp .So here is a fix to the paginator limit.Just add another line like below.

Using Containable behavior with paginator in CakePHP

$this->paginate[‘Playlist’][‘limit’]=3;

//Using limit in paginator with the containable

 

That was all i guess for the cakephp’s Containable behavior with the paginator.So thankx for visiting designaeon.com and reading the content written by us.

Hope you liked our content and Tutorial might be helpful.Do not forget to share if it was helful

Tnxxxx.

[separator/]

Tags : , ,

1 Reply

  1. Rashmil says:

    sir,
    I am new to cakephp and want to create my own plugin. Could u please help me out with it.

Leave a Reply

Want to join the discussion ? Feel Free to contribute !

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code class="" title="" data-url=""> <del datetime=""> <em> <i> <q cite=""> <strike> <strong> <pre class="" title="" data-url=""> <span class="" title="" data-url="">