static function saveExampleServices($example_id, $service_ids) { //print_r ($example_id); //print_r ($service_ids); //ExampleService::deleteAll('example_id = '. $example_id); ExampleService::deleteAll(['example_id' => $example_id]); //print_r($example_id); foreach ($service_ids as $service_id) { //$user = User::find()->where(['name' => 'CeBe'])->one(); $model = ExampleService::find()->where(['example_id' => $example_id, 'service_id' => $service_id])->one(); if (!$model) { $model = new ExampleService(); $model->service_id = $service_id; } //print_r($model); $model->example_id = $example_id; $model->save(); } return true; }
/** * Updates an existing Example model. * If update is successful, the browser will be redirected to the 'view' page. * @param integer $id * @return mixed */ public function actionUpdate($id) { $model = $this->findModel($id); $model->attributes = array('service' => ExampleService::getExampleServices($id)); // Получаем список услуг компании $services = Service::getAllServices(); $post = Yii::$app->request->post(); //print_r(); if ($model->load(Yii::$app->request->post()) && $model->save(false)) { ExampleService::saveExampleServices($id, $post['Example']['service']); return $this->redirect(['view', 'id' => $model->id]); } else { return $this->render('update', ['model' => $model]); } }
public function getExample_service() { return $this->hasMany(ExampleService::className(), ['service_id' => 'id']); }
?> <div class="panel panel-primary"> <div class="panel-heading"> <? if($model->icon) echo '<i class="fa fa-'.$model->icon.'"></i> '?><?php echo $model->title; ?> <? if ($model->description) echo '<span style="float:right;" data-trigger="hover" data-toggle="popover" data-placement="left" data-content="'.$model->description.'"><i class="fa fa-info-circle"></i></span>'; ?> </div> <div class="list-group"> <? foreach ($services as $service): ?> <? if($example){ $active=ExampleService::getExampleService($example,$service->id); } ?> <button type="button" class="list-group-item<?if ($example) { if ($active) echo ' list-group-item-success'; else echo ' disabled'; }; ?>"> <? if($service->icon) echo '<i class="fa fa-'.$service->icon.'"></i> '?><?php echo $service->title; ?> <? if ($service->description) echo '<span style="opacity:0.5;" data-trigger="hover" data-toggle="popover" data-placement="left" data-content="'.$service->description.'"> <i class="fa fa-info-circle"></i> </span>'; ?> <span style="float:right;"><? if ($service->price) echo $service->price.' <i class="fa fa-rub"></i>'; else echo 'бесплатно'; ?> </span> </button> <? endforeach; ?> </div> </div>