Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I have Restaurant, City, Country and User models with relations between them.</p> <p>Model:</p> <pre><code>public function search() { $criteria=new CDbCriteria; $criteria-&gt;together = true; $criteria-&gt;with= array('xCountry','xCity','User'); $criteria-&gt;compare('Id',$this-&gt;Id,true); $criteria-&gt;compare('Restaurant.Name',$this-&gt;Name,true); $criteria-&gt;addSearchCondition('xCountry.Name',$this-&gt;Country); $criteria-&gt;addSearchCondition('xCity.Name',$this-&gt;City); $criteria-&gt;compare('Zip',$this-&gt;Zip,true); $criteria-&gt;compare('Address',$this-&gt;Address,true); $criteria-&gt;compare('Description',$this-&gt;Description,true); $criteria-&gt;compare('Restaurant.Active',$this-&gt;Active,true); $criteria-&gt;addSearchCondition('User.Username',$this-&gt;Owner); $criteria-&gt;compare('Lat',$this-&gt;Lat); $criteria-&gt;compare('Lon',$this-&gt;Lon); return new CActiveDataProvider($this, array( 'criteria'=&gt;$criteria, )); } </code></pre> <p>View:</p> <pre><code>$this-&gt;widget('zii.widgets.grid.CGridView', array( 'id'=&gt;'restaurant-grid', 'dataProvider'=&gt;$model-&gt;search(), 'filter'=&gt;$model, 'columns'=&gt;array( 'Id', 'Name', 'Zip', 'Address', 'Active', array( 'name' =&gt; 'User.Username', 'header' =&gt; 'Username', 'filter' =&gt; CHtml::activeTextField($model, 'Owner'), 'value' =&gt; '$data-&gt;User-&gt;Username', ), array( 'name' =&gt; 'xCountry.Name', 'header' =&gt; 'Country', 'filter' =&gt; CHtml::activeTextField($model, 'Country'), 'value' =&gt; '$data-&gt;xCountry-&gt;Name', ), array( 'name' =&gt; 'xCity.Name', 'header' =&gt; 'City', 'filter' =&gt; CHtml::activeTextField($model, 'City'), 'value' =&gt; '$data-&gt;xCity-&gt;Name', ), array( 'class'=&gt;'CButtonColumn', ), ), )); </code></pre> <p>I hope this can help you.</p> <p>UPDATE:</p> <p>What if you try something like this:</p> <pre><code>... 'columns'=&gt;array( 'mailTemplate.name', 'sendDate', 'mailTemplate.subject', 'client.email', ... </code></pre> <p>UPDATE #2:</p> <p>Prepare yourself this will be a bit dirty.</p> <p>Let's say we've got two classes, A and B. B belongs to A. B's got a property, let's say "color" and we want to display it in our grid where we list the "A"s.</p> <p>The first thing you have to do is, manually create a property to your data provider class (what is "A"). This property will be "colorOfB", so you have to add "public $colorOfB;" to your model A.</p> <p>Add criteria for this property:</p> <pre><code>$criteria-&gt;compare('B.color',$this-&gt;colorOfB,true); </code></pre> <p>Add the column to the grid:</p> <pre><code>array( 'name' =&gt; 'B.color', 'header' =&gt; 'Color of B', 'filter' =&gt; CHtml::activeTextField($model, 'colorOfB'), 'value' =&gt; '$data-&gt;B-&gt;color'), </code></pre> <p>The final thing is to set this property manually in A's controller:</p> <pre><code>$modelA = new A('search'); $modelA-&gt;colorOfB = $_GET['A']['colorOfB']; </code></pre>
 

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