CAKEPHP: conditions with AND

Asked

Viewed 80 times

0

I have a question when making a search.

The item conditions in my current query is this way:

'conditions'=>array(
    'Produto.publicado'=>1,
    'Produto.titulo LIKE'=>"%$kw%",
    'TagsProduto.tag_id'=>$tags
),

However, $tags is an array and it will interpret and implicit the IN in the search. I need it to search incrementally, ie with AND.

Does anyone know how to proceed?

  • could you post an example of the $tags array? It’s easier to understand.

2 answers

1

My dear collection,

The way you are linking the tags is not feasible, do.

Think that bringing products with mandatory A, B and C tags, would have to have subquerys:

<?php
$criterios = array(
    'Produto.publicado'=>1,
    'Produto.titulo LIKE'=>"%$kw%",
    'AND' => array(),
);
foreach ($tags as $tag){
    $criterios['AND'][] = array('TagsProduto.tag_id'=>$tag);
}
?>

See above a way to do what you want, but it makes no sense, because Tagsproduto.id has only 1 value on the line, or 1 ID of Tagsproduto per line.

To bring products with A, B and C tags, you have to do something like:

<?php
$prodTag = array();
foreach ($tags as $tag){
    $prodTag[] = "Produto.id = (SELECT produto_id FROM tags_produto WHERE id={$tag} AND produto_id=Produto.id)";
}
$tags = implode(' AND ', $prodTags);

$this->Produto->find('all', array('conditions'=>array(
    $tags,
)));
?>

0

Try these examples of AND/OR

         $conditons = array(
            'conditions'=>array(
                'AND' => array(
                    array('Produto.publicado'=>1,),
                    array('Produto.titulo LIKE'=>"%$kw%",),
                    array('TagsProduto.tag_id'=>$tags),
                )
            ),
        );

        $conditons = array(
            'conditions'=>array(
                'OR' => array(
                    array('Produto.publicado'=>1,),
                    array('Produto.titulo LIKE'=>"%$kw%",),
                    array('TagsProduto.tag_id'=>$tags),
                )
            ),
        );

Browser other questions tagged

You are not signed in. Login or sign up in order to post.