Save tags. many-many

Hi!

I have a problem. I myself chose to use checkboxes for tags. I am a beginner, and i do not like to turn the table relationships.

Here are the approximate outline of the code:




public function afterSave(){

	if(parent::afterSave()){

		if(!empty($this->tags)){

			foreach($this->tags as $tag){

				$this->tags.id_tag = $tag->id_tag;

				$this->tags.id_post = $this->id_post;

			}

		}

	}

}



For all the right ideas, but does not work.

This extension could help you:

http://www.yiiframework.com/extension/esaverelatedbehavior/

Solved the problem:




public function afterSave(){

    if(parent::afterSave()){

        $sql = 'DELETE FROM tbl_post_tag WHERE id_post = '.$this->id_post;

            $command = Yii::app()->db->createCommand($sql);

            $command->execute();

				

            foreach($this->tags as $tag){

                $sql = 'INSERT INTO tbl_post_tag (id_tag, id_post) VALUES ('.$tag.', '.$this->id_post.')';

                $command = Yii::app()->db->createCommand($sql);

                $command->execute();

            }

        }

    }

}



but, if i update article. always select first checkbox. why?