简体   繁体   English

CakePHP使用逗号分隔的ID加入

[英]CakePHP join with comma separated ids

Here I want to join two table with comma separated ids 在这里我想用逗号分隔的ID将两个表连接起来

For example my data is like: 例如我的数据是这样的:

[Restaurant] => Array
(
    [RST_ID] => 171
    [RST_NAME] => oneone
    [RST_IMAGE] => 
    [RST_CAT_ID] => 2,4,6
    [RST_CT_ID] => 27
    [RST_IS_TOP] => 3
    [RST_QR_CODE] => 
    [RST_CREATED_DATE] => 1394536725
    [RST_MODIFIED_DATE] => 1394536725
    [RST_STATUS] => 1
)

[Category] => Array
(
    [CAT_ID] => 2
    [CAT_NAME] => Vegetarian
    [CAT_CREATED_DATE] => 1375175962
    [CAT_MODIFIED_DATE] => 1375175962
    [CAT_STATUS] => 1
)

My Model Code: 我的模型代码:

 var $belongsTo = array(
        'Category' => array(
           'className' => 'Category',
           'foreignKey' => 'RST_CAT_ID',
           'conditions' => array('Category.CAT_ID IN ( Restaurant.RST_CAT_ID)')
        )
);

Real Query: 实际查询:

SELECT 
  `Restaurant`.`RST_ID`, `Restaurant`.`RST_NAME`, `Restaurant`.`RST_IMAGE`,
  `Restaurant`.`RST_CAT_ID`, `Restaurant`.`RST_CT_ID`, `Restaurant`.`RST_IS_TOP`,
  `Restaurant`.`RST_QR_CODE`, `Restaurant`.`RST_CREATED_DATE`,
  `Restaurant`.`RST_MODIFIED_DATE`, `Restaurant`.`RST_STATUS`, 
  `Category`.`CAT_ID`, `Category`.`CAT_NAME`, `Category`.`CAT_CREATED_DATE`,
  `Category`.`CAT_MODIFIED_DATE`, `Category`.`CAT_STATUS`, `City`.`CT_ID`,
  `City`.`CT_NAME`, `City`.`CT_CREATED_DATE`, `City`.`CT_MODIFIED_DATE`,
  `City`.`CT_STATUS` 
FROM `dailybit_dailybites`.`restaurant` AS `Restaurant`
LEFT JOIN `dailybit_dailybites`.`category` AS `Category` 
       ON (`Restaurant`.`RST_CAT_ID` = `Category`.`CAT_ID` 
           AND `Category`.`CAT_ID` IN ( `Restaurant`.`RST_CAT_ID`))
LEFT JOIN `dailybit_dailybites`.`city` AS `City` 
       ON (`Restaurant`.`RST_CT_ID` = `City`.`CT_ID`)  
WHERE 1 = 1

So what's the solution here? 那么这里的解决方案是什么?

It's giving me just one category data that for first id only. 它只给我一个类别数据,仅用于第一个ID。

First have a look at this question: MySQL search in comma list 首先看看这个问题: MySQL搜索逗号列表

As you can see the belongsTo query is just generating a join on the single id, CakePHP by default doesn't respect this special case. 如您所见,belongsTo查询只是在单个id上生成联接,默认情况下CakePHP不遵守这种特殊情况。 You will have to alter your query and pass all the ids manually, but your DB design is bad and it doesn't follow the CakePHP conventions at all. 您将不得不更改查询并手动传递所有id, 但是您的数据库设计很糟糕, 并且根本不遵循CakePHP约定

  • How do you prevent duplicates (which would waste space) 如何防止重复(这会浪费空间)
  • How do you remove a given value (Requires custom function, leading to possibility of errors? 如何删除给定值(需要自定义功能,导致出现错误的可能性?
  • How do you respond to performance issues as the size of my tables increase? 随着表的大小增加,您如何应对性能问题?

Instead of changing the query you should change this awkward DB design. 而不是更改查询,您应该更改此笨拙的数据库设计。 You want to use HABTM here and a join table : Restaurant hasAndBelongsToMany Categoryy. 您要在此处使用 HABTM 联接 :餐厅hasAndBelongsToMany Categoryy。

restaurants <-> restaurants_categories <-> categories 餐厅<-> restaurant_categories <->类别

If you insist on using this bad DB design you'll have to use bindModel() and set the conditions manually: 如果您坚持使用这种不良的数据库设计,则必须使用bindModel()并手动设置条件:

'conditions' => array('FIND_IN_SET (Category.CAT_ID, ' . $listOfIds. ')')

I haven't tested this, try it yourself, see FIND_IN_SET() vs IN() 我尚未测试过,请自己尝试,请参阅FIND_IN_SET()vs IN()

You'll have to have another method that gets you all the ids you want here before. 您将必须有另一种方法来获取您之前想要的所有ID。 Like I said, this is ineffectice and bad design. 就像我说的那样,这是无效的并且是不良的设计。

You have to set your foreign Key false and find_in_set condition 您必须将外键设置为false和find_in_set条件

 var $belongsTo = array(
        'Category' => array(
           'className' => 'Category',
           'foreignKey' => false,
           'conditions' => array('FIND_IN_SET(Category.CAT_ID,Restaurant.RST_CAT_ID)')
        )
); 

// you can pass an array at the place of 'Restaurant.RST_CAT_ID' //您可以在“ Restaurant.RST_CAT_ID”的位置传递一个数组

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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