【问题标题】:CakePHP 2 - Two foreign keys of a table linked to the same single table primary keyCakePHP 2 - 链接到同一个表主键的表的两个外键
【发布时间】:2017-07-21 12:55:52
【问题描述】:

我如何在这里使用 cakephp 2X 模型 hasone 或其他关联概念来执行查找查询。

在我的 Schinfo.php 模型中是

class Schinfo extends AppModel {
    public $tablePrefix = 'sko_';
    public $hasOne = [
        'State' => [
            'className' => 'Masterlocation',
            'foreignKey' => 'master_locid'
        ],
        'City' => [
            'className' => 'Masterlocation',
            'foreignKey' => 'master_locid'
        ],
        'Area' => [
            'className' => 'Masterlocation',
            'foreignKey' => 'master_locid'
        ]
    ];
}

通过以上我得到了

SELECT
    `Schinfo`.`skool_id`,
    `Schinfo`.`skool_code`,
    `Schinfo`.`skool_name`,
    `Schinfo`.`skool_addr`,
    `Schinfo`.`master_state_id`,
    `Schinfo`.`master_city_id`,
    `Schinfo`.`master_area_id`,
    `Schinfo`.`skool_pin`,
    `Schinfo`.`skool_board`,
    `Schinfo`.`skool_type_id`,
    `Schinfo`.`skool_affilated_to`,
    `Schinfo`.`skool_affilated_no`,
    `Schinfo`.`skool_contact_no`,
    `Schinfo`.`skool_mailid`,
    `Schinfo`.`skool_website`,
    `Schinfo`.`skool_logo`,
    `Schinfo`.`skool_delete`,
    `State`.`master_locid`,
    `State`.`master_parentid`,
    `State`.`master_locname`,
    `State`.`is_checked`,
    `City`.`master_locid`,
    `City`.`master_parentid`,
    `City`.`master_locname`,
    `City`.`is_checked`,
    `Area`.`master_locid`,
    `Area`.`master_parentid`,
    `Area`.`master_locname`,
    `Area`.`is_checked` 
FROM
    `skoolata`.`sko_schinfos` AS `Schinfo` 
    LEFT JOIN
        `skoolata`.`sko_masterlocations` AS `State` 
        ON (`State`.`master_locid` = `Schinfo`.`id`) 
    LEFT JOIN
        `skoolata`.`sko_masterlocations` AS `City` 
        ON (`City`.`master_locid` = `Schinfo`.`id`) 
    LEFT JOIN
        `skoolata`.`sko_masterlocations` AS `Area` 
        ON (`Area`.`master_locid` = `Schinfo`.`id`) 
WHERE
    1 = 1

现在我需要改变

LEFT JOIN
    skoolata.sko_masterlocations AS State
    ON (State.master_locid = Schinfo.id) 
LEFT JOIN
    skoolata.sko_masterlocations AS City 
    ON (City.master_locid = Schinfo.id) 
LEFT JOIN
    skoolata.sko_masterlocations AS Area 
    ON (Area.master_locid = Schinfo.id)

LEFT JOIN
    skoolata.sko_masterlocations AS State
    ON (State.master_locid = Schinfo.master_state_id) 
LEFT JOIN
    skoolata.sko_masterlocations AS City 
    ON (City.master_locid = Schinfo.master_city_id) 
LEFT JOIN
    skoolata.sko_masterlocations AS Area 
    ON (Area.master_locid = Schinfo.master_area_id)

得到我想要的输出

【问题讨论】:

    标签: mysql cakephp cakephp-2.0 cakephp-2.9


    【解决方案1】:

    您想要定义三个关系,CountryStateCity,并为每个关系指定 className 作为您要链接到的模型,例如 Location。然后,您还可以使用foreignKey 指定将用作Student 模型中的外键的列:-

    public $hasOne = [
        'Country' => [
            'className' => 'Location',
            'foreignKey' => 'country_id'
        ],
        'State' => [
            'className' => 'Location',
            'foreignKey' => 'state_id'
        ],
        'City' => [
            'className' => 'Location',
            'foreignKey' => 'city_id'
        ]
    ];
    

    那么在查找结果时,您可以使用contain,例如:-

    $students = $this->Student->find('all', [
        'contain' => ['Country', 'State', 'City']
    ]);
    

    【讨论】:

    • tnxs 供您快速响应。当我更新这个时,请检查我的问题。我附上了我的表格关系视图。 masterLocation -> loc_id 链接到 schoolInfo 表的 3 列
    • 您没有为表列使用 CakePHP 命名约定是有原因的吗?它使事情变得更加困难。
    • 到目前为止,我没有使用任何命名约定,但使用您的代码可以帮助我解决高达 80% 的查询。但他们仍然是一个问题。我正在更新我的表结构和模型代码片段。请检查并告诉我,我还需要做什么。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 2011-11-11
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多