/**
  * Updates an existing Teachers 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);
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         TeachersOtherCathedra::deleteAll('id_teacher = ' . $id);
         foreach ($model->teacher_other_cathedra as $cath) {
             $toc = new TeachersOtherCathedra();
             $toc->id_teacher = $model->teacher_id;
             $toc->id_cathedra = $cath;
             $toc->insert();
         }
         return $this->redirect(['view', 'id' => $model->teacher_id]);
     } else {
         $opt = TeachersOtherCathedra::findAll(['id_teacher' => $_GET['id']]);
         foreach ($opt as $key) {
             $optArr[] = $key['id_cathedra'];
         }
         $model->teacher_other_cathedra = $optArr;
         return $this->render('update', ['model' => $model]);
     }
 }
Пример #2
0
<?php

use yii\helpers\Html;
use yii\widgets\DetailView;
use app\module\handbook\models\TeachersOtherCathedra;
use app\module\handbook\models\Cathedra;
/* @var $this yii\web\View */
/* @var $model app\module\handbook\models\Teachers */
$this->title = $model->teacher_name;
$this->params['breadcrumbs'][] = ['label' => 'Викладачі', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
$optionsId = TeachersOtherCathedra::findAll(['id_teacher' => $model->teacher_id]);
for ($i = 0; $i < count($optionsId); $i++) {
    $optionName[] = Cathedra::findAll(['cathedra_id' => $optionsId[$i]['id_cathedra']]);
    $optionsArray[] = $optionName[$i][0]['cathedra_name'] . " ";
}
$optionsString = implode($optionsArray);
?>
<div class="teachers-view">

    <h1><?php 
echo Html::encode($this->title);
?>
</h1>

    <p>
        <?php 
echo Html::a('Оновити', ['update', 'id' => $model->teacher_id], ['class' => 'btn btn-primary']);
?>
        <?php 
echo Html::a('Видалити', ['delete', 'id' => $model->teacher_id], ['class' => 'btn btn-danger', 'data' => ['confirm' => 'Are you sure you want to delete this item?', 'method' => 'post']]);