Pawan
Pawan

Reputation: 3864

Yii2: Saving return values of function value to Database

I have created a function in my model PatientDetail.php.

This function is working well and returning the desired value, but I am not able to save the same in database.

public function getRoomCategory(){
     $model = \app\models\RoomCategory::find('id')
     ->innerJoin('room_charges','room_charges.room_category=room_category.id')
     ->innerjoin('patient_detail', 'room_charges.id = patient_detail.bed_type')               
     ->Where(['patient_detail.id'=> $this->id])->one();

     return $model;

   }

I have a field/Column in the model as 'room_category' and I want to save the return value of the function getRoomCategory in the Database.

I have tried to create the function with the same name like getroom_category(){}, but then I am not getting any value in the form field in my _form.php

I have also tried in my controller like:

if(isset($_POST['RoomCategory'])){
                $RoomCategory = $_POST['RoomCategory'];

            $model->room_category=$RoomCategory;
            $model->save();
            }

What is the best way to save the calculated value to the database? Thank for your valuable guidance.

Upvotes: 1

Views: 1013

Answers (1)

Kazux Kazux
Kazux Kazux

Reputation: 151

It seems that the rules validation of the model is not allowing you to save it so If u want to save it without the validation you can do like this :

if(isset($_POST['RoomCategory'])){

   $RoomCategory = $_POST['RoomCategory'];

   $model->room_category=$RoomCategory;
   $model->save(false);
        }

Upvotes: 0

Related Questions