Pawan
Pawan

Reputation: 3864

yii2:autofill on multiple condtions

I have models/tables as follows:

table room_category

id room_category
1   Classic
2   Deluxe

table room_charges

id room_category room_name room_charges
1        1             c1       600
2        2             d1       800

table ipd_charges

id    doctor   room_category    charges_cash charges_cashless
1        1          1              200            300
2        1          2              300            400

table patient_detail(patient_admission)

id patient_name tpa_name(if not null, equivalent to charges_cashless)
1        1        Null
2        2         1

table daily_ward_entry

id  patient_name  room_name(id)  doctor_name(id) charges ( from ipd_charges)
1          1           1           1              200
2          2           2           1              400

Now there is a dropdown field in daily_ward_entry table for doctor. When I select the drop-down field the charges field needs to be autofilled.

I achieve this using Jason and ajax with the following code without taking into account the room_category. but the charges vary for room_category.(this is only working after saving the record, I prefer if there is someway to pull the charges before save.)

Here is my code in the controller:

public function actionChargesCash($id){
        $model = \app\models\IpdCharges::findOne(['doctor'=>$id]);
        return \yii\helpers\Json::encode(['visit_charges'=>$model->charges_cash]); 
    }

    public function actionChargesCashLess($id){
        $model= \app\models\IpdCharges::findOne(['doctor'=>$id]);
        return \yii\helpers\Json::encode(['visit_charges'=>$model->charges_cashless]); 
    }

I have also tried this variaton:

public function actionChargesCash($id){
        $model = \app\models\IpdCharges::find()
                ->where(['doctor'=>$id]) 
                 ->andwhere(['room_category'=>'daily_ward_entry.room_name'])->one();

If I replace the 'daily_ward_entry.room_name' with room_name id like 3 or 6, I am getting the result, which means I am not using the correct syntax for referring the column from current table.

How can I include the condition for room_name in the above code? Thanks in advance for your valuable help.

Upvotes: 1

Views: 149

Answers (1)

Ali MasudianPour
Ali MasudianPour

Reputation: 14459

daily_ward_entry.room_name is meaningless without any relation or join or sub-query. Actually, the query does not know the daily_ward_entry.

Suggestions:

1- Create a relation and use with or Join With

2- Create a query with Join of daily_ward_entry and ipd_charges on room_name=room_category

3- Create a query with a sub-query, to find all|one IpdCharge(s) that have room_category in SELECT room_name FROM daily_ward_entry

All of above suggestions satisfy your requirements.

Another important note:

  • andWhere()/orWhere() are to apply where to the default condition.
  • where() is to ignore the default condition

I don't see any default condition (Overridden Find()), So, there is no need to use andWhere, Just:

where(['doctor'=>$id,'room_category'=>3,...])

Would be sufficient.

Upvotes: 1

Related Questions