My bad. I wrote that example without thinking about it. Yes, it would
have to be in the controller because AuthComponent isn't available in
a view, of course.
What you could do is extract the following & follower IDs in the login
action and store them in the session. That's where Auth keeps the data
is returns with the user() method so there's a little bit of
duplication but not much. Especially as you'll only need to extract
the IDs once.
public function login()
{
// other stuff ...
$user = $this->Auth->user();
$this->Session->write(
'User.following',
Set::extract('/Following/id', $user)
);
$this->Session->write(
'User.follower',
Set::extract('/Follower/id', $user)
);
}
Then in you views you can do something like
if (in_array($some_user_id, $this->Session->read('User.following')))
{
...
}
On Fri, Jun 15, 2012 at 6:33 AM, JonStark <[email protected]> wrote:
> OK, I figured out this should go into the controller.
> But How can I use this function to alternate between two buttons follow /
> unfollow in the view ?
>
> public function checkIfFollow() {
> if (in_array(Set::extract('/Following/id', $this->Auth->user()),
> $id)) {
> // stop follow
> } else {
> //follow
> }
> }
>
> Many thanks for your time.
>
> Le jeudi 14 juin 2012 14:40:16 UTC+2, JonStark a écrit :
>>
>> I have a table with the following fields :
>>
>> id follower_id following_id
>>
>> So when a user clicks "follow" on an other user's profile, an entry like
>> this is created :
>>
>> 15 1 3
>>
>> meaning user with id 1 follows user with id 3.
>>
>> If user 3 follows 1, then an entry like
>>
>> 16 3 1
>>
>> is created.
>>
>> But, in order to prevent users for following many time a same user,
>> leading to entry such as :
>>
>> 17 3 1
>> 18 3 1
>> 19 3 1
>>
>> and so on, I want to replace "follow" by "unfollow" if the entry is
>> already saved.
>>
>> So my question is : how can i "scan" the table to check if an entry as
>> both X as follower_id AND Y as following_id ?
>>
>> Thanks a lot.
>
> --
> 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
--
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