Re: Saving relationships

2012-01-23 Thread Sam Sherlock
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.  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+unsubscr...@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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Re: Saving relationships

2012-01-23 Thread jeremyharris
I suggest you read this article:  
http://nuts-and-bolts-of-cakephp.com/2008/07/11/notes-on-cakephp-habtm-part-2-saving-data/

At the end you'll see a behavior where you can save one HABTM link at a 
time. Generally, you save all of the HABTM data because Cake wipes all of 
the current relationships and resaves them. That's the only way it knows 
not to get duplicates, or that you removed them. The HABTM behavior helps 
you save/delete single ones without needing to know the others, i.e., 
saving/removing one follower without knowledge of the other followers.

Your follow button should go to something like /users/follow which in turn 
uses (probably POSTed so you don't have spamming) data and the HABTM 
behavior to save that new follower.

-- 
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php


Saving relationships

2012-01-22 Thread J.
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+unsubscr...@googlegroups.com For more options, visit this group at 
http://groups.google.com/group/cake-php