HABTM CakePHP 没有相关模型的结果
HABTM CakePHP no results for related model
我对 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
)
);
研究所型号:
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
指向参考 table。
此外,正确地将 Cake 指向插件内的 类 很重要。
我认为您的问题与您没有使用 CakePHP 的命名约定有关。从正在生成的查询的外观来看,Cake 不知道如何正确地为连接 table 添加别名,因此在您的查询中得到一个 AppModel
别名。我怀疑这会导致 Cake 尝试从查询构建结果数组时出现问题。
这可能行不通,但请尝试为名为 InstytucjeTagi
的联接 table 创建一个模型;然后使用 with
键更新您的关联以使用它:-
塔吉模型:
public $hasAndBelongsToMany = array(
'Instytucje' =>
array(
'className' => 'Instytucje.Instytucje',
'joinTable' => 'instytucje-tagi',
'with' => 'InstytucjeTagi',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'instytucja_id',
'unique'=> true
)
);
研究所型号:
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;
}
我对 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
)
);
研究所型号:
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
指向参考 table。
此外,正确地将 Cake 指向插件内的 类 很重要。
我认为您的问题与您没有使用 CakePHP 的命名约定有关。从正在生成的查询的外观来看,Cake 不知道如何正确地为连接 table 添加别名,因此在您的查询中得到一个 AppModel
别名。我怀疑这会导致 Cake 尝试从查询构建结果数组时出现问题。
这可能行不通,但请尝试为名为 InstytucjeTagi
的联接 table 创建一个模型;然后使用 with
键更新您的关联以使用它:-
塔吉模型:
public $hasAndBelongsToMany = array(
'Instytucje' =>
array(
'className' => 'Instytucje.Instytucje',
'joinTable' => 'instytucje-tagi',
'with' => 'InstytucjeTagi',
'foreignKey' => 'tag_id',
'associationForeignKey' => 'instytucja_id',
'unique'=> true
)
);
研究所型号:
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;
}