hi,
what I do is have a custom model ppty called
/**
*@var array
*/
private $_categoryIds;
and have getter / setter for it
getter : checks if ppty is null / if not get the categories related to the article / return $this->_categoryIds
This allows to get the related categories from db or from form if the _categoryIds is already populated (by a massive assignment in the controller for instance)
setter : takes an array of numeric key/categories ids value pairs as parameter and sets $this->_categoryIds
This is used when you do a massive assignment in the controller for instance
and also include categoryIds in safeAttributes so that the article model will get the data from the form if you do a massive assignment (if you set it by hand in the action then you don't need this)
//if you do this somewhere you need to tell the model that your custom ppty is safe to be massively assigned
$article->attributes = $_POST['Article']
in the form you can use activeDropDownList on the Article object since now you categoryIds is an Article ppty (attribute)
echo CHtml::activedropDownList($article, 'categoryIds', CHtml::listData($categoriesList, 'id', 'name'), array('multiple' => 1));
then in an afterSave call (in Article) you loop through your categoryIds and use the join model ArticleCategory to setup relations
public function afterSave() {
//saving relations with Category
//update scenario
if(!$this->isNewRecord) {
//delete ArticleCategory relations
ArticleCategory::model()->deleteAll('ArticleId = :ArticleId', array(':ArticleId' => $this->id));
}
//set ArticleCategory relations
foreach($this->categoryIds as $category_id) {
$relation = new ArticleCategory;
$relation->ArticleId = $this->id;
$relation->Category_id = $category_id;
$relation->save();
}
parent::afterSave();
}
all this is prbably not complete or completely errorLess but this should help you