I've following DB structure:
table news:
CREATE TABLE `scrawroc`.`news` ( `id` mediumint(unsigned NOT NULL auto_increment COMMENT 'id', `title` varchar(100) collate utf8_polish_ci NOT NULL COMMENT 'title of news', `author_desc` varchar(20) collate utf8_polish_ci NOT NULL COMMENT 'author of news', `date` date NOT NULL COMMENT 'date when posted', `time` time NOT NULL COMMENT 'time when posted', `author_id` smallint(5) unsigned default NULL, `total_comments` mediumint(
unsigned NOT NULL default '0', `published` tinyint(1) NOT NULL default '0', PRIMARY KEY (`id`), KEY `FK_news_1` (`author_id`), CONSTRAINT `FK_news_1` FOREIGN KEY (`author_id`) REFERENCES `user` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=43 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci COMMENT='news';
table category:
CREATE TABLE `scrawroc`.`category` ( `id` tinyint(3) unsigned NOT NULL auto_increment, `category` varchar(40) collate utf8_polish_ci NOT NULL, `description` varchar(100) collate utf8_polish_ci NOT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci COMMENT='news categories';
table news_category
CREATE TABLE `scrawroc`.`news_category` ( `id` mediumint(unsigned NOT NULL auto_increment COMMENT 'id', `news_id` mediumint(
unsigned NOT NULL COMMENT 'ID of news', `category_id` tinyint(3) unsigned NOT NULL COMMENT 'ID of category', PRIMARY KEY (`id`), KEY `FK_news_id` (`news_id`), KEY `FK_categoryid` (`category_id`), CONSTRAINT `FK_categoryid` FOREIGN KEY (`category_id`) REFERENCES `category` (`id`) ON DELETE CASCADE, CONSTRAINT `FK_news_id` FOREIGN KEY (`news_id`) REFERENCES `news` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB AUTO_INCREMENT=53 DEFAULT CHARSET=utf8 COLLATE=utf8_polish_ci COMMENT='describes categories where belong news';
…and following model classes:
News
class News extends CActiveRecord{ /../ public function relations() { return array( 'author'=>array(self::BELONGS_TO, 'User', 'author_id'), 'comments'=>array(self::HAS_MANY, 'NewsComment', 'news_id', 'with'=>'author'), 'category'=>array(self::HAS_MANY, 'NewsCategory', 'news_id'), 'txt'=>array(self::HAS_ONE, 'NewsTxt', 'news_id'), ); } /../ }
Category
class Category extends CActiveRecord{ /../ public function relations() { return array( 'category'=>array(self::BELONGS_TO, 'Category', 'category_id'), 'news'=>array(self::HAS_MANY,'NewsCategory','category_id'), ); } /../ }
NewsCategory
class News extends CActiveRecord{ /../ public function relations() { return array( 'category'=>array(self::BELONGS_TO, 'Category', 'category_id'), ); } /../ }
How to pass news data for update action to CHtml::activeCheckBoxList() helper to achieve list of news categories with correct labels (field description from table category) and correct values in checkboxes from news_category table?