Friday, August 26, 2011

Re: accessing field of paginate array in my view

OK, I would create a model for the BookUser table rather than using
the standard HABTM relationship. This way, BookUser would belongTo
Book and User and you can run a paginate on BookUser being able to
simply include conditions from all 3 tables.

I assume we're working in an action of /users/profile. If so, I would
then have the following (I specify the paginate parameters just before
I call paginate so I can have all variables available for conditions).

function profile($nickname = null) {
if($username) {
$user = $this->User->find('first',
array('conditions'=>array('User.nickname'=>$nickname)));
} else {
$user = $this->User->find('first',
array('conditions'=>array('User.nickname'=>$nickname)));
}

$this->paginate['BookUser'] = array(
'conditions' => array('BookUser.user_id'=>$user['User']['id']),
'contain' => array('Book'),
'order' => array('BookUser.created' => 'DESC'),
'limit' => 25
);
if($user['User']['id'] != $this->Session->read('Auth.User.id')) {
$this->paginate['BookUser']['conditions']['Book.is_visible'] = 1;
}
$books = $this->paginate('BookUser');

$this->set(array(
'user'=>$user,
'books'=>$books
));
}

Untested but, other than some possible typos, it should work fine.

HTH, Paul.

On Aug 24, 9:31 am, Tomfox Wiranata <tomfox.wiran...@gmail.com> wrote:
> and to differ between one attribute "visible=true" and "visible=false"
> i wanted to make two paginates with the same model in the same view.
> but that wont work in cake...
>
> On Aug 24, 10:03 am, Tomfox Wiranata <tomfox.wiran...@gmail.com>
> wrote:
>
>
>
>
>
>
>
> > i wouldnt be surprised if i did :).
> > but i am using the same action "profile" because the index of the
> > books is shown on that profile... or to be more specific, in the view
> > "profile.ctp" i included an
> > "$this->element" that shows all books, that each user is following.
>
> > that is, why i am using one paginator. it happens all in one
> > controller, in my view/action "profile"
>
> > On Aug 24, 9:04 am, WebbedIT <p...@webbedit.co.uk> wrote:
>
> > > Tomfox,
>
> > > I think you're approaching this from the wrong angle.
>
> > > When a user is viewing their profile you're running a different action
> > > to when viewing the index of books. In each of these actions you would
> > > run different paginates/finds on the book model to find what you're
> > > after.
>
> > > /users/profile/$username: Book.user_id=>$user_id
>
> > > /books/index: Book.is_visible=>1
>
> > > HTH, Paul
>
> > > On Aug 23, 10:01 pm, Tomfox Wiranata <tomfox.wiran...@gmail.com>
> > > wrote:
>
> > > > hi everyone,
>
> > > > i have the object book with its attribute visible as boolean. i wanna
> > > > use pagination and it works except for one thing.
>
> > > > first i did two "this->paginate" in my controller. one with visible
> > > > condition true and the other with false. but cake doesnt like two
> > > > paginations in one controller.
>
> > > > so i just returned any book, that a user "follows", no matter if it is
> > > > set to visible true or false.
>
> > > > controller:
> > > >         public $paginate = array('Book' => array('limit' => 1, 'joins' =>
> > > > array(
> > > >         array(
> > > >             'table' => 'books_users',
> > > >             'alias' => 'BookUser',
> > > >             'conditions'=> array('BookUser.fk_book_id = Book.id AND
> > > > Book.locked = false')
> > > >         )
> > > >     )));
>
> > > > NOW in my view, i want to make the distinction. when the user is
> > > > visiting his own profile, non visible books are listed, since these
> > > > are his own. if someone visits a another profile, the non visible
> > > > books shall not appear.
>
> > > > view:
>
> > > > if ($user['User']['username'] == $this->Session->read('User.username'))
> > > > {
>
> > > >      if ($private_books['BookUser']['BooksUser']['visible'] = 'true')
> > > >      .......show
>
> > > >      else
> > > >      .......dont show
>
> > > > Problem: i cant access the visible field from the pagination array,
> > > > that looks like that
>
> > > > [BookUser] => Array
> > > >         (
> > > >             [0] => Array
> > > >                 (
> > > >             [firstname] => test
> > > >             [lastname] => user
> > > >             [username] => testuser
>
> > > >             [BooksUser] => Array
> > > >                 (
>
> > > >                     >>>>>>>>>>>>>>     [visible] => 1
> > > > <<<<<<<<<<<<<<<<<<
> > > >                 )
>
> > > >         )
>
> > > > )
>
> > > > }
>
> > > > so how can i access the visible here? I tried
>
> > > >   if ($private_books['BookUser']['BooksUser']['visible'] = 1)
>
> > > > but it didnt work....
>
> > > > thanks :)- Hide quoted text -
>
> > > - Show quoted text -- Hide quoted text -
>
> > - Show quoted text -

--
Our newest site for the community: CakePHP Video Tutorials http://tv.cakephp.org
Check out the new CakePHP Questions site http://ask.cakephp.org and help others with their CakePHP related questions.


To unsubscribe from this group, send email to
cake-php+unsubscribe@googlegroups.com For more options, visit this group at http://groups.google.com/group/cake-php

No comments: