Note that there are some explanatory texts on larger screens.

plurals
  1. POYii Framework: Display associated table entry through foreign key and search against it
    text
    copied!<p>Hello I am quite new to Yii Framework and I am following <strong>Larry Ullman's</strong> Tutorial Series. I have a <strong>Employee</strong> model and a <strong>Department</strong> Model. The <strong>Department Model</strong> has a <strong>has_many</strong> relation with <strong>Employee Model</strong>, <strong>departmentId</strong> being the foreign key in Employee Model.</p> <p>In the <strong>admin</strong> view, I have a search bar followed by a list of Employee, I want to display the name of the Department instead of the departmentId, and also make the search through department name. For a try, I wrote the following code having an array corresponding to departmentId field. This one worked for the <strong>view action</strong> but is not working for the <strong>admin action</strong>.</p> <p>Kindly Help.</p> <pre><code>&lt;?php echo CHtml::link('Advanced Search','#',array('class'=&gt;'search-button')); ?&gt; &lt;div class="search-form" style="display:none"&gt; &lt;?php $this-&gt;renderPartial('_search',array( 'model'=&gt;$model, )); ?&gt; &lt;/div&gt;&lt;!-- search-form --&gt; &lt;?php $this-&gt;widget('zii.widgets.grid.CGridView', array( 'id'=&gt;'employee-grid', 'dataProvider'=&gt;$model-&gt;search(), 'filter'=&gt;$model, 'columns'=&gt;array( 'id', array( 'name'=&gt;'departmentId', 'value'=&gt;$model-&gt;department-&gt;name, ), //'departmentId', 'firstName', 'lastName', 'email', 'ext', /* 'hireDate', 'leaveDate', */ array( 'class'=&gt;'CButtonColumn', ), ), )); ?&gt; </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