Hello everyone!
I’m struggling with many_many relations and auto incrementing id’s…
Everything is fine when a id is set manually in the form, but when I’m leaving the idfield (mysql, auto increment) blank, it creates a new record for Client, but not doesn’t create a record in GroupHasClient. This is probably caused because $model->getPrimaryKey() returns null. Same with $model->id…
Any ideas?
public function actionCreate()
{
$model = new Client;
if(isset($_POST['Client']))
{
$model->attributes=$_POST['Client'];
if($model->save()) {
$client_id = $model->getPrimaryKey(); // problem here?
if(isset($_POST['Client']['groups'])){
$arr = $_POST['Client']['groups'];
if(count($arr)>0){
foreach($arr as $group_id){
$modelGroup = new GroupHasClient;
$modelGroup->client_id = $client_id;
$modelGroup->group_id = $group_id;
$modelGroup->save();
}
}
}
$this->redirect(array('view','id'=>$model->id));
}
}
$this->render('create',array(
'model'=>$model,
));
}
I tried http://www.yiiframework.com/extension/save-relations-ar-behavior but same issue here. Works well when setting the id, but not with auto incrementing ids…
Best regards and thanks for any help!