2013-04-28 22 views
0

我在Cakephp中遇到了HABTM問題。使用HABTM時出現「Missing Database Table」錯誤

有兩種模式:用戶(音樂家)和流派。 有三個表格:用戶,流派,genres_users(id(主要),genre_id,user_id)。

中寫道用戶:

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

但作爲結果我得到 「丟失的數據庫表」。 我在做什麼錯?

謝謝。

UPD 用戶模型

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', 
     ), 
    ); 
} 

類型模型:

class Genre extends AppModel { 

    public $primaryKey = 'id'; 

} 
+1

可能是一個愚蠢的問題,該表沒有其報告爲失蹤? – 2013-04-28 11:02:36

+0

@Sam Delaney,找不到模型AppModel的數據庫表app_models。如果刪除habtm塊,此錯誤消失。 – biosvs 2013-04-28 11:26:04

+0

這聽起來像是一個配置錯誤給我。 'AppModel'是所有應用程序模型的父類,可以繼承,您不需要直接與它進行交互。我猜你不知道,但框架似乎堅持。您的HABTM配置看起來與文檔一致,唯一可以幫助我們提供解決方案的是如果您發佈'User'和'Genre'模型類。這樣我們檢查是否有其他地方可能配置不正確。 – 2013-04-28 11:43:29

回答

0

答案很簡單。

誤差在AppModel

它是:

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

必須是:

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