For the update form i would like to pre select the categories and fill in the tags that belongs to the daytrip item.
I’ve created two get functions in the model that will store the selected categories and tags in public $categoryIds; and public $tagIds;.
Unfortunately, the debug print_r in the functions getDaytripCategories and getDaytripTags are empty ‘array( )’.
When i check the application log i see that ther is some lazy loading going on for categories and tags.
Controller:
public function actionUpdate()
{
$model=$this->loadModel();
Daytrip::model()->getDaytripCategories();
Daytrip::model()->getDaytripTags();
Model:
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
'city' => array(self::BELONGS_TO, 'City', 'city_id'),
'categories' => array(self::MANY_MANY, 'Category', 'daytrip_category(daytrip_id, category_id)'),
'tags' => array(self::MANY_MANY, 'Tag', 'daytrip_tag(daytrip_id, tag_id)'),
);
}
/**
* Getters for this model
*/
public function getDaytripCategories()
{
print_r($this->categories); //DEBUG PRINT
$data=array();
foreach ($this->categories as $n => $category)
$data[] = $category->id_category;
$this->categoryIds = $data;
return;
}
public function getDaytripTags()
{
print_r($this->tags); //DEBUG PRINT
$data=array();
foreach ($this->tags as $n => $tag)
$data[] = array('id'=>$tag->id_tag, 'text'=>$tag->tag);
$this->tagIds = CJSON::encode($data);
return;
}