hello,
if i understand your view display several posts
if your jointable is named fav_posts_users for exemple
you should have in your post model :
var $hasAndBelongsToMany = array(
        'Favorite' => array(
                        'className' => 'User',
                        'joinTable' => 'fav_posts_users',
                        'foreignKey' => 'post_id',
                        'associationForeignKey' => 'user_id',
                        'unique' => true
        ),
);
in your user model :
var $hasAndBelongsToMany = array(
        'Favorite' => array(
                        'className' => 'Post',
                        'joinTable' => 'fav_posts_users',
                        'foreignKey' => 'user_id',
                        'associationForeignKey' => 'post_id',
                        'unique' => true
        ),
);

and in your view :

foreach ($posts as $post):
       if ($this->Session->check(Auth.User)):
              // Loop through the posts favorites
              foreach($post['Favorite'] as $favorite):
                    if ($favorite['user_id'] == $this->Session-
>read('Auth.User.id')):
                        /***** ITS YOUR FAVORITE!!!! *****/
                    else:
                        /*** BUTTON TO ADD TO FAVORITES ***/
                    endif;
              endforeach;
       else:
        /*** BUTTON TO ADD TO FAVORITES ***/
       endif;
endforeach;



On 1 déc, 01:04, andrewperk <andrewp...@gmail.com> wrote:
> Hello, I'm having a hard time displaying different text in a view
> depending on if a user who is logged in has marked the post as a
> favorite or not.
>
> Users can have many posts, posts belong to users.
> Users can have many favorites, favorites belong to users and posts.
>
> In my index view I loop through all posts and display the post.
>
> Inside that I loop, I then loop through all the favorites of that
> post. Remember sometimes a post may not have any favorites so the
> $post['Favorite] array might be empty. I want to display different
> text if the post is a favorite of the logged in user. I do this by
> comparing the current favorite's user_id to the logged in user's id.
>
> Where I run into problems is when a post is marked as a favorite by 2
> or more users. When this happens the Favorite array for the post
> associations contains multiple users and thus I end up with both of my
> different texts being displayed instead of just one or the other.
>
> So if the post is a favorite I want to tell the user its already their
> favorite.
>
> If the post is not a favorite, I want to display a button so they can
> add it as a favorite.
>
> And if they are not logged in, they get a link describing to them what
> favorites are.
>
> Here's my view code:
>
> *******************************
>
> <?php foreach ($posts as $post): ?>
> <?php
>     // Check for logged in user
>     if ($logged_in): ?>
>        // Loop through the posts favorites
>         <?php foreach($post['Favorite'] as $favorite): ?>
>               <?php
>                      // If favorite matches user id tell them its a
> favorite
>                      if ($favorite['user_id'] == $current_user['id']): ?>
>                             ***** ITS YOUR FAVORITE!!!! *****
>                     <?php
>                     else: ?>
>                         *** BUTTON TO ADD TO FAVORITES ***
>                     <?php endif; ?>
>        <?php endforeach; ?>
> <?php
> // Favorites array might be empty
>  if (empty($post['Favorite'])): ?>
>            *** BUTTON TO ADD TO FAVORITES ***
> <?php endif; ?>
> <?php else: ?>
>     // No logged in user. Display link to describe what favorites are
>     *** LINK Favorites Description ***
> <?php endif; ?>

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php

Reply via email to