简体   繁体   中英

cakephp comma separated ids as foreign id

How Can I use comma separated ids as foreign key in cakephp, my NursingHome Model has these ids of Specialization Model NursingHome.specialization_ids=2,4,7 and I want there Respective Name using single query I am using this code:

class NursingHomeextends AppModel {
public 'hasMany' => array(
        'Specialization' => array(
           'className' => 'Specialization',
           'foreignKey' => false,
           'conditions' =>  'FIND_IN_SET(Specialization.id,NursingHome.specialization_ids)',
        ),
);
}

is there is any method to doing this?

first need to make the array for ids.

$storIds = array(2,4,7);  

Then you have 2 ways to execute this query.

first:

public 'hasMany' => array(
    'Specialization' => array(
       'className' => 'Specialization',
       'foreignKey' => false,
       'conditions' => 'FIND_IN_SET(\''. $storeIds .'\',Specialization.id)',
       )
    ),
);

second

'conditions' => array('Specialization.id' => $storIds)

I think you can do the following:

$ListOfIds = new array(1,2,3,4);

And then use it in conditions like this

'conditions' => array('Specialization.id' => $ListOfIds)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM