Monday, January 23, 2012

Re: Saving relationships

your relations are in the controller - they should be in the model

you don't need to loadModel follower as User hasMany followers
 - S




On 22 January 2012 16:20, J. <contact.mysparetime@gmail.com> wrote:
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

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