Many_many relations

Hi

Yes another question about how to save data that is in many_many relationship. I already read all the entries in the forum on the subject but i still don’t get it. I also read the blog tutorial but that doesn’t cover many_many relationships. It does mention it when it talks about posts and categories but doesn’t say a word about how these details are saved in the database. As far as i can see there is no full example that deals with saving data to an associative table that doesn’t have it’s own AR model. I am new to Yii and i am starting to get it but many_many is beating me. I have spent three days trying to figure this out but i don’t get it. Yes i am blonde.

So i have a very simple app with members and events. A member can be part of many events and an event can have many members. I have two tables to hold the members and events. In between i have a table that holds the id of a member and the id of an event to connect the two. This is what my db looks like:

CREATE TABLE if not EXISTS tbl_member (

member_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,


member_name VARCHAR(128)

) engine = innodb ;

CREATE TABLE if not exists tbl_event (

event_id INTEGER NOT NULL PRIMARY KEY AUTO_INCREMENT,


event_name VARCHAR(128)

) engine = innodb ;

create table if not exists tbl_member_in_event (

member_id int,


event_id int,


primary key (member_id, event_id)

) engine = innodb ;

/* Relations *****************************************************/

alter table tbl_member_in_event add constraint FK_member_id foreign key (member_id) references tbl_member(member_id) on delete cascade on update restrict;

alter table tbl_member_in_event add constraint FK_event_id foreign key (event_id) references tbl_event(event_id) on delete cascade on update restrict;

I have models for the tables tbl_member and tbl_event but no model for tbl_member_in_event. I have created a form model that looks like this:

<?php

class AddMemberToEventForm extends CFormModel

{

public &#036;member_id;


public &#036;event_id;





public function rules()


{


    return array(


        array('member_id, event_id', 'required'),


    );


}

}

In the EventController class i have made the following action:

public function actionAddMemberToEvent(){

	&#036;model	= new AddMemberToEventForm;


	


	&#036;this-&gt;render('addmembertoevent', array( 'model' =&gt; &#036;model ));


}

I also created the following form in the views/event folder and named it addmembertoevent.php:

<div class="form">

<?php $form=$this->beginWidget(‘CActiveForm’, array(

'id'=&gt;'addmembertoeventform-form',


'enableAjaxValidation'=&gt;false,

)); ?>

&lt;p class=&quot;note&quot;&gt;Fields with &lt;span class=&quot;required&quot;&gt;*&lt;/span&gt; are required.&lt;/p&gt;





&lt;?php echo &#036;form-&gt;errorSummary(&#036;model); ?&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'member_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'member_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'member_id'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row&quot;&gt;


	&lt;?php echo &#036;form-&gt;labelEx(&#036;model,'event_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;textField(&#036;model,'event_id'); ?&gt;


	&lt;?php echo &#036;form-&gt;error(&#036;model,'event_id'); ?&gt;


&lt;/div&gt;





&lt;div class=&quot;row buttons&quot;&gt;


	&lt;?php echo CHtml::submitButton('Save'); ?&gt;


&lt;/div&gt;

<?php $this->endWidget(); ?>

</div><!-- form -->

All this will render a form on the screen that has two input fields and a submit button. But the data i enter in the input fields is not being saved in the inbetween table. So i ask you how can this be done? Please help. I don’t get it.