cakephp-3.0 - 关联来自不同数据库 cakephp 3.0 的表
问题描述
我有两个名为 default 和 default_history 的数据库。以及default数据库下名为users和wafer_detail_history的表和default_history数据库下的order_history。想要将 Users 表与 OrderHistory 表相关联。
订单历史表:-
public function initialize(array $config)
{
parent::initialize($config);
$this->setTable('order_history');
$this->setDisplayField('id');
$this->setPrimaryKey('id');
$this->addBehavior('Timestamp');
$this->hasMany('WaferDetailHistory', [
'foreignKey' => 'order_id'
]);
$this->belongsTo('Users', [
'foreignKey' => 'created_by',
'joinType' => 'INNER'
]);
}
我用这个。
$connection = ConnectionManager::get('default_history');
$this->OrderHistory = TableRegistry::get('OrderHistory');
$this->OrderHistory->setConnection($connection);
$id = 37;
$order_history = $this->OrderHistory->get($id, ['contain' => ['Users']]);
但无法成功。收到此错误:
未找到基表或视图:1146 表“default_history.users”不存在
解决方案
在 OrderHistoryTable.php 文件上试试这个:
$this->setTable('default_history.order_history');
推荐阅读
- shopify - 基于分组标签的相关产品会限制内存。使用带有特定句柄标签的集合
- python - 删除每一行熊猫python中每一列中的重复值
- go - go 模块是否意味着作为可执行程序或包安装?
- amazon-web-services - 如何将 AWS elasticsearch 中的生命周期模式应用于许多索引
- r - 我们可以与 R 中的 Google Devtools 应用程序元素进行交互吗?
- sql - 使用条件 AND 从 COUNT 中查找重复记录和折扣的 SQL 查询
- python - Python按两个元素排序,它可以在任何索引/反转
- css - 在 next.js 上首次渲染时,material-ui 无法构建类名 jss-*
- php - WP 更新插件数据库表
- flutter - Flutter中如何处理API统一返回格式