I solved the same problem today, just a couple of minutes before becoming crazy 
I paste here the code, only the relevant parts. The models are in spanish, but if you have any doubt just ask!!
(Empleado = Employee, Sucursal = Department, Empresa = Company)
The example shows how to edit the Employee-Empleado form to include a checkBoxList with Departments he/she can be assigned to.
FIRST STEP:
Set the relations!
In this example I’m focusing on Employee, so:
public function relations()
{
return array(
'empresa' => array(self::BELONGS_TO, 'Empresa', 'id_empresa'),
'sucursales' => array(self::MANY_MANY, 'Sucursal', 'tbl_sucursal_empleado(id_empleado, id_sucursal)'),
);
}
SECOND STEP:
Install some extension that extends the MANY_MANY relation management. I’m using CAdvancedArBehavior but there are several.
THIRD STEP:
Here are the parts that you should add to your view, model and controller.
//////////= Empleado View _form: =//////////
<?php echo CHtml::activeCheckBoxList($model, 'sucursalesIDs',
CHtml::listData(
Sucursal::model()->findAll(
'id_empresa = :id_empresa',
array(':id_empresa'=>$model->id_empresa)
),
'id_sucursal',
'nombre_sucursal'
) /* In this case I want to show only the sucursales-departments that belong
* to the company that the Employee belongs to. Otherwise you just
* Sucursal::model()->findAll() without condition */
) ?>
//////////= Empleado Model: =//////////
class Empleado extends CActiveRecord
{
/**
* Array to manage the HAS_MANY relation.
* You can't assign values directly to $sucursales:
* it's a relation, so you have to bypass it
*/
public $sucursalesIDs = array();
/**
* To sync the two "twins": the relation called 'sucursales'
* and the public variable 'sucursalesIDs'. If you don't do that
* you will not see checkbox checked for the departments-sucursales
* that are currently assigned to the Employee
*/
public function afterFind()
{
if(!empty($this->sucursales))
{
foreach($this->sucursales as $n=>$sucursal)
$this->sucursalesIDs[]=$sucursal->id_sucursal;
}
}
// Just follow the extension installation
public function behaviors(){
return array( 'CAdvancedArBehavior' => array(
'class' => 'application.extensions.CAdvancedArBehavior'));
}
public function rules()
{
....
array('sucursalesIDs', 'unsafe'), // I'm not sure about this. Actually I get a warning 'Failed to set unsafe attribute "sucursalesIDs"' but the application works.. does anybody know how to fix this?
....
}
}
//////////= Empleado Controller: =//////////
public function actionUpdate($id)
{
$model=$this->loadModel($id);
if(isset($_POST['Empleado']))
{
$model->attributes=$_POST['Empleado'];
$model->sucursales=$_POST['Empleado']['sucursalesIDs']; // If you don't do this it will not save the assigned sucursales-departments
if($model->save())
$this->redirect(array('view','id'=>$model->id_empleado));
}
$this->render('update',array(
'model'=>$model,
));
}
I hope this solved your problem. If yes, mark the thread as [Solved] and let’s hope that one day Yii documentation will be better!