简体   繁体   中英

Use array in pagination conditions [CakePHP 3]

I want to use an array of ids as a condition to my paginate function, but I get this error 'Cannot convert value to integer', I understand that a array is not an integer, but how could I make the condition look for all the values in the array.

$friendsid = explode(',', $this->Auth->user('friends'));
$this->paginate = [ 
    'conditions' => [
        'Users.id' => $friendsid,
    ]
]; 
$this->set('users', $this->paginate($this->Users));
$this->set('_serialize', ['users']);

You could try it:

$friendsid = explode(',', $this->Auth->user('friends'));
$query     = $this->Users->find() 
                ->where(function ($exp, $q) use ($friendsid) {
                    return $exp->in('Users.id', $friendsid);
                });
$this->set('users', $this->paginate($query));
$this->set('_serialize', ['users']);

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