【发布时间】:2017-11-14 17:28:07
【问题描述】:
我有一个 articles 表,其中有一列 tagging_id 需要引用名为 tracking_categories 的表中的 id 列作为外键。
我的Article.php 文件包含以下内容:
class Article extends AppModel {
public $controllerPath = 'articles';
public $useTable = 'articles';
var $binds = array(
'TrackingCategory' => array(
'bindType' => 'belongsTo',
'className' => 'TrackingCategory',
'foreignKey' => 'tagging_id'
),
'Channel' => array(
'bindType' => 'belongsTo',
'className' => 'Channel',
'foreignKey' => 'channel_id'
)
);
我的TrackingCategory.php 看起来像这样:
class TrackingCategory extends AppModel {
public $useTable = 'tracking_categories';
我在Article.php 继承的AppModel 中有以下内容:
function expects($binds, $reset = false) {
if (func_num_args() > 2 || !is_bool($reset)) {
throw new InvalidArgumentException('Did you mean to pass an
array of binds?');
}
if (!is_array($binds)) {
$binds = array($binds);
}
foreach ($binds as $bind) {
if (isset($this->binds[$bind])) {
$tmp = array($this->binds[$bind]['bindType'] => array($bind => $this->binds[$bind]));
$this->bindModel($tmp, $reset);
}
}
}
我收到以下错误:
Database Error
Error: SQLSTATE[42S22]: Column not found: 1054 Unknown column 'Article.tracking_category_id' in 'field list'
它仍在寻找通过 CakePHP 的命名约定解析的列,而不是我通过 foreignKey 值明确设置的名称。我在这里错过了什么?
【问题讨论】:
标签: mysql cakephp foreign-keys cakephp-2.0