1

How can i use cakephp 2X model hasone or other association concept here to execute the find query.

My two tables

In my Schinfo.php model is

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'
        ]
    ];
}

With the above I got

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

Now I need to change

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)

to

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)

to get my desire output

ndm
  • 59,784
  • 9
  • 71
  • 110

1 Answers1

0

You want to define three relationships, Country, State and City and specify the className for each to be the model you are linking to, for example Location. You can then also specify the column you will be using as the foreign keys in your Student model using foreignKey:-

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

Then when it comes to finding the results you can use contain like:-

$students = $this->Student->find('all', [
    'contain' => ['Country', 'State', 'City']
]);
drmonkeyninja
  • 8,490
  • 4
  • 31
  • 59
  • tnxs for your quick response. Please chk my question as I updated this. I attached my table relation view. masterLocation -> loc_id is linked to 3 column of schoolInfo table – Dhiraj Kumar Mohapatra Jul 21 '17 at 13:57
  • Is there a reason why you are not using CakePHP naming conventions for your table columns? It makes things more hard work. – drmonkeyninja Jul 21 '17 at 14:13
  • I m no using any naming conventions till now, but with your code its help me to resolve my query upto 80%. but still their is a problem. I m updatting my table structure & model code snipet. plz chk and let me know, what i need to do more. – Dhiraj Kumar Mohapatra Jul 21 '17 at 15:04