checkBox

Hello. Please help to solve the problem.

There are three tables: tbl_posts, tbl_tags and table connection tbl_post_tag.

Communication spelled like this:


'tags' => array(self::MANY_MANY, 'Tag', 'tbl_post_tag(id_post, id_tag)')

When you create an article that I need to display all the available tags from the table tbl_tags, if the article is update, those tags which it already owned should be noted.

I tried through cycles, but then I learned about checkBoxList.

The main problem is that I do not know how to get data into checkBox.

Thank you!

Load the Tag Data from database in model

then use it using CHtml::checkBox

How? If I use $model->relation_name I receive an empty array.

For example in the view $model->tags come empty array.

How do I apply?

Is there are any data in tbl_post_tag related to $model ?

No, but I need to put check marks when the article is from this table. On the idea because if an empty array, so there should be no check marks?

Maybe someone has sample code representation using checkboxes?

Action my controller:


public function actionCreate(){

		$model = new Post;

		$tags = Post::model()->with('tags')->findAll();

		if(isset($_POST['Post'])){

			$model->attributes = $_POST['Post'];

			if($model->save()){

				$this->redirect(array('view', 'id' => $model->id_post));

			}

		}

		

		$this->render('_form', array(

			'model' => $model,

			'tags' => $tags,

		));

	}

I tried this:




$tags = Tag::model()->findAll();

foreach($tags as $tag){

	$label[] = $tag->text_tag;

	$url[] = $tag->url_tag;

}






<?php echo $form->checkBoxList('myCheckBoxList', $model->tags, $label); ?>



Writes that: