Yii2: Conditional fill of Json data

≯℡__Kan透↙ 提交于 2019-12-24 10:35:34

问题


I have a table ipd_charges like

id doctor_name charges_cash charges_cashless
1      1             300         600
2      2             200         400

and in my controller the code is like this which is working fine

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

Now what I want is to fill the data on condition and tried to modify the above code which is only returning the first condition in either case

    public function actionCharges($id){
    $model= \app\models\IpdCharges::findOne(['doctor'=>$id]);
    $query = (new \yii\db\Query())
    ->select('tpa_name')
    ->from('patient_detail') 
    ->innerJoin('daily_ward_entry', 
   'daily_ward_entry.general_regn_no = patient_detail.general_regn_no')
    ->where(['daily_ward_entry.id'=>$id]);

   $command = $query->createCommand();
   $rows = $command->queryAll();



 if ($rows['tpa_name'] === NULL){
    return \yii\helpers\Json::encode([      
        'visit_charges'=>$model->charges_cash,        
    ]);
    }else {
       return \yii\helpers\Json::encode([         
        'visit_charges'=>$model->charges_cashless,
            ]);
       }
    }

Need a little clue what I am doing wrong here?


回答1:


$model1= \app\models\Tpa::findOne(['tpa_name']);

You are specifying this condition wrong. You only provide column name without any condition. Also you need to check $model for existence too.



来源:https://stackoverflow.com/questions/27405760/yii2-conditional-fill-of-json-data

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!