Quantcast

Check table for a particular entry

classic Classic list List threaded Threaded
22 messages Options
12
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Check table for a particular entry

JonStark
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

Steve Found
You would probably be better off checking if the entry already exists
BEFORE creating a duplicate, then deleting it ?

That way you could have a 'Follow' if the entry does not exist and an
'Unfollow' if it does exist.

Assuming this is a HABTM relationship, you would just need to query the
following model then look in the follower relationship to see if the
users ID exists.

Steve (Ratty)


On 14/06/12 13:40, JonStark wrote:

> 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
> [hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
Assuming this is a HABTM relationship, you would just need to query the 
following model then look in the follower relationship to see if the 
users ID exists. 

YES this is exactly what I want to achieve, but I'm really new to PHP/Cake, I can't figure out how to check this.

My user array looks like :

/app/Controller/UsersController.php (line 28)
array(
	'User' => array(
		'password' => '*****',
		'id' => '1',
		'username' => 'testuser',
		'email' => '@',
		'hash' => '',
		'created' => '2012-06-12 20:49:06'
	),
	'Following' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
			'email' => '@',
			'hash' => '',
			'created' => '2012-06-13 21:03:01',
			'UsersUser' => array(
				'id' => '23',
				'follower_id' => '1',
				'following_id' => '2'
			)
		),
		(int) 1 => array(
			'password' => '*****',
			'id' => '3',
			'username' => 'test',
			'email' => 'test',
			'hash' => '',
			'created' => '0000-00-00 00:00:00',
			'UsersUser' => array(
				'id' => '24',
				'follower_id' => '1',
				'following_id' => '3'
			)
		)
	),
	'Follower' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
'email' => '@', 'hash' => '', 'created' => '2012-06-13 21:03:01', 'UsersUser' => array( 'id' => '26', 'follower_id' => '2', 'following_id' => '1' ) ) ) )

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

Steve Found
Can you post what your models look like please ?


On 14/06/12 14:00, JonStark wrote:
Assuming this is a HABTM relationship, you would just need to query the 
following model then look in the follower relationship to see if the 
users ID exists. 

YES this is exactly what I want to achieve, but I'm really new to PHP/Cake, I can't figure out how to check this.

My user array looks like :

/app/Controller/UsersController.php (line 28)
array(
	'User' => array(
		'password' => '*****',
		'id' => '1',
		'username' => 'testuser',
		'email' => '@',
		'hash' => '',
		'created' => '2012-06-12 20:49:06'
	),
	'Following' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
			'email' => '@',
			'hash' => '',
			'created' => '2012-06-13 21:03:01',
			'UsersUser' => array(
				'id' => '23',
				'follower_id' => '1',
				'following_id' => '2'
			)
		),
		(int) 1 => array(
			'password' => '*****',
			'id' => '3',
			'username' => 'test',
			'email' => 'test',
			'hash' => '',
			'created' => '0000-00-00 00:00:00',
			'UsersUser' => array(
				'id' => '24',
				'follower_id' => '1',
				'following_id' => '3'
			)
		)
	),
	'Follower' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
			'email' => '@',
			'hash' => '',
			'created' => '2012-06-13 21:03:01',
			'UsersUser' => array(
				'id' => '26',
				'follower_id' => '2',
				'following_id' => '1'
			)
		)
	)
)

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
[hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
This is my User model :

<?php
class User extends AppModel {
    public $name = 'User';
    
    public $hasMany = 'Post';
    
    public $hasAndBelongsToMany = array(
      'Following' => array(
          'className' => 'User',
          'joinTable' => 'users_users',
          'foreignKey' => 'follower_id',
          'associationForeignKey' => 'following_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Following.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        ),
      'Follower' => array(
          'className' => 'User',
          'joinTable' => 'users_users',
          'foreignKey' => 'following_id',
          'associationForeignKey' => 'follower_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Follower.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        )
     );
    
}


My follow function in UsersController :

public function follow() {
    $this->User->bindModel(array('hasOne'=>array('UsersUsers')),false);
   if ($this->User->UsersUsers->save($this->request->data)) {
     $this->Session->setFlash('You are now following this user.');
     $this->redirect(array('controller' => 'users', 'action' => 'index'));
   }
   else {
     $this->Session->setFlash('Unable to follow this user.');
     $this->redirect(array('controller' => 'users', 'action' => 'index'));
   }
    }

And the Follow form :

<!-- Follow -->
<?php
echo $this->Form->create('User', array(
'class' => 'form-inline',
'action' => 'follow',
'inputDefaults' => array(
   'label' => false,
) ));

echo $this->Form->input('UsersUsers.follower_id',array(
'type' => 'hidden',
'value' => $this->Auth->user('id'),
));
echo $this->Form->input('UsersUsers.following_id',array(
'type' => 'hidden',
'value' => $user['User']['id'],
));

echo ("<input type='submit' class='btn btn-success' value='Follow'>"); ?>
<!-- End follow -->


Thanks a lot for your help

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

Steve Found
Hmmm...  I'm not convinced that Cake will handle a HABTM relationship with a model to itself very well as the table alias will be the same name in the SQL it generates.

I would try creating a class Other.php as :

class Otheruser extends AppModel {
    public $name = 'Otheruser';
    public $useTable = 'users';   
    public $hasMany = 'Post';
    
    public $hasAndBelongsToMany = array(
      'User' => array(
          'className' => 'User',
          'joinTable' => 'users_users',
          'foreignKey' => 'follower_id',
          'associationForeignKey' => 'following_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Following.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        ),
     );  
}

class User extends AppModel {
    public $name = 'User';
    public $hasMany = 'Post';
    
    public $hasAndBelongsToMany = array(
      'Otheruser' => array(
          'className' => 'Other',
          'joinTable' => 'users_users',
          'foreignKey' => 'following_id',
          'associationForeignKey' => 'follower_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Following.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        ),
     );  
}

That way Cake will use the aliases User and Otheruser in the generated query but both models are using the 'users' table.

I can't guarantee that this will work, but it would be worth a try.



On 14/06/12 14:23, JonStark wrote:
This is my User model :

<?php
class User extends AppModel {
    public $name = 'User';
    
    public $hasMany = 'Post';
    
    public $hasAndBelongsToMany = array(
      'Following' => array(
          'className' => 'User',
          'joinTable' => 'users_users',
          'foreignKey' => 'follower_id',
          'associationForeignKey' => 'following_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Following.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        ),
      'Follower' => array(
          'className' => 'User',
          'joinTable' => 'users_users',
          'foreignKey' => 'following_id',
          'associationForeignKey' => 'follower_id',
          'unique' => true,
          'conditions' => '',
          'fields' => '',
          'order' => 'Follower.username ASC',
          'limit' => '',
          'offset' => '',
          'finderQuery' => '',
          'deleteQuery' => '',
          'insertQuery' => ''
        )
     );
    
}


My follow function in UsersController :

public function follow() {
    $this->User->bindModel(array('hasOne'=>array('UsersUsers')),false);
   if ($this->User->UsersUsers->save($this->request->data)) {
     $this->Session->setFlash('You are now following this user.');
     $this->redirect(array('controller' => 'users', 'action' => 'index'));
   }
   else {
     $this->Session->setFlash('Unable to follow this user.');
     $this->redirect(array('controller' => 'users', 'action' => 'index'));
   }
    }

And the Follow form :

<!-- Follow -->
<?php
echo $this->Form->create('User', array(
'class' => 'form-inline',
'action' => 'follow',
'inputDefaults' => array(
   'label' => false,
) ));

echo $this->Form->input('UsersUsers.follower_id',array(
'type' => 'hidden',
'value' => $this->Auth->user('id'),
));
echo $this->Form->input('UsersUsers.following_id',array(
'type' => 'hidden',
'value' => $user['User']['id'],
));

echo ("<input type='submit' class='btn btn-success' value='Follow'>"); ?>
<!-- End follow -->


Thanks a lot for your help

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
[hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
I was wondering if there was a way to scan the array produced when looking at an user, in the section "Follower", to match id any ID == Auth.user.id, since arrays are like this :

/app/Controller/UsersController.php (line 28)
array(
	'User' => array(
		'password' => '*****',
		'id' => '1',
		'username' => 'testuser',
		'email' => '@',
		'hash' => '',
		'created' => '2012-06-12 20:49:06'
	),
	'Following' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
			'email' => '@',
			'hash' => '',
			'created' => '2012-06-13 21:03:01',
			'UsersUser' => array(
				'id' => '23',
				'follower_id' => '1',
				'following_id' => '2'
			)
		),
		(int) 1 => array(
			'password' => '*****',
			'id' => '3',
			'username' => 'test',
			'email' => 'test',
			'hash' => '',
			'created' => '0000-00-00 00:00:00',
			'UsersUser' => array(
				'id' => '24',
				'follower_id' => '1',
				'following_id' => '3'
			)
		)
	),
	'Follower' => array(
		(int) 0 => array(
			'password' => '*****',
			'id' => '2',
			'username' => 'anotertest',
'email' => '@', 'hash' => '', 'created' => '2012-06-13 21:03:01', 'UsersUser' => array( 'id' => '26', 'follower_id' => '2', 'following_id' => '1' ) ) ) )

Wouldn't this be easier ?

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
To match if*

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
In reply to this post by Steve Found
On Thu, Jun 14, 2012 at 9:59 AM, Steve Found <[hidden email]> wrote:
> Hmmm...  I'm not convinced that Cake will handle a HABTM relationship with a
> model to itself very well as the table alias will be the same name in the
> SQL it generates.

No, that's exactly the way to do it. Cake will use the alias -- not
the className -- for the SQL. In this case, it'll use
Follower/Following.

The className option seemed redundant to me at first; this explains
why it's there.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
In reply to this post by JonStark
You could extract all of the "Following" IDs.

$following_ids = Set::extract('Folllowing/id', $this->Auth->user());

Or:

if (in_array(Set::extract('Folllowing/id', $this->Auth->user()),
$some_user_id) {
   // already following
} else {
   ...
}

On Thu, Jun 14, 2012 at 10:03 AM, JonStark <[hidden email]> wrote:

> I was wondering if there was a way to scan the array produced when looking
> at an user, in the section "Follower", to match id any ID == Auth.user.id,
> since arrays are like this :
>
> /app/Controller/UsersController.php (line 28)
>
> array(
> 'User' => array(
> 'password' => '*****',
> 'id' => '1',
> 'username' => 'testuser',
> 'email' => '@',
> 'hash' => '',
> 'created' => '2012-06-12 20:49:06'
> ),
> 'Following' => array(
> (int) 0 => array(
> 'password' => '*****',
> 'id' => '2',
> 'username' => 'anotertest',
> 'email' => '@',
> 'hash' => '',
> 'created' => '2012-06-13 21:03:01',
> 'UsersUser' => array(
> 'id' => '23',
> 'follower_id' => '1',
> 'following_id' => '2'
> )
> ),
> (int) 1 => array(
> 'password' => '*****',
> 'id' => '3',
> 'username' => 'test',
> 'email' => 'test',
> 'hash' => '',
> 'created' => '0000-00-00 00:00:00',
> 'UsersUser' => array(
> 'id' => '24',
> 'follower_id' => '1',
> 'following_id' => '3'
> )
> )
> ),
> 'Follower' => array(
> (int) 0 => array(
> 'password' => '*****',
> 'id' => '2',
> 'username' => 'anotertest',
> 'email' => '@',
> 'hash' => '',
> 'created' => '2012-06-13 21:03:01',
> 'UsersUser' => array(
> 'id' => '26',
> 'follower_id' => '2',
> 'following_id' => '1'
> )
> )
> )
> )
>
>
> Wouldn't this be easier ?
>
> 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
> [hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
From what I understand, this method extracts the ID of the followed users from the AUTH data right ?

Can set::extract work with the array produced when finding/viewing an user ?

Le jeudi 14 juin 2012 19:31:27 UTC+2, cricket a écrit :
You could extract all of the "Following" IDs.

$following_ids = Set::extract('Folllowing/id', $this->Auth->user());

Or:

if (in_array(Set::extract('Folllowing/id', $this->Auth->user()),
$some_user_id) {
   // already following
} else {
   ...
}

On Thu, Jun 14, 2012 at 10:03 AM, JonStarkwrote:

> I was wondering if there was a way to scan the array produced when looking
> at an user, in the section "Follower", to match id any ID == Auth.user.id,
> since arrays are like this :
>
> /app/Controller/UsersController.php (line 28)
>
> array(
>         'User' => array(
>                 'password' => '*****',
>                 'id' => '1',
>                 'username' => 'testuser',
>                 'email' => '@',
>                 'hash' => '',
>                 'created' => '2012-06-12 20:49:06'
>         ),
>         'Following' => array(
>                 (int) 0 => array(
>                         'password' => '*****',
>                         'id' => '2',
>                         'username' => 'anotertest',
>                         'email' => '@',
>                         'hash' => '',
>                         'created' => '2012-06-13 21:03:01',
>                         'UsersUser' => array(
>                                 'id' => '23',
>                                 'follower_id' => '1',
>                                 'following_id' => '2'
>                         )
>                 ),
>                 (int) 1 => array(
>                         'password' => '*****',
>                         'id' => '3',
>                         'username' => 'test',
>                         'email' => 'test',
>                         'hash' => '',
>                         'created' => '0000-00-00 00:00:00',
>                         'UsersUser' => array(
>                                 'id' => '24',
>                                 'follower_id' => '1',
>                                 'following_id' => '3'
>                         )
>                 )
>         ),
>         'Follower' => array(
>                 (int) 0 => array(
>                         'password' => '*****',
>                         'id' => '2',
>                         'username' => 'anotertest',
>                         'email' => '@',
>                         'hash' => '',
>                         'created' => '2012-06-13 21:03:01',
>                         'UsersUser' => array(
>                                 'id' => '26',
>                                 'follower_id' => '2',
>                                 'following_id' => '1'
>                         )
>                 )
>         )
> )
>
>
> Wouldn't this be easier ?
>
> 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
> [hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
On Thu, Jun 14, 2012 at 1:41 PM, JonStark <[hidden email]> wrote:
> From what I understand, this method extracts the ID of the followed users
> from the AUTH data right ?

I thought the array you posted was the output of $this->Auth->user()
but maybe i misread that. When a given User logs in, their data is
kept in the session and is available through that method. If it
doesn't already contain the associated Users, you could add it to the
session in the login() method.

> Can set::extract work with the array produced when finding/viewing an user ?

Yes, pass the array as the 2nd param. Use debug($the_array) to figure
out the xpath to use for 1st param.

(It's Set, not set, btw)

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
This returned :

Notice (8): Undefined property: View::$Auth [CORE/Cake/View/View.php, line 806]

Fatal error: Call to a member function user() on a non-object in /app/View/Users/view.ctp on line 41

with ths code:

               <?php 
40
41 if (in_array(Set::extract('Following/id', $this->Auth->user()), 
$id)) { 
  echo("following already"); 
} else { 
echo("not following.");
?>

Any idea ?

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
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 <[hidden email]> 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
> [hidden email] 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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
I tried this, it's actually clever, but there's a flaw I guess.

Since the extract is done when logging in, the follow link will still appear for all the users you will follow after, and disappear only when re-logging in.

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.

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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
On Sat, Jun 16, 2012 at 6:44 AM, JonStark <[hidden email]> wrote:
> I tried this, it's actually clever, but there's a flaw I guess.
>
> Since the extract is done when logging in, the follow link will still appear
> for all the users you will follow after, and disappear only when re-logging
> in.

Just update each array as they are modified.

$following = $this-Session->read('User.following');
$following[] = $this->Following->lastInsertId();
$this->Session->write('User.following', $following);

Same idea if a person is removed.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
Thanks a lot Ratty and cricket, couldn't have managed it without your help.
For other people looking at this, I managed it this way to prevent multiple sessions reload, seems to work for now :

In UsersController, in the view function :

$followers = Set::extract('/Follower/id', $user);
$this->set('followers', $followers);

In User/view.ctp :

<?php 
if ($user['User']['id'] == $this->Session->read('Auth.User.id')) {
echo("<a class='btn disabled'>This is you</a>");
} elseif (in_array($this->Session->read('Auth.User.id'), $followers)) {
echo $this->element('unfollow');
} else {
echo $this->element('follow');
}
 ?>

Elements :

Follow.ctp :

<?php
echo $this->Form->create('User', array(
'class' => 'form-inline',
'action' => 'follow',
'inputDefaults' => array(
   'label' => false,
) ));
echo $this->Form->input('UsersUsers.follower_id',array(
'type' => 'hidden',
'value' => $this->Session->read('Auth.User.id'),
));

echo $this->Form->input('UsersUsers.following_id',array(
'type' => 'hidden',
'value' => $user['User']['id'],
));
echo ("<input type='submit' class='btn btn-success' value='Follow'>"); ?>
</form>


Only need to code the "unfollow" form now I guess, wich lead me to my initial question I guess, how can I locate and delate the database entry ?

Should look like this for example:

id follower_id following_id
X     1                 3



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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

lowpass
On Sat, Jun 16, 2012 at 12:43 PM, JonStark <[hidden email]> wrote:
>
> Only need to code the "unfollow" form now I guess, wich lead me to my
> initial question I guess, how can I locate and delate the database entry ?
>
> Should look like this for example:
>
> id follower_id following_id
> X     1                 3

The simplest way would be to store both the id and follow[er|ing]_id.
Then have your "unfollow" button pass the id to delete().

And you'd need to do in_array(array_values(...)) as well.

--
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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
Reply | Threaded
Open this post in threaded view
|  
Report Content as Inappropriate
star

Re: Check table for a particular entry

JonStark
In reply to this post by JonStark
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).

Any clue on doing this ? I'm reading the Cookbook page for Set:: but I really can't figure out.

Many thanks.


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
[hidden email] For more options, visit this group at http://groups.google.com/group/cake-php
12
Loading...