json yii2 mysql,Yii2宁静的API:显示来自数据库中的数据与特定条件SQL JSON格式

I'm working on Yii2 restful API and want to display the data into JSON format. This is my structure database:

TABLE `volunteer`(

`volunteer_id` int(11) NOT NULL auto_increment,

`state_id` int(11) null

`nama` varchar(200) null

TABLE `state`(

`state_id` int(11) NOT NULL auto_increment,

`state` varchar(225) null

Basically, when I run at browser with specific ID = 1 (http://localhost/KDMA/web/index.php/volunteers/1) the data will display like this:

{

"volunteer_id": "1",

"state_id":"12",

"nama": "Bentong",

}

that result is display the data from volunteer_id = 1. So, what I want to do right now is display the data from state_id, not volunteer_id. For example in SQL:

SELECT * FROM volunteer where state_id = 12;

What are the ways that can solve my problem?

解决方案

If I understand your question I think you can solve the problem as follows:

try adding another method in the controller that performs the action you need eg in volunteerController

public function actionViewByState ($ id)

{

if (($ model = State :: findOne ($ id))! == null) {

$ this-> setHeader (200);

echo json_encode (array ('status' => 1, 'date' => array_filter ($ model-> attributes)), JSON_PRETTY_PRINT);

} Else {

$ this-> setHeader (400);

echo json_encode (array ('status' => 0, 'error_code' => 400, 'message' => 'Bad request'), JSON_PRETTY_PRINT);

exit;

}

}

and then call the action with

http: //localhost/KDMA/web/index.php/volunteers/view-by-state/1

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值