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

Reply via email to