Example #1
0
 /**
  * Creates data provider instance with search query applied
  *
  * @param array $params
  *
  * @return ActiveDataProvider
  */
 public function search($params)
 {
     $query = Calitems::find();
     $dataProvider = new ActiveDataProvider(['query' => $query]);
     $this->load($params);
     if (!$this->validate()) {
         // uncomment the following line if you do not want to return any records when validation fails
         // $query->where('0=1');
         return $dataProvider;
     }
     $query->andFilterWhere(['id' => $this->id, 'cal_id' => $this->cal_id, 'tool_id' => $this->tool_id]);
     $query->andFilterWhere(['like', 'result', $this->result])->andFilterWhere(['like', 'cuase', $this->cuase]);
     return $dataProvider;
 }
Example #2
0
 /**
  * Updates an existing Cals 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);
     $modelsCalitems = $model->calitem;
     if ($model->load(Yii::$app->request->post()) && $model->save()) {
         $oldIDs = ArrayHelper::map($modelsCalitems, 'id', 'id');
         $modelsCalitems = Model::createMultiple(Calitems::classname(), $modelsCalitems);
         Model::loadMultiple($modelsCalitems, Yii::$app->request->post());
         $deletedIDs = array_diff($oldIDs, array_filter(ArrayHelper::map($modelsCalitems, 'id', 'id')));
         $valid = $model->validate();
         $valid = Model::validateMultiple($modelsCalitems) && $valid;
         if ($valid) {
             $transaction = \Yii::$app->db->beginTransaction();
             try {
                 if ($flag = $model->save(false)) {
                     if (!empty($deletedIDs)) {
                         Calitems::deleteAll(['id' => $deletedIDs]);
                     }
                     foreach ($modelsCalitems as $modelCalitems) {
                         $modelCalitems->cal_id = $model->id;
                         if (!($flag = $modelCalitems->save(false))) {
                             $transaction->rollBack();
                             break;
                         }
                     }
                 }
                 if ($flag) {
                     $transaction->commit();
                     return $this->redirect(['view', 'id' => $model->id]);
                 }
             } catch (Exception $e) {
                 $transaction->rollBack();
             }
         }
     } else {
         return $this->render('update', ['model' => $model, 'modelsCalitems' => empty($modelsCalitems) ? [new Calitems()] : $modelsCalitems]);
     }
 }
 /**
  * Finds the Calitems model based on its primary key value.
  * If the model is not found, a 404 HTTP exception will be thrown.
  * @param integer $id
  * @return Calitems the loaded model
  * @throws NotFoundHttpException if the model cannot be found
  */
 protected function findModel($id)
 {
     if (($model = Calitems::findOne($id)) !== null) {
         return $model;
     } else {
         throw new NotFoundHttpException('The requested page does not exist.');
     }
 }
Example #4
0
 public function getCalitemtool()
 {
     // ความสัมพันธ์จาก tools ไปหา calitems
     return $this->hasMany(Calitems::className(), ['tool_id' => 'id']);
 }
Example #5
0
 public function getCalitem()
 {
     // เชื่อมจาก cals ไปหา calitems
     return $this->hasMany(Calitems::className(), ['cal_id' => 'id']);
 }