0

Hello I'm having trouble setting up a simple HABTM in cakephp.

Please tell me what is wrong? I followed the documentation closely.

My controller (it's a REST service)

class UsersController extends AppController {

    public $components = array('RequestHandler');

    public function index() {
        $this->set(array(
            'users' => $this->User->find('all'),
            '_serialize' => array('users')
        ));
    }
}

My User.php model:

class User extends AppModel {

    public $hasAndBelongsToMany = array(
        'Sport' => array(
            'className' => 'Sport',
            'joinTable' => 'sports_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'sport_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
    );
}

My Sport.php Model

class Sport extends AppModel{

    public $hasAndBelongsToMany = array(
        'User' => array(
            'className' => 'User',
            'joinTable' => 'sports_users',
            'foreignKey' => 'sport_id',
            'associationForeignKey' => 'user_id',
            'unique' => true,
            'conditions' => '',
            'fields' => '',
            'order' => '',
            'limit' => '',
            'offset' => '',
            'finderQuery' => '',
            'with' => ''
        )
    );
} 

And this is how my DB looks like:

The users table:

enter image description here

The sports table:

enter image description here

Finally the sports_users table:

enter image description here

Lastly this is the error I'm getting:

Fatal error: Call to a member function schema() on a non-object in C:\wamp\www\SportsAround\lib\Cake\Model\Model.php on line 3627

Full image:

enter image description here

So where did I got it wrong? Thank you

Alejandro
  • 1,159
  • 3
  • 16
  • 30
  • Any one? I haven't solved it – Alejandro Sep 11 '14 at 15:20
  • not completely clear, the error you are getting is when you execute `$this->User->find('all')`, right? set recursive to -1 and try again, if it's the same error, the problem is not the habtm, but the user model alone (in that case, post all user model to see what's up) – Nunser Sep 11 '14 at 15:32
  • Hello, thank you for your answer. The code uploaded is my complete code. How do I set recursive to -1? Yes the problem comes when I call find('all') – Alejandro Sep 11 '14 at 15:39
  • That is in fact all the code I have in my app. Plus some lines in config files. – Alejandro Sep 11 '14 at 15:44
  • try doing `class User extends AppModel {public $hasAndBelongsToMany = array('Sport');}` and `class Sport extends AppModel{ public $hasAndBelongsToMany = array('User');}`, and before the `$this->set` put this `$users = $this->User->find('all', array('recursive'=>-1)); pr($users);`, to see what you've got – Nunser Sep 11 '14 at 15:47
  • Ok I'm driving... I will test it in about 20 mins and come back with an answer. Thank you so much for all your help – Alejandro Sep 11 '14 at 15:55
  • Ok ok, but *please*, don't text and drive o.o Receiving feedback for a comment isn't *that* urgent. – Nunser Sep 11 '14 at 15:59
  • Ok that solved it. I still don't know what was wrong before, but declaring the $hasAndBelongsToMany as a simple String array solved it. Thank you – Alejandro Sep 11 '14 at 17:04

1 Answers1

0

I solved it by changing

public $hasAndBelongsToMany = array(
    'Sport' => array(
        'className' => 'Sport',
        'joinTable' => 'sports_users',
        'foreignKey' => 'user_id',
        'associationForeignKey' => 'sport_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'with' => ''
    )
);

To

public $hasAndBelongsToMany = array('Sport');

And

public $hasAndBelongsToMany = array(
    'User' => array(
        'className' => 'User',
        'joinTable' => 'sports_users',
        'foreignKey' => 'sport_id',
        'associationForeignKey' => 'user_id',
        'unique' => true,
        'conditions' => '',
        'fields' => '',
        'order' => '',
        'limit' => '',
        'offset' => '',
        'finderQuery' => '',
        'with' => ''
    )
);

To

public $hasAndBelongsToMany = array('User');

But I still don't understand why that solved it.

Alejandro
  • 1,159
  • 3
  • 16
  • 30
  • If you follow CakePhp naming conventions you don't have to specify any options and just indicate the association as you did here. An incorrect setting that you applied where you didn't need to likely caused a problem. – Jage Jun 18 '15 at 15:56