简体   繁体   中英

FIND_IN_SET in cakephp not working

I have a table in which category ids are stored in the DB as comma separated values, so I need to search another array in this comma separated values.

Need to search $required_ids_array in 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)')
        );
    }

Thanks in advance

This solution worked for me :)

$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'  
                    ) );

You should write your FIND_IN_SET condition in your WHERE condition array. You wrote it outside conditions array.

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

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