Retrieve Model Data In Layout With Dropdownlist

Hello everyone,

as someone who is quite new to the whole Yii-experience, I would like to share some of my findings regarding a dropdownlist at te top of my website. I use this list to choose a client. In this way, I have the least amount of clicks to switch between clients. The list is dynamically populated BTW.

I don’t know if this is the correct way to do it (but it works, and that must count for something) and maybe I’m going against the whole MVC, DRY and whatever paradigms. But here goes:

The layout.php is not connected to any model, so my first step is to make a widget, under /protected/components/ClientWidget.php


class ClientWidget extends CWidget {

    public function run() {

        $models = Clienten::model()->findAll();

        //echo YiiBase::getPathOfAlias('views');

        $this->render('application.views.clientdropdown', array(





This widget gets all of my clients in model: Clients.

In my view (I use the theme named abound, BTW) I paste this in /views/layouts/tpl_navigation.php. This is the topbar navigation of the site:


<div class="subnav navbar navbar-fixed-top">

    <div class="navbar-inner">

    	<div class="container">

    	    <div class="navbar-search pull-left">


              $this->widget('ClientWidget') ?>



    	</div><!-- container -->

    </div><!-- navbar-inner -->

</div><!-- subnav -->

And the final piece of code in /protected/views/clientdropdown.php

This one had me pulling my hairs out! (but I always start with the gray hairs. So at least there is a little bit of positive downside to it :) ).

I was googling for the better part of a day, but couldn’t figure out how to react to a change in the dropdownlist. So, I devised something, by not using the properties of the dropdownlist, but of JQuery.

After a onchange event, I redirect to the proper page. I know, there is some hardcoding of the URL there, but to change that in something more elegant and variable shouldn’t pose much of a problem.

I hope this solution may help some of you.

<?php if($models != null): ?>


     <?php //$models = categories::model()->findAll();

     $select = '';

     $NaarClientPagina = Yii::app()->controller->createUrl("clienten");


     // format models resulting using listData     

     $list = CHtml::listData($models, 

                'ClientID', 'ClientAchternaam');    


     echo CHtml::dropDownList('clienten', $select, 


              array('empty' => 'Selecteer een client',





<?php endif; ?>

<script type="text/javascript">

$("#clienten").change(function() {


     /* do something here */ 

     var Keuze="<?php echo $NaarClientPagina; ?>";

     Keuze = 'localhost/bs/clienten/'+$("#clienten").val();

     window.location.href = '//'+(location.hostname)+'/bs/clienten/'+$("#clienten").val();

     //Okay, not really up to standards, this line, have to change it into something more elegant...