Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Let's imagine for a minute that your <code>Mcourse</code> table is called <code>courses</code> and model for that table is called <code>Courses</code>, your <code>Lcourse</code> table is called <code>courses_colleges</code> and your colleges table is <code>colleges</code> and model for that table is <code>Colleges</code></p> <p>Now, You should have <code>Courses</code> model with relations:</p> <pre><code>public function relations() { return array( 'colleges' =&gt; array(self::MANY_MANY, 'Colleges', 'courses_colleges(course_id, college_id)') ); } </code></pre> <p>Your <code>Colleges</code> model should have similar relations:</p> <pre><code>public function relations() { return array( 'courses' =&gt; array(self::MANY_MANY, 'Courses', 'courses_colleges(college_id, course_id)') ); } </code></pre> <p>Now if you want to print out a dropdown with all courses available for a certain college. In your controller action method get the model of that college including its courses:</p> <pre><code>public function actionShow() { $id = 1; // We set just some sample id. You could get it from request ofc. $college = Colleges::model()-&gt;with('courses')-&gt;findByPk($id); $this-&gt;render('show', array('college'=&gt;$college)); } </code></pre> <p>Now in your view print out this:</p> <pre><code>echo CHtml::dropDownList('courses', '', CHtml::listData($college-&gt;courses, 'id', 'name')); </code></pre> <p>Where <code>'id'</code> and <code>'name'</code> are columns of your <code>Courses</code> model.</p> <p>Something like that.</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