【问题标题】:Error "Missing Database Table" when using HABTM使用 HABTM 时出现“缺少数据库表”错误
【发布时间】:2013-04-30 04:20:00
【问题描述】:

我在 Cakephp 中遇到了 HABTM 问题。

有两种模式:用户(音乐家)和流派。 并且有三张表:users,genres,genres_users(id(primary),genre_id,user_id)。

写于用户:

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

但结果我得到“缺少数据库表”。 我做错了什么?

谢谢。

更新 用户模型

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';

}

【问题讨论】:

  • 可能是一个愚蠢的问题,它报告缺少哪个表?
  • @Sam Delaney,找不到模型 AppModel 的数据库表 app_models。如果删除 habtm 块,此错误就会消失。
  • 这对我来说听起来像是一个配置错误。 AppModel 是所有应用程序模型的父类,您不需要直接与其交互。我猜你没有,但似乎框架坚持。您的 HABTM 配置看起来与文档一致,唯一可以帮助我们为您提供解决方案的另一件事是,如果您发布您的 UserGenre 模型类。这样我们就可以检查其他地方的配置是否有误。
  • 型号的邮政编码。至于hasOne块,删除后错误不会消失。
  • 您的模型看起来不错。您可以尝试减少您的 HABTM 的配置,以强制它回退到默认值。抓着稻草,你在你的模型上调用什么方法来产生这个错误?

标签: cakephp has-and-belongs-to-many


【解决方案1】:

答案很简单。

错误出现在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);

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2017-12-23
    • 1970-01-01
    • 2012-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-05-12
    相关资源
    最近更新 更多