public function actionPolygons() { $model = new Sector(); $this->performAjaxValidation($model); foreach ($_POST['polygons'] as $sectorId => $coordinates) { $sector = Sector::model()->findByPk($sectorId); if ($sector) { foreach ($sector->polygons as $polygon) { $polygon->delete(); } } else { $sector = new Sector(); $sector->attributes = $_POST[get_class($sector)]; $sector->save(); } foreach ($coordinates as $i => $latLng) { $polygon = new Polygon(); $polygon->sector_id = $sector->id; $polygon->attributes = $latLng; $polygon->save(); } } if (!Yii::app()->request->isAjaxRequest) { $this->redirect('/regions/index/index'); } }