Note that there are some explanatory texts on larger screens.

plurals
  1. POYii Framework : Help to translate relational Sql query
    primarykey
    data
    text
    <p>I encountered a problem to write this query in a Yii model oriented style. </p> <p>I have a 3 main tables : questions, categories, countries and relational tables : questions_has_categories, questions_has_countries. </p> <p>Now i try to search questions that belongs to country, category. </p> <p>With normal SQL statement i write : </p> <pre><code>SELECT q.id, q.question, c.name, co.name FROM questions AS q, categories AS c, countries AS co, questions_has_countries AS ta, questions_has_categories AS qhc WHERE q.id = qhc.questions_id AND c.id = qhc.categories_id AND q.id = ta.questions_id AND co.id = ta.countries_id AND c.id = 1 AND co.id = 2 </code></pre> <p>This works fine. Now with Yii Active Record i try to search like this : </p> <pre><code>$model = Questions::model()-&gt;with( array( 'categories' =&gt; array ( 'select' =&gt; 'name', 'condition' =&gt; 'categories.id=:cat_id', 'params' =&gt; array (':cat_id' =&gt; $_POST["Questions"]["categories"]) ) ), array( 'countries' =&gt; array ( 'select' =&gt; 'name', 'condition' =&gt; 'countries.id=:cou_id', 'params' =&gt; array (':cou_id' =&gt; $_POST["Questions"]["countries"]) ) ) )-&gt;findAll(array ('select' =&gt; 'question')); </code></pre> <p>This code respects only the categories, not countries ( returns for all countries, doesnt filter, narrow down ). </p> <p>What am i doing wrong ? </p> <p>My Question Model relations function looks like this : </p> <pre><code>public function relations() { return array('categories' =&gt; array(self::MANY_MANY, 'Categories', 'questions_has_categories(questions_id, categories_id)'), 'countries' =&gt; array(self::MANY_MANY, 'Countries', 'questions_has_countries(questions_id, countries_id)'), ); } </code></pre> <p>Pls help :) Thanks</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload