PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Wednesday, March 9, 2022

[FIXED] Cakephp 3.0 REST API add conditions in API response

 March 09, 2022     cakephp, cakephp-3.0     No comments   

Issue

Can we set conditions in xml response. Like there is status fields in database that is in numeric like 0,1,2,3 and we want to show it like below

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

How can we add a new fields in xml response if the fields does not exist in database ?


Solution

Lets suppose you are fetching the sales report from Sales table in Cakephp 3.0 Using rest API. If you want to customize, rename, add few fields in response of Rest API You can do it like below

REST API URL will be something like below

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

And functino in controller be something like below:-

 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']
        ]);
    }


Answered By - kantsverma
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing