Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The best way I found for creating an extra searchable relation column in a gridview is by using the following pattern:</p> <pre class="lang-php prettyprint-override"><code>// the model class class Product extends CActiveRecord { // create a custom field in your model class to hold your search data public $searchSupplierName; // [...] // make sure the custom field is safe to set in your rules public function rules() { return array( // [...] // add your custom field name to this rule array('id, searchSupplierName', 'safe', 'on'=&gt;'search'), ); } // edit your search function public function search() { // [...] // use the value of the custom attribute as a condition in your search $criteria-&gt;compare('supplier.name', $this-&gt;searchSupplierName,true); // you could use something like the following // to make sure you don't fire a lazy loading query // for every result that is shown on the gridview. if($this-&gt;searchSupplierName) $criteria-&gt;with = array( 'supplier' ); // then do something like this if you want to be able to sort on the field return new CActiveDataProvider($this, array( 'criteria'=&gt;$criteria, 'sort'=&gt;array( 'attributes'=&gt;array( 'searchSupplierName'=&gt;array( 'asc'=&gt;'supplier.name', 'desc'=&gt;'supplier.name DESC', ), '*', // this makes sure the rest of the field sorts keep working ), ), )); } } </code></pre> <pre class="lang-php prettyprint-override"><code>// in the template &lt;?php $this-&gt;widget('zii.widgets.grid.CGridView', array( // set the dataprovider to the changed search function output 'dataProvider' =&gt; $model-&gt;search(), 'filter' =&gt; $model, // model is an instance of Product in this case (obviously ;) // [...] 'columns' =&gt; array( // [...] // this is the column we care (extra) about // notice I still use a custom value here (as @ethan describes in his answer) array( 'name' =&gt; 'searchSupplierName', 'value' =&gt; '$data-&gt;supplier-&gt;name', ), ), )); ?&gt; </code></pre> <p>This solution should work for (almost) any case you want. It is the best solution I can think of if you want text based filtering on the column. </p> <p>But there are <a href="http://www.yiiframework.com/forum/index.php?/topic/9083-search-filter-of-a-relations-field-through-cgridview/" rel="nofollow">other options</a> as well. For example if you just want to search on a <code>relation_id</code> by using a combobox to find the right value there are <a href="http://www.yiiframework.com/forum/index.php/topic/9083-search-filter-of-a-relations-field-through-cgridview/page__p__45552#entry45552" rel="nofollow">less complicated solutions</a>.</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.
    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