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.

1

5 Answers 5

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));
Sign up to request clarification or add additional context in comments.

Comments

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.

Comments

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

1 Comment

This will not perform LIKE search
0

Just a guess but try it like

'Model.category LIKE' => "%$ids%"

Comments

0

Havent tested it but this should work

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

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.