cakephp中的FIND_IN_SET无法正常工作

时间:2018-08-10 05:09:10

标签: php mysql cakephp-2.0 find-in-set

我有一张表,其中类别ID作为逗号分隔值存储在数据库中,因此我需要在此逗号分隔值中搜索另一个数组。

需要在$required_ids_array中搜索Posts.category_ids

$required_ids_array = Array (
        [0] => 14 
        [1] => 15 
        [2] => 16 
        [3] => 25 
        [4] => 35 
);


if(isset($required_ids_array)){
    foreach ($required_ids_array as  $storeId) {
        $condition = array ();
        $condition ['AND'] ['Post.status']=1;
        $blogs = $this->Post->find('all', array(
                'conditions' => $condition,
                'order' => 'Post.id.DESC',
                'limit'=>'4',
                'FIND_IN_SET(\''.$storeId.'\',Post.category_ids)')
        );
    }

预先感谢

2 个答案:

答案 0 :(得分:0)

此解决方案对我有用:)

$blogs = $this->Post->find ( 'all', array (
                                    'conditions' => array (
                                    'Post.status' => 1,
                                    'Post.id !=' => $id,
                                    'FIND_IN_SET(?, Post.category_ids)' => array ($storeId) ),
                            'limit' => 4,
                            'order' => 'Blog.modified DESC'  
                    ) );

答案 1 :(得分:0)

您应该在WHERE条件数组中写入FIND_IN_SET条件。您是在条件数组之外编写的。

 $blogs = $this->Post->find('all', array 
 ('conditions' => array('Post.status' => 1, 'FIND_IN_SET(\''. $storeId 
   .'\',Post.category_ids)' )),'order' => 'Post.id 
   DESC','limit'=>'4');