/** * Creates a new model. * If creation is successful, the browser will be redirected to the 'view' page. */ public function actionCreate() { $model = new Programa(); $ciudad = new ProgramaHasCiudad(); $ciudades[0] = $ciudad; if (isset($_POST['Programa'])) { $model->attributes = $_POST['Programa']; $error = false; if (isset($_POST['ProgramaHasCiudad'])) { foreach ($_POST['ProgramaHasCiudad'] as $index => $ciudad) { $int = new ProgramaHasCiudad(); $int->attributes = $ciudad; $int->validate(); $ciudades[$index] = $int; if (count($int->getErrors()) > 0) { $error = true; } } } if ($model->validate() && $error == false) { $model->fecha_creacion = date("Y-m-d H:i:s"); $model->fecha_modificacion = date("Y-m-d H:i:s"); if ($model->save()) { foreach ($ciudades as $ciudad) { $ciudad->programa_id = $model->id; $ciudad->insert(); } $this->redirect(array('view', 'id' => $model->id)); } } } $this->render('create', array('model' => $model, 'ciudades' => $ciudades)); }