Hello people!
I’m finally starting to write one of the mail functions in my app: adding people to events.
What I have:
3 models - Person , Event , Person_Event. The last one is not only a connection table but a whole model with CRUD as well , because it has additional info about the person who attended an event , and thus I have 2 belongs_to relations with the other two models.
How it should work:
Person_Event has its own PK with auto-increment , but needs to get the person_id and event_id from the other models.
Now with event_id there is no problem , cause I’m sending it by URL from Event.view
However , I want my user to be able to pick several people for a certain event. Now , I could use a multi-dropdown list ,
but I want to provide him with more info about every person , and thus, I want the user to pick the people from a CgridView.
So far:
In the Person_event form , the event_id is sent by URL , and a CgridView of people is being shown. The grid has a checkbox column , which , as far as I know , saves the id of the selected person.
I’m failing to write the actionCreate in the controller , it keeps saying that person_id is empty , thus doesn’t get the checked people from the grid.
Here is my action:
public function actionCreate()
{
$model=new Person_Event;
$model->event_id=$this->_event->id; //event_id by URL
if(isset($_POST['Person_Event']))
{
$model->attributes=$_POST['Person_Event'];
if($model->save())
{
foreach ($_POST['Person_Event']['person'] as $model) //person is the name of the relation with Person model
{
$model->event_id=$this->_event->id; //as above , do I need it again?
$model->person_id = $model; //probably what doesn't work?
if (!$model->save()) print_r($model->errors);
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
EDIT: tried with ajax instead , didn’t work
public function actionCreate()
{
$model=new Person_Event;
$model->event_id=$this->_event->id; //event_id by URL
if(Yii::app()->request->getIsAjaxRequest())
{
$checkedIDs=$_GET['checked'];
foreach($checkedIDs as $id)
{
$model->person_id=$id;
$model->event_id=$this->_event->id;
}
}
$this->render('create',array(
'model'=>$model,
));
}