Saving Many-Many In The Assignments Table!

Hi there,

I know that this is a battered topic but, I am a complete newbie and I have been trying to get my head around this problem for quite a while now but haven’t been able to get it.

I have two tables : Post and Tags

Now the relationship defined in this scenario is a many-many. And hence it has a table named ‘tbl_tags_has_tbl_post’.

This is storing the assignment between the relations.

Now I am taking the data for the the “tag” model in my “post” model and I have been able to save the data for the tags from the Post form. Now however, I have not been able to assign the values in the ‘tbl_tags_has_tbl_post’ table.

I have tried even using CAdvancedArRelationship extension and plugin but still to no avail.

This is my DB structure Graphically :

My Db Structure

This is the relationship that I have defined in my Post table :


public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'comments' => array(self::HAS_MANY, 'Comment', 'tbl_post_id'),

			'author' => array(self::BELONGS_TO, 'User', 'tbl_user_id'),

			'tagsRelation' => array(self::MANY_MANY, 'Tags', 'tbl_tags_has_tbl_post(tbl_post_id, tbl_tags_id)'),

			'commentCount' => array(self::STAT,'Comment','tbl_post_id'), // this shall count the number of comments present

		);

	}

And this is the relationship that I have defined in my Tags model:


public function relations()

	{

		// NOTE: you may need to adjust the relation name and the related

		// class name for the relations automatically generated below.

		return array(

			'posts' => array(self::MANY_MANY, 'Post', 'tbl_tags_has_tbl_post(tbl_tags_id, tbl_post_id)'),

		);

	}



And this is my actionCreate() method in my PostController class :


public function actionCreate()

	{

		$model=new Post;


		$tags = new Tags; // This is for initializing the second model




		$model->tbl_user_id = Yii::app()->user->id;


		// Uncomment the following line if AJAX validation is needed

		// $this->performAjaxValidation($model);


		if(isset($_POST['Post'],$_POST['Tags']))

		{

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

			$tags->attributes = $_POST['Tags'];

			$valid=$model->validate();

        	$valid=$tags->validate() && $valid;

			

        	if($valid)

        {

            // use false parameter to disable validation

            

            $tags->save(false);

            // ...redirect to another page

            // 

  

        }

			if($model->save()){

				

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

			}

		}


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

			'model'=>$model,

			'tags' =>$tags,

		));

	}



So now, what I want to be able to do is, whenever a Post is created with few tags, the assignment table should automatically be filled as well, for ex :

tbl_post_id tbl_tags_id

1                       1


2                       1


2                       2

Something like that.

I have even tried to use the CAdvancedAr Extension as well, but I have not been able to get it to run. I am really clueless, I have tried a lot of stuffs but have not been able to insert values into the assignments table.

Can anybody please guide me as to where am I going wrong and what would I need to do?

I am sorry if this question is a really old one. But I am really stuck here.

Regards,