How to make Yii checkBoxList selected

How to make Yii checkBoxList selected
Standard

I Suppose you have created a MANY_MANY relation as given below.

Book Model:

'authors' => array(self::MANY_MANY, 'Author', 'authorbook(book_id,author_id)'),

Author Model:

'books' => array(self::MANY_MANY, 'Book', 'authorbook(author_id, book_id)'),

Note: authorbook is the table name containing ids

Checkbox List in form:

$books = CHtml::listData(Book::model()->findAll(), 'id', 'name');
$selected_keys = array_keys(CHtml::listData( $model->books, 'id' , 'id'));
echo CHtml::checkBoxList('Author[books][]', $selected_keys, $books);
Advertisements

4 thoughts on “How to make Yii checkBoxList selected

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: