简体   繁体   English

CakePHP-跨多个数据库关联模型

[英]CakePHP - Associating models across multiple databases

I am using two models, each on a different database, but with the same server log in info (host, user and password) 我正在使用两个模型,每个模型都在不同的数据库上,但是使用相同的服务器登录信息(主机,用户和密码)

Model Consumer (Database A) HasMany ConsumerOrder 型号消费者 (数据库A) 的hasMany ConsumerOrder

Model ConsumerOrder (Database B) BelongsTo Consumer 消费者 订单模型(数据库B) 属于消费者

I've done all associations, but at the moment that I run $this->ConsumerOrder->fetch('all'); 我已经完成了所有关联,但是现在我运行$this->ConsumerOrder->fetch('all'); it is not writing the join, I think it is simply putting a prefix to the name of the database tables, but I don't know how to do it, because it should to change the core. 它不是在编写联接,我认为它只是在数据库表的名称前添加前缀,但是我不知道该怎么做,因为它应该更改核心。

Any ideas? 有任何想法吗?

SOLVED : 解决

I commented the line 1064 in the file "lib / Model / Datasource / DboSource.php" 我在文件“ lib / Model / Datasource / DboSource.php”中注释了第1064行

The line kept CakePHP to generate the joins as the name of the databases are different. 这行代码使CakePHP可以生成联接,因为数据库的名称不同。

if ($model->useDbConfig === $linkModel->useDbConfig) { } if ($model->useDbConfig === $linkModel->useDbConfig) { }

Please check have you import Model in controller or not. 请检查是否在控制器中导入了模型。 if not so please import model like that.. 如果不是这样,请导入这样的模型。

$ConsumerOrder= ClassRegistery::init('ConsumerOrder');

After you can write : 写完之后:

$variable = $ConsumerOrder->fetch('all');
print_r($variable);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM