Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you also want to search/sort by that column, this will probably be the best answer I can give you:</p> <p><a href="http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in-a-cgridview/" rel="nofollow">http://www.mrsoundless.com/php/yii/searching-and-sorting-a-column-from-a-related-table-in-a-cgridview/</a></p> <p><strong>UPDATE</strong> (because I'm guessing you didn't really understand the link. Or simply didn't want to read it because it's long..)</p> <p>I'm assuming:</p> <ul> <li>You got a relation to your bookdetails table called 'bookdetails'</li> <li>You got a column in your bookDetails table called 'name'</li> </ul> <p>Step 1: Add this to your model:</p> <pre><code>private $_name = null; public function getName() { if ($this-&gt;_name === null &amp;&amp; $this-&gt;bookDetails !== null) { $this-&gt;_name = $this-&gt;bookDetails-&gt;name; } return $this-&gt;_name; } public function setName($value) { $this-&gt;_name = $value; } </code></pre> <p>Step 2: Now add 'name' to the search rule in the 'rules' function of your Book model</p> <p>Step 3: Change your 'search' function of your Book model to:</p> <pre><code>public function search() { $criteria=new CDbCriteria; $criteria-&gt;with = "bookDetails"; // Make sure you query with the post table. $criteria-&gt;compare('t.bookId',$this-&gt;bookId,true); $criteria-&gt;compare('t.bookState',$this-&gt;bookState); $criteria-&gt;compare('t.bookBookdetailId',$this-&gt;bookBookdetailId,true); $criteria-&gt;compare('bookDetails.name', $this-&gt;name,true); $sort = new CSort(); $sort-&gt;attributes = array( 'defaultOrder'=&gt;'t.create_time DESC', 'bookId'=&gt;array( 'asc'=&gt;'t.bookId', 'desc'=&gt;'t.bookId desc', ), 'bookState'=&gt;array( 'asc'=&gt;'t.bookState', 'desc'=&gt;'t.bookState desc', ), 'bookBookdetailId'=&gt;array( 'asc'=&gt;'t.bookBookdetailId', 'desc'=&gt;'t.bookBookdetailId desc', ), 'name'=&gt;array( 'asc'=&gt;'bookDetails.name', 'desc'=&gt;'bookDetails.name desc', ), ); return new CActiveDataProvider('Book', array( 'criteria'=&gt;$criteria, 'sort'=&gt;$sort )); } </code></pre> <p>Step 4: Now add 'name' to your columns array in your CGridView. Should become something like:</p> <pre><code>'dataProvider' =&gt; $model-&gt;search(), 'columns' =&gt; array( 'bookCatalgoueNumber', 'bookDescription', 'bookBookdetailId', 'name', ), </code></pre> <p>Step 5: Read the article to understand what's going on. Step 6: Enjoy</p>
 

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