Reputation: 1520
public function actionCreate()
{
$model = new Bookings();
$temp = new RoomTypes();
$roomtype = $model->room_type;
$checkRoomModel = RoomTypes::find()->where(['room_type' => $room_type])->one();
$totalremain = $temp->total_remain;
if ($model->load(Yii::$app->request->post()))
{
if ($checkRoomModel->totalremain > 0)
{
$imageName = $model->first_name;
$mobile = $model->primary_mobile;
$model->file = UploadedFile::getInstance($model, 'file');
$model->file->saveAs('uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension);
//save the path in the db column
$model->id_image = 'uploads/id_images/' . $imageName . '_' . $mobile . '.' . $model->file->extension;
$model->save();
Yii::$app->db->createCommand("UPDATE room_types SET total_booked = total_booked + 1 WHERE room_type = '$model->room_type'")->execute();
Yii::$app->db->createCommand("UPDATE room_types SET total_remain = total_remain - 1 WHERE room_type = '$model->room_type'")->execute();
} else {
echo "This room Types are full ";
}
return $this->redirect(['view', 'id' => $model->id]);
}
} else {
return $this->render('create', [
'model' => $model,
'temp' => $temp,
]);
}
}
in database there is column total_remain and total_booked, i need to validate before actioncreate() that total_remain of $model->room_type is not equal zero
How to do this?
Upvotes: 0
Views: 145
Reputation: 3988
If you want to run it before the action you can use before action
public function beforeAction($event)
{
if (parent::beforeAction($action)) {
if ($this->action->id == 'create') {
$roomModel = RoomTypes::find()->where( [ 'room_type' => $room_type ])->one();
if($roomModel->totalremain) > 0)
{
return false;
}
else
{
return true;
}
}
}
else
{
return false;
}
}
Upvotes: 1
Reputation: 133370
The simplest way is a select of the related model and then drive you code to the right activiti
public function actionCreate()
{
$model = new Bookings();
$temp = new RoomTypes();
$checkRoomModel = RoomTypes::find()->where( [ 'room_type' => $room_type ])->one();
if ( $checkRooModel->totalremain) > 0 {
....
Upvotes: 0