0

I have a problem with HABTM in Cakephp.

There is two models: User (musicians) and Genre. And there is three tables: users, genres, genres_users (id (primary), genre_id, user_id).

Wrote in User:

<?php
public $hasAndBelongsToMany = array(
        'Genre' => array(
            'className' => 'Genre',
            'joinTable' => 'genres_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'genre_id',
        ),
    );

But as result i'm getting "Missing Database Table". What am I doing wrong?

Thanks.

upd User model

class User extends AppModel {

    public $primaryKey = 'id';

    public $hasOne = array(
        'PerformerProfile' => array(
            'className' => 'PerformerProfile',
            'dependent' => false,
        ),

        'OrganizerProfile' => array(
            'className' => 'OrganizerProfile',
            'dependent' => false,
        ),
    );

    public $hasAndBelongsToMany = array(
        'Genre' => array(
            'className' => 'Genre',
            'joinTable' => 'genres_users',
            'foreignKey' => 'user_id',
            'associationForeignKey' => 'genre_id',
            'unique' => 'keepExisting',
        ),
    );
}

Genre model:

class Genre extends AppModel {

    public $primaryKey = 'id';

}
biosvs
  • 11
  • 3
  • 1
    Possibly a silly question, which table does it report as missing? – Sam Delaney Apr 28 '13 at 11:02
  • @Sam Delaney, Database table app_models for model AppModel was not found. This error disappears if delete habtm block. – biosvs Apr 28 '13 at 11:26
  • This sounds like a configuration error to me. `AppModel` is a parent class for all your application's models to inherit from and you shouldn't need to interact with it directly. I'm guessing you don't but it appears the framework insists. Your HABTM configuration looks consistent with the documentation, the only other thing that may be able to help us with giving you a solution is if you post your `User` and `Genre` model classes. That way we check if something may be incorrectly configured elsewhere. – Sam Delaney Apr 28 '13 at 11:43
  • Post code of models. As for `hasOne` block, error does not disappear when remove it. – biosvs Apr 28 '13 at 11:53
  • Your models look fine. You could try and reduce the configuration of your HABTM to force it to fall back onto defaults. Clutching at straws, what method are you calling on your model to produce this error? – Sam Delaney Apr 28 '13 at 12:03
  • Default configuration gives the same result. Error appears with any methods. Even when i don't call any methods. Apparently, the fact of creating model object causes error. – biosvs Apr 28 '13 at 12:17
  • I'm sorry @biosvs, I don't think I'm going to be able to solve your problem given that this conversation is growing (not how SO should be used) and no-one else has contributed. What I recommend you do is have a look on GitHub for a [CakePHP boilerplate](https://github.com/neilcrookes/CakePHP-Blog-Plugin) which uses a HABTM relationship and compare it with your code. For live help, visit the CakePHP IRC channel where someone should be able to assist you in real-time. – Sam Delaney Apr 28 '13 at 13:05
  • @SamDelaney, thanks for trying to help and your time. – biosvs Apr 28 '13 at 13:24
  • Have you tried adding the corresponding HABTM association to the Genre model also? And this error happens when trying to use the `Users` controller or the `Genre` controller or both? – Nunser Apr 29 '13 at 14:29

1 Answers1

0

Answer is simple.

Error was in AppModel

It was:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct();

Must be:

public function __construct($id = false, $table = null, $ds = null) {
    parent::__construct($id, $table ,$ds);
biosvs
  • 11
  • 3