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

Tuesday, March 8, 2022

[FIXED] Returning JSON after a query in yii

 March 08, 2022     json, php, rest, yii     No comments   

Issue

I'm new to yii framework. I'm just trying to implement Restful APIs. In the simple case scenario (after following some tutorial) I've succeeded with this:

$result = [];
foreach($this->getData() as $record) {
    $result[] = $record->getAttributes();
}
return $result;

Note that getData() is a built-in method. Where when trying to use queries for more advanced scenarios, it's done this way:

$attributeNames = 'udid,model,appverionid';
$connection = Yii::app()->db;
$command = $connection->createCommand('select ' . $attributeNames . ' from device');
$models = $command->queryAll();

$attributeNames = explode(',', $attributeNames);
$rows = array();
foreach ($models as $model) {
  $row = array();
  foreach ($attributeNames as $name) {
    $row[$name] = CHtml::value($model, $name);
  }
  $rows[] = $row;
}
return $rows;

Is that the best practice to return get JSON from queries results, or maybe it can be done in a better way?

Update:

The final response is returned from the following method:

private function sendAjaxResponse(AjaxResponseInterface $interface)
{
    $success = count($interface->getErrors()) === 0;
    $responseCode = $success ? 200 : 404;
    header("content-type: application/json", true, $responseCode);
    echo json_encode([
    'success' => $success,
    'data' => $interface -> getResponseData(),
    'errors' => $interface -> getErrors()
    ]);
    Yii::app()->end();
}

And I found out that only these lines are sufficient:

$attributeNames = 'udid,model,appverionid';
$connection = Yii::app()->db;
$command = $connection->createCommand('select ' . $attributeNames . ' from device');
$models = $command->queryAll();
return $models;

The other part (the nested loop) seems for encoding with relations (see here) Then my question is what is encoding with relations and when it is helpful?


Solution

The Yii way of creating JSOn data is using CJson library

$query = "'select ' . $attributeNames . ' from device'";
$command = Yii::app()->db->createCommand($query);
$result = $command->queryAll();

$ret = array_values($result);

/* or ...
  $ret = array();
  foreach ($models as $model) {
      $ret = array();
      foreach ($attributeNames as $name) {
          $row[$name] = CHtml::value($model, $name);
      }
      $ret[] = $row;
  }
*/

echo CJSON::encode($ret);
Yii::app()->end();


Answered By - crafter
  • 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