CakePHP:使用不同数据库关联两个模型?

发布于 2024-12-02 14:13:48 字数 1069 浏览 1 评论 0原文

我有两个模型,Plant 和 Emp,它们具有 Has And Belongs To Many 关系。我已将它们配置为关联,并且获取每个数据的查询是正确的,但问题是 Plant 和 Emp 位于不同的数据库上。 Emp 位于数据库 1 上,Plant 位于数据库 2 上。因此,它们无法正确查询连接表;连接表仅位于数据库 1 上。

当 Plant 模型尝试访问连接表时,它正在查询数据库 2,该数据库没有此数据。

这是 Emp 与 Plant 的关联。

var $hasAndBelongsToMany = array(
    'Plant' =>
        array(
            'className'              => 'Plant',
            'joinTable'              => 'emp_plant',
            'foreignKey'             => 'employee_id',
            'associationForeignKey'  => 'LocationID',
            'unique'                 => true,
            'conditions'             => '',
)
);

更新:我尝试设置一个“finderQuery”属性来让我查询连接表,但我不知道如何给出这样的原始 SQL 查询并允许它动态使用 id模型的实例而不是预定义的值。

我可以设置类似

SELECT * FROM [Plant] AS [Plant] JOIN [DB].[DBO].[empplant] AS 
[EmpPlant] ON ([EmpPlant].[employee_id] = **4** 
AND [EmpPlant].[ID] = [Plant].[LocationID]) 

Which 将为我提供 one 员工的正确数据,但我不知道如何使此 finderQuery 成为动态查询。必须有一种方法可以使其发挥作用。

I have two models, Plant and Emp, that have a Has And Belongs To Many relationship. I've configured them to be associated and the query to get the data for each is correct, but the problem is Plant and Emp are on different databases. Emp is on Database 1, Plant is on Database 2. Because of this they don't query the join table properly; the join table is only on Database 1.

When the Plant model tries to access the join table it's querying Database 2, which does not have this data.

This is the association Emp has for Plant.

var $hasAndBelongsToMany = array(
    'Plant' =>
        array(
            'className'              => 'Plant',
            'joinTable'              => 'emp_plant',
            'foreignKey'             => 'employee_id',
            'associationForeignKey'  => 'LocationID',
            'unique'                 => true,
            'conditions'             => '',
)
);

Update:I tried to set a "finderQuery" attribute to let me query the join table, but I don't know how to give a raw SQL query like that and allow it to dynamically use the id for the instance of the Model instead of a predefined value.

I can set something like

SELECT * FROM [Plant] AS [Plant] JOIN [DB].[DBO].[empplant] AS 
[EmpPlant] ON ([EmpPlant].[employee_id] = **4** 
AND [EmpPlant].[ID] = [Plant].[LocationID]) 

Which will give me the correct data for one employee, but I don't know how to make this finderQuery a dynamic query. There has to be a way for this to work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

绻影浮沉 2024-12-09 14:13:48

在你的模型类中尝试一下

var $useDbConfig = 'alternate';

Try

var $useDbConfig = 'alternate';

in your Model Class.

千仐 2024-12-09 14:13:48

我需要使用自定义 finderQuery 并使用特殊的 {$__cakeID__$} 标识符来代替匹配的模型 ID。这是上述示例的固定版本,设置为 $hasAndBelongsToMany 数组的关系条目中的查找器查询。

'finderQuery'=>'SELECT * FROM [Plant] AS [Plant] JOIN [DB].[DBO].[empplant] AS 
[EmpPlant] ON ([EmpPlant].[employee_id] = {$__cakeID__$} 
AND [EmpPlant].[ID] = [Plant].[LocationID])' 

这是可行的,但如果有人知道如何在没有自定义查找器查询的情况下解决这种情况(我试图通过使用关联来避免),请发布答案,我会标记为相反,正确。

I needed to use a custom finderQuery and use the special {$__cakeID__$} identifier in place of the model ID being matched. This is a fixed version of the sample above, set as the finder query in the relationship entry for the $hasAndBelongsToMany array.

'finderQuery'=>'SELECT * FROM [Plant] AS [Plant] JOIN [DB].[DBO].[empplant] AS 
[EmpPlant] ON ([EmpPlant].[employee_id] = {$__cakeID__$} 
AND [EmpPlant].[ID] = [Plant].[LocationID])' 

This works but if anyone knows how to fix this situation without a custom finder query (what I was trying to avoid by using associations) please post an answer and I will mark that correct instead.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文