我正在使用cakephp,我希望在我的userController中.php从另一个表中检索数据(ingredient_aliases)。这是成分别名的模型
class IngredientAlias extends AppModel {
public $name = 'IngredientAlias';
public $useTable = 'ingredient_aliases';
public $belongsTo = array(
'Ingredient' => array(
'className' => 'Ingredient',
'foreignKey' => 'ingredient_id'
)
);
...
我想从我的用户控制器中检索表ingredient_aliases(最近创建的 10 个)中的数据.php
我已经尝试过这种模式:
$this->loadModel('IngredientAlias', 2);
$ingg = $this->IngredientAlias->read();
$options['joins'] = array(
array('table' => 'ingredient_aliases',
'alias' => 'ing',
'type' => 'LEFT',
'foreignKey' => 'ing.user_id',
'order' => 'ing.created DESC',
'limit' => 3,
'conditions' => array(
'ing.user_id = User.id'
)
)
);
$options['conditions'] = array( );
$this->set('ingredient',$this->IngredientAlias->find('all', $options));
但这给了我这个错误:错误: SQLSTATE[42S22]: 找不到列: 1054 "on 子句"中的未知列"User.id"
有人可以帮助我吗?谢谢
您可以使用以下命令简单地实例化任何控制器或组件上的另一个模型(表)。
$data = ClassRegistry::init('IngredientAlias')->find('first', array());
就是这样。您将能够访问任何表上的任何数据。