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 $member_id;
public $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(){
$model = new AddMemberToEventForm;
$this->render('addmembertoevent', array( 'model' => $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'=>'addmembertoeventform-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'member_id'); ?>
<?php echo $form->textField($model,'member_id'); ?>
<?php echo $form->error($model,'member_id'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'event_id'); ?>
<?php echo $form->textField($model,'event_id'); ?>
<?php echo $form->error($model,'event_id'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton('Save'); ?>
</div>
<?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.