/** * Finds the Item model based on its primary key value. * If the model is not found, a 404 HTTP exception will be thrown. * @param integer $id * @return Item the loaded model * @throws NotFoundHttpException if the model cannot be found */ protected function findModel($id) { if (($model = Item::findOne($id)) !== null) { return $model; } else { throw new NotFoundHttpException('The requested page does not exist.'); } }
/** * Returns the data model based on the primary key given in the GET variable. * If the data model is not found, an HTTP exception will be raised. * @param integer the ID of the model to be loaded */ public function findModel($id) { $model = Item::findOne($id); if ($model === null) { throw new \yii\web\HttpException(404, 'The requested page does not exist.'); } return $model; }
/** * Deletes an existing Purchase model. * If deletion is successful, the browser will be redirected to the 'index' page. * @param integer $id * @return mixed */ public function actionDelete($id) { $model = $this->findModel($id); $modelDetails = $model->getPurchasesDetails()->all(); $transaction = \Yii::$app->db->beginTransaction(); try { foreach ($modelDetails as $modelDetail) { $item = Item::findOne($modelDetail->item_id); $item->stock -= $modelDetail->quantity; if (!($flag = $item->save())) { $transaction->rollBack(); break; } } if ($flag) { if ($model->delete()) { $transaction->commit(); } else { $transaction->rollBack(); } } } catch (Exception $e) { $transaction->rollBack(); } return $this->redirect(['index']); }
public function itemVal($attribute, $params) { $item = Item::findOne(["id" => $this->{$attribute}]); if ($item === null) { $this->addError($attribute, Yii::t('app', 'Item id not found')); } }