Many_Many Save related w/o an extension (repost,sorry)

Hi

I want to use the extension withRelatedBehaviour (or another one) to save MANY_MANY related models.

I am using this kind of models

Post has: ‘categories’=>array(self::MANY_MANY, ‘Category’, ‘tbl_post_category(post_id, category_id)’)

Category has: ‘posts’=>array(self::MANY_MANY, ‘Post’, ‘tbl_post_category(category_id, post_id)’)

In the tbl_post-category table post_id and category_id fields exit and they are both primary keys and foreign keys.

First of all

1-should I use post_id and category_id fields as primary keys on tbl_post_category?

2-should I define foreign keys on them? If so, how can one save a record on tbl_post_category and on tbl_post at the same time?

3-Does WithRelatedBehaviour extension help me in this?

4-Is there any way of saving related models in my case without removing the foreign keys on tbl_post_category?

I hve read many forum posts and wiki pages. But still need help and any help would be appreciated.

Hi

This is my repost to here extensions’ subforum of my old post. Nobody replied, so I decided to post it here too.

Sorry for inconvenience.

I want to use the extension withRelatedBehaviour (or another one) to save MANY_MANY related models.

I tried withRelatedBehaviour and CAdvancedBehaviour extensions with no success.

I am using this kind of models

Post has: ‘categories’=>array(self::MANY_MANY, ‘Category’, ‘tbl_post_category(post_id, category_id)’)

Category has: ‘posts’=>array(self::MANY_MANY, ‘Post’, ‘tbl_post_category(category_id, post_id)’)

In the tbl_post-category table post_id and category_id fields exit and they are both primary keys and foreign keys.

First of all

1-should I use post_id and category_id fields as primary keys on tbl_post_category?

2-should I define foreign keys on them? If so, how can one save a record on tbl_post_category and on tbl_post at the same time?

3-Does WithRelatedBehaviour extension help me in this?

4-Is there any way of saving related models in my case without removing the foreign keys on tbl_post_category?

I hve read many forum posts and wiki pages. But still need help and any help would be appreciated.

Yes, because the two compose the unique primary key try something like this





        public function primaryKey()

	{

		return array('post_id', 'category_id');

	}



I do not understand the question

If I recall correctly yes in that it allows you to save your model along with its related model. Many people choose to go this route. Saves you from coding the saving logic but I cannot advise mush in this as I have not used it personally

Again I am not clear on the question.

Sorry for interrupting.

@Seal

I’d like to hear your opinion on that kind of extension.

Why didn’t you choose to use it?

I myself didn’t have a chance to use it, and I’m wondering whether I should try it or not.

OK dude. That’s not good enough.

If you don’t get answers to your question, please revise your question, instead of duplicating it across topics.

I am merging this with the original topic.

BTW, if you manage to post in the wrong forum, contact a moderator sot that it can be moved to the proper forum. ;)

[color="#006400"]/* Merged */[/color]

I like your style jacmoe

Thanks for the quick reply. I solved my problem.

I forgot to make Post id field auto incremented. I was getting some errors due to this.

Never mind my logic was wrong.

Still I am curious doing this with withRelatedBehaviour extension.

I have done it without an extension and secondly with CAdvancedBehaviour extension.

Actually I have done the save part not the update,view and delete part.

I am using AjaxCrudGiiTemplate too. So it seems a bit more complicated.

Now I am trying to make view Post with Categories and update etc.

@softark

Did not get round to using it.

At the time, l wanted to get a deeper insight into yii, so I just coded it.

@Seal

I see. Thanks for your reply.