Many people find it hard to make the Yii checkBoxList selected on update view, but believe me in fact its very easy and straight forward. This post will help you in showing user’s last selection by showing them a preselected checkbox list. Need help? You can Email me or Skype chat I Suppose you have created a MANY_MANY relation as given below. Book Model: Author Model: Note: authorbook is the table name containing ids checkBoxList function is taking three parameters. 2nd parameter is important here, because it makes the checkBoxList selected.
'authors' => array(self::MANY_MANY, 'Author', 'authorbook(book_id,author_id)'), 'books' => array(self::MANY_MANY, 'Book', 'authorbook(author_id, book_id)'), Make a selected checkBoxList
$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);
How to make Yii checkBoxList selected
Learn how to preselect previously saved checkboxes in Yii 1.1 using CHtml::checkBoxList() and a MANY_MANY relationship. This guide explains how to retrieve selected book IDs and display them as checked when updating an author’s record.
Work with Scriptbaker
Let's talk about your project
Whether you're extending an existing platform, modernising legacy code, or planning something new, our engineers can help. Tell us what you're working on and we'll explore a practical technical solution together.
Related posts
Yii
Yii Multiple Related Models Save Guide
Learn how to save multiple related models in Yii 1.1 using ActiveRecord relationships, HAS_MANY, dynamic child forms, and ESaveRelatedBehavior. This guide shows how to save parent and child records together with a single form submission while reducing repetitive database transaction and loop code.
Yii
Separate Front & Admin in Yii
Split a Yii 1.1 app into a front end and an admin panel with separate controllers, views and config files, a custom behavior, and clean backend URLs.
Yii
Yii 1.1: Behaviors & Events
Learn how Yii 1.1 behaviors and events work: attach reusable methods to components, handle application events, and use behaviors with Active Record models.