cakephp-3.0 - 无法加入两个数据库表 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 加入食谱表。!!
解决方案
看起来您的食谱表没有 id 列(主键)。你能发布你的三个表的数据库方案吗!
推荐阅读
- user-accounts - useradd --non-unique 选项,重复的 uid 什么时候有用?
- android - Flutter:`Container`高度无法在`Stack`内部调整
- angular - Angular 8:安全干净地使用 ElementRef 和 Renderer2?
- sql - 需要将表上特定项目的所有字段值复制到 SQL Server 中的另一个项目
- jenkins - java.lang.NullPointerException:运行 Jenkins 管道时无法在空对象上调用方法 startsWith()
- c# - 反序列化非常大的图像文件
- sql - 根据其他列中的几个条件选择数据行
- database - 我可以使用 SCP 将数据从 Clickhouse 服务器下载到我的本地工作站吗?
- c# - .net转换/绑定前如何获取请求原始参数?
- python - Plotly:为什么 px.line 不会在 google colab 中显示这个数字?