【问题标题】:HABTM CakePHP no results for related modelHABTM CakePHP 没有相关模型的结果
【发布时间】:2015-06-16 08:45:04
【问题描述】:

我对 HABTM 模型有疑问。当我尝试获取任何相关模型 f.e.像这样:

$this->Tagi->find('first');

我没有得到任何关联模型的结果。结果如下所示:

array(
    'Tagi' => array(
        'id' => '1',
        'nazwa' => 'sth'
        ),
    'Instytucje' => array()
)

我确定应该有结果,我已经仔细检查过,甚至

$this->Tagi->getDataSource()->getLog(false, false) 

显示正确的查询,获取正确的结果。 如果您有任何想法,请给我一个提示。

塔吉模型:

public $hasAndBelongsToMany = array(
    'Instytucje' =>
        array(
            'className' => 'Instytucje.Instytucje',
            'joinTable' => 'instytucje-tagi',
            'foreignKey' => 'tag_id',
            'associationForeignKey' => 'instytucja_id',
            'unique'=> true
        )
);

Instytucje 模型:

public $hasAndBelongsToMany = array(
    'Tagi' =>
        array(
            'className' => 'Instytucje.Tagi',
            'joinTable' => 'instytucje-tagi',
            'foreignKey' => 'instytucja_id',
            'associationForeignKey' => 'tag_id',
            'unique'=> true
        )
);

编辑: 主要问题是HABTM引用AppModel导致错误:

Error: Table app_models for model AppModel was not found in datasource prod.

可以通过在AppModel中添加$useTable来绕过,这会导致之前的问题。

已解决 当使用远远超出此 Cake 用途的命名约定时,您已使用第三个模型,$useTable 指向参考表。 此外,正确地将 Cake 指向插件内的类也很重要。

【问题讨论】:

  • 您能否将生成的 SQL 包含在内,以便我们可以看到它的工作原理?您还检查过您的模型没有使用afterFind() 回调来更改数据吗?
  • 我检查了生成的 SQL,它获取了正确的结果:'SELECT Instytucje.id, Instytucje.nazwa, ****, AppModel.instytucja_id, AppModel 987654337 epf 987654339 @ $ 987654340 @ epf 987654341 instytucje-tagi AS AppModel ON(tag_id 987654345 @ AppModel 987654347 instytucja_id 987654347 instytucja_id 987654347 instytucja_id 987654347 instytucja_id987654348@.id) ' **** - 代表表格的其他字段(很长的列表)。
  • 您的查询将 Tagi 别名为 AppModel 是否有原因?这对我来说看起来不正确,可能是导致问题的原因。
  • 不知道,你知道它为什么这样做吗?它不是故意命名为 AppModel。
  • 它可能将 instytucje-tagi 别名为 AppModel,因为我没有显示“Instytucje”和“Tagi”之间关系的表格模型。

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


【解决方案1】:

我认为您的问题与您没有使用 CakePHP 的命名约定有关。从正在生成的查询的外观来看,Cake 不知道如何正确地为连接表设置别名,因此在您的查询中获得了 AppModel 别名。我怀疑这会在 Cake 尝试从查询构建结果数组时引起问题。

这可能行不通,但尝试为连接表创建一个名为InstytucjeTagi 的模型;然后使用with 键更新您的关联以使用它:-

塔吉模型:

public $hasAndBelongsToMany = array(
    'Instytucje' =>
        array(
            'className' => 'Instytucje.Instytucje',
            'joinTable' => 'instytucje-tagi',
            'with' => 'InstytucjeTagi',
            'foreignKey' => 'tag_id',
            'associationForeignKey' => 'instytucja_id',
            'unique'=> true
        )
);

Instytucje 模型:

public $hasAndBelongsToMany = array(
    'Tagi' =>
        array(
            'className' => 'Instytucje.Tagi',
            'joinTable' => 'instytucje-tagi',
            'with' => 'InstytucjeTagi',
            'foreignKey' => 'instytucja_id',
            'associationForeignKey' => 'tag_id',
            'unique'=> true
        )
);

如果这不能解决问题,请尝试使用 Tagi 模型中的 afterFind() callback 来检查 Cake 返回的内容:-

afterFind($results, $primary = false) {
    parent::afterFind($results, $primary);
    // Print out the $results to check what Cake is returning.
    debug($results);
    return $results;
}

【讨论】:

  • 不幸的是它不起作用。对关系表使用模型不会更改查询。 Cake 内部仍然使用 AppModel。此外,afterFind 回调不显示“Instytucje”模型的结果。首先是所有标签,然后将每个标签分开,最后标签与空的“Instytucje”数组合并。
  • 您是否更新了关联以使用新模型?您能否发布afterFind() 的结果,这可能有助于了解发生了什么。
  • 重要信息 - 它使用 AppModel,因为我在 AppModel 中使用了 $useTable。没有它我得到错误:在数据源产品中找不到模型 AppModel 的表 app_models。
  • @Maxiz 你不应该在AppModel 中设置$useTable。这意味着还有其他问题。由于您没有使用标准命名约定,您可能需要在每个模型中使用它来设置相关表。听起来您正在错误地使用AppModel,因为此错误意味着它正在被直接访问(它不应该)。 AppModel 用于放置希望由您的模型扩展的通用模型代码。
  • 每个模型都有对应的 $useTable 集。在使用 HABTM 之前一切正常,除了 HABTM 之外的所有关系类型。在 AppModel 中使用 $useTable 是让我进一步处理其他部分的技巧。 “Tagi”和“Insytytucje”两个模型都在扩展 AppModel。不知何故,HABTM 指的是 AppModel 类,即使 HABTM 已经定义了“with”参数。知道为什么没有正统命名约定的 HABTM 指的是 AppModel 吗?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多