Sunday, January 22, 2012

Saving relationships

I'm making a follower system ala Twitter.

Here is my UsersController :


class UsersController extends AppController {

public $name = 'Users';
public $helpers = array('Html', 'Form', 'Paginator');
var $hasAndBelongsToMany = array(
'Followed' => array(
'className' => 'User',
'joinTable' => 'followed',
'foreignKey' => 'user_id',
'associationForeignKey' => 'followed_id',
),
'Follower' => array(
'className' => 'User',
'joinTable' => 'followers',
'foreignKey' => 'user_id',
'associationForeignKey' => 'follower_id',
)
);

My tables are :

followers , containing user_id and follower_id
followeds , containing user_id and followeds_id

And finally I use this in my UsersController :

public function view($id = null) {
$this->loadModel('Follower');
$followers = $this->set('followers', $this->Follower->find('all',
array('conditions' => array('Follower.user_id' => $id))));
$this->loadModel('Followed');
$followeds = $this->set('followeds', $this->Followed->find('all',
array('conditions' => array('Followed.user_id' => $id))));

I use foreach to display it in the view.


So when I set up numbers in the tables, everything works correctly,
but I don't know how to make a follow button on user view.
I'm discovering phh AND cake, and I have no idea how to do this.
Thanks a lot for your time.

--
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: