简体   繁体   中英

Is Api Result the same as the frontend result?

In my code I am trying to get access my data bank through query builder, everything works I am able to get access to everything as a string (which this is what I want), everything but my date is returned as a class DateTime object in my Frontend, the intersting part for me through Api I am getting my date as a string and not as a class DateTime object and I cant understand why I am getting two different results when I am using the same methods same query builder and my Question is why am I getting two different results? is it possible to get different result through Api? and if so why? and is there a way to convert the class DateTime object to a string ?

Api Controller

public function indexAction()
    {

        $request = $this->Request();
        $limit = $request->getParam('limit', 1000);
        $offset = $request->getParam('start', 0);
        $sort = $request->getParam('sort', []);
        $filter = $request->getParam('filter', []);

        $result = $this->resource->getList($offset, $limit, $filter, $sort);

        $view = $this->View();
        $view->assign($result);
        $view->assign('success', true);
    }

Fronend Controller

public function listAction()
{
    $feedback= $this->resource->getList(0, 10, null, null);

    $this->View()->assign('feedback', $feedback);

}

QueryBuilder

protected function getBaseQuery()
{
    $builder = $this->getManager()->createQueryBuilder();
    $builder->select(['feedback', 'user_id.firstname','user_id.lastname',])
        ->from(FeedbackModel::class, 'feedback')
        ->leftJoin('feedback.customer', 'user_id');
    return $builder;
}

getList function

public function getList($offset, $limit, $filter, $sort)
{
    $this->checkPrivilege('read');
    $builder = $this->getBaseQuery();

    $builder->setFirstResult($offset)
        ->setMaxResults($limit);


    if (!empty($filter)){
        $builder->addFilter($filter);
    }

    if (!empty($sort)){
        $builder->addOrderBy($sort);
    }

    $query = $builder->getQuery();
    $query->setHydrationMode($this->getResultMode());

    $paginator = $this->getManager()->createPaginator($query);
    $totalResult = $paginator->count();
    $feedback = $paginator->getIterator()->getArrayCopy();



    return ['data' => $feedback , 'total' => $totalResult];
}

Api result

data    
0   
0   
id  1
feedback    "this shop is boring"
date    "2022-12-07T00:00:00+0100"
public  true
firstname   "some"
lastname    "thing"
total   1
success true

Frontend Result

0 => Array (3)
  0 => Array (4)
    id => 1
    feedback => "this shop is boring"
    date => DateTime Object (0)
    public => true
  firstname => "some"
  lastname => "thing"
  total => 1
->nocache = null

How I fixed my Problem and what it was:

With the help of @ADyson I found out what the problem was. I was tackling the problem from the wrong way though I didn't need to convert my DateTime to string neither in my Model , in my Controller or my Query-builder the only problem was the way I was calling it, there is a way of calling date objects like this in smarty and its by using |date:'dd.MM.y' in my case it was {$feedbacks.date|date:'dd.MM.y'} this get the date inside of the class and converts it to a string at the same time and like that I got to call the date that I want.

To answer my Original Question:

Api and Frontend don't give different result but a different Format which is for the human eye a bit different with the use of different Tools such as PHPStorm Debugger, Postman and Smarty Debugger I got to see the different result, why I am getting these Kind of results and the data inside of the class DateTime in my case.

what helped me find a solution my to problem

  1. PHPStorm Debugger .
  2. Smarty Debugger
  3. Postman

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM