如何在CakePHP中通过未按惯例命名的字段关联模型? [英] How to associate model in CakePHP by fields not named by convention?

查看:103
本文介绍了如何在CakePHP中通过未按惯例命名的字段关联模型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个表,其中有 username 。我如何可以指定本地和外表的字段名?

I have two tables with field username in both. How i can specify field name for both local and foreign table?

我想CakePHP会做一些像

I want CakePHP will do something like

ON (`T1`.`username` = `T2`.`username`)`


如果没有任何更改,表将会加入以下条件:

in result. Without any changes tables will be joined with following condition:

ON (`T1`.`id` = `T2`.`t1_id`)`

设置'foreign_key'='username '属性是不够的,因为它会产生这样的查询:

Setting 'foreign_key' = 'username' property is not enough because it will produce query like this:

ON (`t1`.`id` = `t2`.`username`)`

我有两个解决方案。
第一个是使用'join'属性和联接表。在这种情况下,我可以设置本地和外地字段。但是如果我需要连接更多的表到那个,手动加入,我不能使用包含任何更多我需要手动编写以下连接,即使关联设置正确。
所以我需要每次写入长连接定义,而只是使用'contains'=>数组('T1','T2','T3')

I have two solutions. First one is use 'join' property and join table on the fly. In such case i can set both local and foreign field. But if i need to join more tables to that one, joined manually, i can't use contain anymore i need to write following joins manually even if that associations was set correctly. So i need to write long join definitions every time instead just use 'contain' => array('T1', 'T2', 'T3')

第二是将表的primary_key它可以在模型文件或运行时完成。在我的情况下,它不能在模型中完成,因为该表还通过其'id'字段具有正确关联。
设置它运行时是这样,但我不喜欢它,因为它不明显,看起来像一个黑客。

Second is to set 'primary_key' of table to corresponding field. It can be done in model file or in runtime. In my case it can not be done in model because that table also have "correct" association by its 'id' field. Setup it runtime is the case but i dislike it because it's not obvious and looks like a hack.

当我问这个问题,我想我错过了一些明显但现在我明白,CakePHP只是不能这样做。所以我开始一个赏金希望有人分享的解决方案。如果不是我将尝试读取蛋白源和重新定义模型一些方法添加能力定义'foreign_key'在关联定义中的局部字段。

When i ask this question i thought i missing something obvious but now i understand that CakePHP just can't do that. So i started a bounty hoping that somebody share solution. If not i will try to read cake sources and redefine model some of method to add ability to define local field near the 'foreign_key' in association definition.

推荐答案

ForeignKey false



要具有不使用相关主键的关联连接条件下的模型 - 标准的做法是使用'foreignKey'=> false

而这个关联:

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
        )
    );
}

将生成此sql:

SELECT ... LEFT JOIN profiles on ON (Profile.id = Comment.profile_id)

指定一个foreignKey不是这样使用的:

Specifying that a foreignKey isn't to be used like so:

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
            'foreignKey' => false
        )
    );
}

将产生此(无效)sql:

Will produce this (invalid) sql:

SELECT ... LEFT JOIN profiles on ON ()


b $ b

从这一点,可以使用条件数组键指定所需的条件:

From this point, the desired conditions can be specified using the conditions array key:

class Comment extends AppModel {
    public $belongsTo = array(
        'Profile' => array(
            'foreignKey' => false,
             'conditions' => array(
                 'Comment.username = Profile.username'
             ),
        )
    );
}

(注意,条件定义为字符串) p>

(Note that the conditions are defined as a string) resulting in:

 SELECT ... LEFT JOIN profiles on ON (Comment.username = Profile.username)

这篇关于如何在CakePHP中通过未按惯例命名的字段关联模型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆