You can combine Paginator and Containable to do this, and you would
probably run the search against the Member model.

http://lampable.blogspot.com/2008/07/merging-containablebehavior-and.html

On Jul 17, 11:55 am, mrconfused <[EMAIL PROTECTED]> wrote:
> Hello,
> I wanted to know if there is a better way to do this ?
>
> //my old way of getting all my friends but has no pagination.
>  class FriendsController extends AppController
>  {
>         var $name = 'Friends';
>         function view($memberId = null)
>         {
>                 if (!isset($memberId) ){
>                         $memberId = $this->Authentication->getId();
>                 }
>                 $this->pageTitle = "View Friends of " . $memberData["Member"]
> ["fname"];
>
>                 $this->set('memberData',$memberData);
>                 $this->set('friendList', 
> $this->Friend->getFriendList($memberId));
>                 $this->render();
>         }}
>
> ?>
> <?php
> class Friend extends AppModel {
>         var $name = 'Friend';
>
>         function getFriendList($personId = null)
>         {
>                 if (!isset($personId) || empty($personId))
>                 {
>                         return false;
>                 }
>                 else
>                 {
>                         $q = "SELECT Member.id, Member.fname, Member.lname, 
> Profile.gender,
> Profile.primaryPhotoSrc FROM members Member JOIN profiles Profile ON
> Member.id = Profile.member_id WHERE Member.id IN ( SELECT
> receiver_member_id FROM friends WHERE sender_member_id = '" .
> $personId . "') and Member.isActive = '1' order by Member.fname";
>                         return $this->query($q);
>                 }
>         }}
>
> ?>
>
> /******** The above is the old way i did it. I want to be able to
> paginate my view.
>           Is there a better way to do it than the way i have it
> below ? ***************/
>
>  <?php
>   class FriendsController extends AppController
>   {
>         var $name = 'Friends';
>         var $paginate = array(
>                         'fields' => array('Member.id', 'Member.fname',
> 'Member.lname','Member.modified',
> 'Profile.gender','Profile.primaryPhotoSrc'),
>                         'order' => array('Member.fname' => 'asc'),
>                         'limit' => 10
>                         );
>
>         function view($memberId = null)
>         {
>                 $friendIdsExtracted = array();
>                 $memberData = array();
>
>                 //I'd like to view person X's friends.
>                 if (isset($memberId) ){
>                         $this->Session->del($this->name.'.friendId');
>                         
> $this->Session->del($this->name.'.friendIdsExtracted');
>                 }
>
>                 //Scenario: I just in to this action with a memberId and I am
> currently going
>                 //through or paging through all of his friends
>                 if($this->Session->check($this->name.'.friendId')){
>                         $memberData = 
> $this->Session->read($this->name.'.memberData');
>                         $friendId =  
> $this->Session->read($this->name.'.friendId');
>                         $friendIdsExtracted = $this->Session->read($this-
>
> >name.'.friendIdsExtracted');
>
>                 }else{
>                         //I just came into this action with a memberId.
>                         //and I'd like to see page 1 of all of his friends.
>
>                                 if( !isset($memberId) ){
>                                         $memberId = $this->Auth->user("id");
>                                 }
>                                 $memberData = 
> $this->Member->getActiveMemberInfoById($memberId);
>
>                                 //if they want to view friends of a non 
> active member, return him
> to member page.
>                                 if (empty($memberData) ){
>                                         
> $this->redirect(array("controller"=>"members",
> "action"=>"index"),null,true);
>                                 }
>
>                                 //keeping with the fact that i just came in 
> with a memberId and i
> am on page1
>                                 //I want a list of ids that I am a friend 
> with. this is similar to
> the inner select
>                                 //i had before : SELECT receiver_member_id 
> FROM friends WHERE
> sender_member_id = '" . $personId . "'
>
>                                 $listofIds = 
> $this->Friend->findAll(array("sender_member_id" =>
> $memberId), array("receiver_member_id"));
>
>                                 //the list of ids returned is like
> [0]=>array([Friend]=>array(receiver_member_id=>x)),
>                                 //manipulate list to show only the ids.
>                                 $friendIdsExtracted =
> Set::extract($listofIds,'{n}.Friend.receiver_member_id');
>
>                                 
> $this->Session->write($this->name.'.memberData', $memberData);
>                                 
> $this->Session->write($this->name.'.friendId', $memberId);
>                                 
> $this->Session->write($this->name.'.friendIdsExtracted',
> $friendIdsExtracted);
>                 }
>
>                 //this makes the paginate function do a select the above 
> fields from
> Member where id in (a,b,c,d,e,f,g,h) limit x,y
>                 $conditions= array("Member.id" => $friendIdsExtracted);
>
>                 $this->set('memberData', $memberData);
>                 $this->set('friendList', $this->paginate('Member', 
> $conditions));
>         }
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups 
"CakePHP" group.
To post to this group, send email to cake-php@googlegroups.com
To unsubscribe from this group, send email to [EMAIL PROTECTED]
For more options, visit this group at 
http://groups.google.com/group/cake-php?hl=en
-~----------~----~----~----~------~----~------~--~---

Reply via email to