CAKEPHP: conditions with AND

0

I have a question when I search.

The conditions item in my current query looks like this:

'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, that is, with AND.

Does anyone know how to proceed?

    
asked by anonymous 09.03.2015 / 18:20

2 answers

1

My dear collection,

The way you are relating the TAGs is not feasible, to do.

Think of bringing products with mandatory TAGs A, B and C, 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 does not make sense, because TagsProduto.id has only 1 value in the line, or 1 ID of Product Tags per line.

To bring products with TAGs A, B and C, 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,
)));
?>
    
27.03.2015 / 22:36
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),
                )
            ),
        );
    
26.09.2015 / 03:59