On Sun, Jun 17, 2012 at 2:50 PM, JonStark <[email protected]> wrote:
> Ok, almost there, just need a little help with Set::
>
> I'm using
>
> Set::extract('/Follower/UsersUser', $user)
>
> to get all the data from the association table of a given user, wich when
> debugged produces an array like this one :
>
> array(
> (int) 0 => array(
> 'UsersUser' => array(
> 'id' => '36',
> 'follower_id' => '1',
> 'following_id' => '2'
> )
> ),
> (int) 1 => array(
> 'UsersUser' => array(
> 'id' => '37',
> 'follower_id' => '3',
> 'following_id' => '2'
> )
> )
> )
>
> Now, the only thing left is to find which Set:: to use :
>
> Indeed, I just have to check if any array contains a 'follower_id' wich is
> equal to $this->Session->read('Auth.User.id'), and if so, return the
> associated 'id' value. (wich will be passed to the delete button).
I might b wrong but I think you have this backwards. The
Following/Follower arrays will contain all of the other users which
the *current* user is following or being followed by, respectively. So
by definition, Auth.User.id will be found in every entry in both
arrays (ID #2 in the example above).
So, let's say you have an array of all users and are listing their
names. If the current user is following one of them you want to
include an "unfollow" link which will delete the relationship. What
you need to do is look for the ID of the other user in the Following
array.
For example, if your users are in a $data array which look slike this:
array(
'User' => array(
'id' => some_id
...
),
...
);
$followers = $this->Session->read('User.follower');
$followings = $this->Session->read('User.following');
foreach($data as $d)
{
// print the name and whatever other info ...
$follower = Set::extract('/UsersUser[follower_id='.$d['User']['id'],
$followers);
if (!empty($follower))
{
// create delete link, passing $follower['id'] to delete action
}
else
{
// create link to follow, passing $follower['id'] to add action
}
}
--
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
[email protected] For more options, visit this group at
http://groups.google.com/group/cake-php