【发布时间】:2012-08-25 04:07:40
【问题描述】:
我有一个使用 CakePHP 2.0 开发的网站。 我有一个包含许多表的数据库,我想关联两个表。我已经这样做了很多次,但是有两张桌子我不能这样做,我不知道为什么。 这是我的两张桌子:
成分别名:
id INT(10) UNSIGNED AUTO_INCREMENT
ingredient_id INT(10)
user_id INT(10)
alias VARCHAR(100) latin1_swedish_ci
活性成分
id INT(10) UNSIGNED
activity_id INT(11)
ingredient_id INT(10)
created DATETIME
ingredient_id 是我的外键,这是我的模型,我想将ingredient_id 设为我的外键。
class ActivityIngredients extends AppModel{
public $name = 'ActivityIngredients';
public $useTable = 'activity_ingredients';
public $belongsTo = array(
'IngredientAlias' => array(
'className' => 'IngredientAlias',
'conditions' => '',
'order' => '',
'foreignKey' => 'ingredient_id'
)
);
}
class IngredientAlias extends AppModel {
public $name = 'IngredientAlias';
public $useTable = 'ingredient_aliases';
public $belongsTo = array(
'Ingredient' => array(
'className' => 'Ingredient',
'foreignKey' => 'ingredient_id'
),
'User' => array(
'className' => 'User',
'foreignKey' => 'user_id'
)
);
public $hasMany = array (
'ActivityIngredients' => array (
'className' => 'ActivityIngredients',
'dependent' => true,
'foreignKey' => false,
'associatedKey' => 'ingredient_id'
)
);
当我在IngredientAlias 中创建变量的 var_dump 时,什么都没有,是空的,就像它不带外键一样。为什么?
问题出在关系上?
我试着写了
'foreignKey' => 'ingredient_id'
但没什么……一样
该查询是一个简单的 3 递归查询,我认为全选不是问题,而是与表的关系..
【问题讨论】:
标签: sql cakephp cakephp-model