Note that there are some explanatory texts on larger screens.

plurals
  1. POYii - Query Manipulation for Custom CGridView with Advanced Search
    primarykey
    data
    text
    <p>So, I've extended <a href="http://www.yiiframework.com/doc/api/1.1/CGridView" rel="noreferrer">CGridView</a> to include an Advanced Search feature tailored to the needs of my organization. </p> <ul> <li><strong>Filter</strong> - lets you show/hide columns in the table, and you can also reorder columns by dragging the little drag icon to the left of each item.</li> <li><strong>Sort</strong> - Allows for the selection of multiple columns, specify Ascending or Descending.</li> <li><strong>Search</strong> - Select your column and insert search parameters. Operators tailored to data type of selected column.</li> </ul> <p><img src="https://i.stack.imgur.com/z1LDi.png" alt="Advanced Search Screenshot"></p> <p>Version 1 works, albeit slowly. Basically, I had my hands in the inner workings of <a href="http://www.yiiframework.com/doc/api/1.1/CGridView" rel="noreferrer">CGridView</a>, where I snatch the results from the <a href="http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider" rel="noreferrer">DataProvider</a> and do the searching and sorting in PHP before rendering the table contents.</p> <p>Now writing Version 2, where I aim to focus on clever <a href="http://www.yiiframework.com/doc/api/1.1/CDbCriteria" rel="noreferrer">CDbCriteria</a> creation, allowing MySQL to do the heavy lifting so it will run quicker. The implementation is trivial when dealing with a single database table. The difficulty arises when I'm dealing with 2 or more tables... For example, if the user intends to search on a field that is a <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.arr#statistical-query" rel="noreferrer">STAT</a> relation, I need that relation to be present in my query so that I may include comparisons.</p> <p><strong>Here's the question. How do I assure that Yii includes all <code>with</code> relations in my query so that I include comparisons?</strong> I've included all my relations <code>with</code> my criteria in the model's <code>search</code> function and I've tried CDbCriteria's <code>together</code> set to true ...</p> <pre><code>public function search() { $criteria=new CDbCriteria; $criteria-&gt;compare('id', $this-&gt;id); $criteria-&gt;compare( ... ... $criteria-&gt;with = array('relation0','relation1','relation3'); $criteria-&gt;together = true; return new CActiveDataProvider( get_class($this), array( 'criteria'=&gt;$criteria, 'pagination' =&gt; array('pageSize' =&gt; 50) ));} </code></pre> <p>Then I'll snatch the criteria from the <a href="http://www.yiiframework.com/doc/api/1.1/CActiveDataProvider" rel="noreferrer">DataProvider</a> and <a href="http://www.yiiframework.com/doc/api/1.1/CDbCriteria#addCondition-detail" rel="noreferrer">add a few conditions</a>, for example, looking for dates > 1234567890. But I still get errors like this...</p> <pre><code>CDbCommand failed to execute the SQL statement: SQLSTATE[42S22]: Column not found: 1054 Unknown column 't.relation3' in 'where clause'. The SQL statement executed was: SELECT COUNT(DISTINCT `t`.`id`) FROM `table` `t` LEFT OUTER JOIN `relation_table` `relation0` ON (`t`.`id`=`relation0`.`id`) LEFT OUTER JOIN `relation_table` `relation1` ON (`t`.`id`=`relation1`.`id`) WHERE (`t`.`relation3` &gt; 1234567890) </code></pre> <p>Where <code>relation0</code> and <code>relation1</code> are <code>BELONGS_TO</code> relations, but any <code>STAT</code> relations, here depicted as <code>relation3</code>, are missing. Furthermore, why is the query a <code>SELECT COUNT(DISTINCT 't'.'id')</code> ?</p> <p><strong>Edit</strong> @DCoder Here's the specific relation I'm working with now. The main table is Call, which has a <a href="http://www.yiiframework.com/doc/guide/1.1/en/database.arr#declaring-relationship" rel="noreferrer"><code>HAS_MANY</code></a> relation to CallSegments, which keeps the times. So the <code>startTime</code> of the Call is the minimum start_time of all the related CallSegments. And <code>startTime</code> is the hypothetical <code>relation3</code> in my anonymized query error.</p> <pre><code>'startTime' =&gt; array(self::STAT, 'CallSegments', 'call_id', 'select' =&gt; 'min(`start_time`)'), </code></pre> <p><strong>Edit</strong> Other people have sent me to <a href="http://www.yiiframework.com/doc/api/1.1/CDbCriteria#together-detail" rel="noreferrer">CDbCriteria's <code>together</code> property</a>, but as you can see above, I am currently trying that to no avail.</p> <p><strong>Edit</strong> Looks like the issue <strike>has</strike> may have been reported: <a href="http://code.google.com/p/yii/issues/detail?id=2494" rel="noreferrer">Yii</a> and <a href="https://github.com/yiisoft/yii/issues/204" rel="noreferrer">github</a> tickets.</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.
 

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