首页 > 解决方案 > 无法加入两个数据库表 cakephp 3

问题描述

我正在使用名称为“default”和“default_history”的多个两个数据库。在“默认”数据库下,我在“默认历史”数据库下有名为“食谱”的表和“订单详细历史”。我有名为 OrderDetailHistoryTable 的模型表。这是我的模型表:-

public static function defaultConnectionName()  
{
    return 'default_history';
}

public function initialize(array $config)
{
    parent::initialize($config);

    $this->setTable('order_detail_history');
    $this->setDisplayField('id');
    $this->setPrimaryKey('id');

    $this->belongsTo('RecipesLeft', [
        'strategy' => 'select',
        'propertyName' => 'Recipes',
        'className' => 'Recipes',
        'foreignKey' => 'recipe_id',
        'joinType' => 'LEFT'
    ]);
}

我正在使用查询:-

$recipe_list_history = $this->OrderDetailHistory->find('list', ['keyField' => 'RecipesLeft.id', 'valueField' => 'RecipesLeft.recipe_name'])->select(['RecipesLeft.id', 'RecipesLeft.recipe_name'])->contain(['RecipesLeft'])->where(['OrderDetailHistory.order_id' => $history_id])->order(['OrderDetailHistory.id'])->hydrate(false)->toArray();

我收到了这个数据库错误:-

Column not found: 1054 Unknown column 'RecipesLeft.id' in 'field list'
SELECT RecipesLeft.id AS `RecipesLeft__id`, RecipesLeft.recipe_name AS `RecipesLeft__recipe_name` FROM order_detail_history OrderDetailHistory WHERE OrderDetailHistory.order_id = :c0 ORDER BY OrderDetailHistory.id

我已经在模型表中定义了关联,但我无法使用 order_detials_history 加入食谱表。!!

标签: cakephp-3.0

解决方案


看起来您的食谱表没有 id 列(主键)。你能发布你的三个表的数据库方案吗!


推荐阅读