如何在cakephp查找方法中使用“find_in_set”

问题描述 投票:0回答:2

我有一个表,其中逗号分隔另一个表的 id 我想在 cakephp 中以正确的形式使用 find 函数使用以下查询

"select * from special_offers where find_in_set('".$storeId."', stores) and user_id = '". $userId ."'";
cakephp
2个回答
13
投票

像这样使用

$data = $this->SpecialOffer->find('all',array('conditions' => array('SpecialOffer.user_id' => $userId ,'FIND_IN_SET(\''. $storeId .'\',SpecialOffer.stores1)')));

希望这可以帮助你


0
投票

为了避免SQL注入,也可以这样绑定参数:

$data = $this->SpecialOffer->find('all')
    ->where([
        'SpecialOffer.user_id' => $userId,
        'FIND_IN_SET(:storeId, SpecialOffer.stores1)',
    ])
    ->bind(':storeId', $storeId, 'integer');
© www.soinside.com 2019 - 2024. All rights reserved.