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

查看:36
本文介绍了如何通过未按约定命名的字段关联 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"属性并动态连接表.在这种情况下,我可以设置本地和外国字段.但是如果我需要将更多的表加入到那个表中,手动加入,我不能再使用包含我需要手动编写以下连接,即使该关联设置正确.所以我每次都需要写长连接定义,而不是使用 'contain' =>数组('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' =>错误.

即而这个关联:

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

会生成这个sql:

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

指定不能像这样使用外键:

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 ()

从这里开始,可以使用条件数组键指定所需的条件:

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'
             ),
        )
    );
}

(注意条件定义为字符串)导致:

(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天全站免登陆