php - cakephp 从另一个表中检索数据到 Controller 中

标签 php mysql cakephp mysql-error-1054

我正在使用 cakephp,我希望在我的 userController.php 中从另一个表 (ingredient_aliases) 检索数据。 这是 ingredientAlias 的模型

class IngredientAlias extends AppModel {
    public $name = 'IngredientAlias';
    public $useTable = 'ingredient_aliases';
    public $belongsTo = array(
        'Ingredient' => array(
            'className'    => 'Ingredient',
            'foreignKey'   => 'ingredient_id'
        )
    );
...

我想从我的 userController.php 中检索表 ingredient_aliases(最后创建的 10 个)的数据

我试过这种模式:

$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’

有人可以帮助我吗? 谢谢

最佳答案

您可以使用以下命令在任何 Controller 或组件上简单地实例化另一个模型(表)。

$data = ClassRegistry::init('IngredientAlias')->find('first', array());

就是这样。您将能够访问任何表上的任何数据。

关于php - cakephp 从另一个表中检索数据到 Controller 中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11281460/

相关文章:

php - 从表单上传将数据加载到文件中

php - ubuntu上CakePHP(带有捆绑软件)的最佳编辑器是什么

jquery - 如何使用CakePHP和jQuery实现跨域Ajax请求?

php - mysql_fetch_assoc - 不显示结果

php - Laravel Group By 和其他列的 Sum 总值

mysql - MySQL release_lock()是否阻塞?

mysql - 从同一个表中选择相同的数据,只是将条件作为不同的列

PHP PDO函数更新值插入错误

php - 在 CakePHP 中插入多个表

php - 在 PHP 中抛出 NotImplementedError?