Yii2:有条件地填充Json数据 [英] Yii2: Conditional fill of Json data

查看:181
本文介绍了Yii2:有条件地填充Json数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个类似ipd_charges的表

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?

推荐答案

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

您将此条件指定为错误.您仅提供列名,没有任何条件. 另外,您还需要检查$model是否存在.

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

这篇关于Yii2:有条件地填充Json数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆