Cakephp 3.0 REST API在API响应中添加条件

时间:2016-11-22 08:58:08

标签: cakephp cakephp-3.0

我们可以在xml响应中设置条件吗?就像数据库中的状态字段是数字一样,如0,1,2,3,我们想要显示如下

0 for => Complete
2 for => Cancelled
2 for => Return
5 for => Refund.

如果数据库中不存在字段,我们如何在xml响应中添加新字段?

2 个答案:

答案 0 :(得分:0)

假设您正在使用rest API从Cakephp 3.0中的Sales表中获取销售报表。 如果您想自定义,重命名,添加几个字段以响应Rest API您可以像下面这样做

REST API URL将如下所示

https://example.com/api/index.json?fromdate_utc=2016-10-03&todate_utc=2016-10-03

控制器中的functino如下所示: -

 public function index($fromdate = null, $todate = null)
    {
        //set date range for 
        if(!empty($_GET['fromdate_utc']) && !empty($_GET['todate_utc'])){
            // if from amd to date are same add +1 day in to date to get result 
            $to_date = date('Y-m-d', strtotime($_GET['todate_utc'] . ' +1 day'));
            $dateRage = array('Sales.SalesDate >= ' => $_GET['fromdate_utc'], 'Sales.SalesDate <=' => $to_date);
        }else{
            $dateRage = array();                    
        }

        $conditions = array(
            'and' => array($dateRage),
        );

        //$this->Auth->allow();
        $sales = $this->Sales->find('all', array(
                        'conditions' => $conditions
                    ))
                    ->select(['SalesNo', 'SalesDate', 'TotalValue', 'TotalDiscount', 'NetTotal', 'PaymentMode', 'Status'])
                    ->where(['StoreId' => '8','Status  !=' => '2','Status  !=' => '4'])->andWhere(['Status !=' => 1])->andWhere(['Status !=' => 4]);
                    //->limit(3);
        $salesResponse = array();
        //echo '<br/>Count no of output => '.$sales->count();
        if($sales->count() >0 ){
            foreach($sales as $key=>$salesData){
                //re generate the array as per your requirements
                $salesResponse[$key]['SalesNo']         = $salesData->SalesNo;
                $salesResponse[$key]['SalesDate']   = $salesData->SalesDate;
                $salesResponse[$key]['TotalValue']      = $salesData->TotalValue;
                $salesResponse[$key]['TotalDiscount']   = $salesData->TotalDiscount;
                $salesResponse[$key]['NetTotal']        = $salesData->NetTotal;
                $salesResponse[$key]['SaleTax']         = 0; // add new fields that does not exist in database
                $salesResponse[$key]['PaymentMode']     = $salesData->PaymentMode;
                // change the status from numeric to character and pass it to response
                if($salesData->Status == 5){
                    $salesResponse[$key]['Status']      = 'Refund';
                }elseif($salesData->Status == 3){
                    $salesResponse[$key]['Status']      = 'Return';
                }elseif($salesData->Status == 2){
                    $salesResponse[$key]['Status']      = 'Cancelled';
                }elseif($salesData->Status == 0){
                    $salesResponse[$key]['Status']      = 'Complete';
                }
            }
        }else{
            $salesResponse = array('Error ! sorry not found any record.');
        }

        $this->set([
            'sales' => $salesResponse,
            '_serialize' => ['sales']
        ]);
    }

答案 1 :(得分:0)

如果您对XML中的状态名称稍有不同,则可以创建一个名为status_text的虚拟字段。在Sale实体类中添加protected $_virtual = ['status_text'];,它根据状态整数返回所需的文本,并将def my_method(self, arg_my_field1): return self.my_model_c.join(MyModelA).filter(MyModelA.my_field1.ilike(arg_my_field1)).one().my_field3 添加到Sale类,您应该开始在XML中获取状态文本。