2

I am trying to get multiple fields which match to those ids(1, 2, 3) by Like %% search. I have tried to make it work for two hours, but it hasn't worked though it looks pretty simple.

$ids = array ('1','2','3');

$result = $this -> Model -> find ('all', array(
    'conditions' => array( 'Model.category LIKE' => '%'.$ids.'%')
));

I need somebody's help.

Sho
  • 343
  • 1
  • 3
  • 9
  • possible duplicate of [CakePHP how to get multiple rows by array of ID's](http://stackoverflow.com/questions/6819865/cakephp-how-to-get-multiple-rows-by-array-of-ids) – kero Dec 16 '14 at 13:59

5 Answers5

1

I'd suggest you build the 'conditions' array separately. For example:

$ids = array ('1','2','3');
$conditions = array();
foreach($ids as $id){
    $conditions['OR'][] = array( 'Model.category LIKE' => '%'.$id.'%')
}

$result = $this->Model->find('all', array('conditions'=>$conditions));
Nick Zinger
  • 1,174
  • 1
  • 12
  • 28
1

Sorry for the late reply, and thank you for answering my question. As long as I read your answers, the ansewer Nick Zinger posted was the most helpful. However, what I'd like to do was AND search. So I improved the answer like bellow.

$ids = array ('1','2','3');
$conditions = array();
foreach($ids as $id){
    $conditions['AND'][] = array( 'Model.category LIKE' => '%'.$id.'%')
}

$result = $this->Model->find('all', array('conditions'=>$conditions));

Again, I really appreciate for your helps.

Sho
  • 343
  • 1
  • 3
  • 9
0

See CakePHP how to get multiple rows by array of ID's

$this->YourModel->find('all', array(
    'conditions' => array(
        "YourModel.id" => array(1, 2, 3, 4)
    )
));
Community
  • 1
  • 1
Vineet
  • 4,525
  • 3
  • 23
  • 42
0

Just a guess but try it like

'Model.category LIKE' => "%$ids%"
p0d4r14n
  • 681
  • 5
  • 15
0

Havent tested it but this should work

$ids = array(1, 2, 3);

$result = $this->Model->find('all', array(
    'conditions' => array('Model.category'=>$ids ')
        ));
Jelle Keizer
  • 723
  • 5
  • 9