我正在创建一个小的API,主要用于学习目的,但是,我可以将其实现到我正在从事的项目中。到目前为止,我已经安装了zend表达框架应用程序并设置了模型和实体。我可以查询数据库并获取结果,但是当我将结果作为JSON响应返回时,我只能看到每个结果的空数组列表。我希望能够返回从数据库返回的实际对象,而不是将它们转换为数组。
HomePageHandler.php
<?php
declare(strict_types=1);
namespace App\Handler;
use App\Entity\Product;
use Psr\Http\Message\ResponseInterface;
use Psr\Http\Message\ServerRequestInterface;
use Psr\Http\Server\RequestHandlerInterface;
use Zend\Diactoros\Response\HtmlResponse;
use Zend\Diactoros\Response\JsonResponse;
use Zend\Expressive\Router;
use Zend\Expressive\Template\TemplateRendererInterface;
use App\Model\ProductModel;
class HomePageHandler implements RequestHandlerInterface
{
/** @var string */
private $containerName;
/** @var Router\RouterInterface */
private $router;
/** @var null|TemplateRendererInterface */
private $template;
private $productModel;
public function __construct(
string $containerName,
Router\RouterInterface $router,
?TemplateRendererInterface $template = null,
ProductModel $productModel
) {
$this->containerName = $containerName;
$this->router = $router;
$this->template = $template;
$this->productModel = $productModel;
}
public function handle(ServerRequestInterface $request) : ResponseInterface
{
$data = $this->productModel->fetchAllProducts();
return new JsonResponse([$data]);
//return new HtmlResponse($this->template->render('app::home-page', $data));
}
}
我期望返回的JSON响应包含18个“产品”实体的列表。我的结果看起来像。
[[{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{},{}]]
让我知道您是否还想查看其他代码。 预先感谢!
使用Product.php代码编辑
<?php
/**
* Created by PhpStorm.
* User: Brock H. Caldwell
* Date: 3/14/2019
* Time: 4:04 PM
*/
namespace App\Entity;
class Product
{
protected $id;
protected $picture;
protected $shortDescription;
protected $longDescription;
protected $dimensions;
protected $price;
protected $sold;
/**
* @return mixed
*/
public function getId()
{
return $this->id;
}
/**
* @param mixed $id
*/
public function setId($id)
{
$this->id = $id;
}
/**
* @return mixed
*/
public function getPicture()
{
return $this->picture;
}
/**
* @param mixed $picture
*/
public function setPicture($picture)
{
$this->picture = $picture;
}
/**
* @return mixed
*/
public function getShortDescription()
{
return $this->shortDescription;
}
/**
* @param mixed $shortDescription
*/
public function setShortDescription($shortDescription)
{
$this->shortDescription = $shortDescription;
}
/**
* @return mixed
*/
public function getLongDescription()
{
return $this->longDescription;
}
/**
* @param mixed $longDescription
*/
public function setLongDescription($longDescription)
{
$this->longDescription = $longDescription;
}
/**
* @return mixed
*/
public function getDimensions()
{
return $this->dimensions;
}
/**
* @param mixed $dimensions
*/
public function setDimensions($dimensions)
{
$this->dimensions = $dimensions;
}
/**
* @return mixed
*/
public function getPrice()
{
return $this->price;
}
/**
* @param mixed $price
*/
public function setPrice($price)
{
$this->price = $price;
}
/**
* @return mixed
*/
public function getSold()
{
return $this->sold;
}
/**
* @param mixed $sold
*/
public function setSold($sold)
{
$this->sold = $sold;
}
public function exchangeArray($data)
{
$this->id = (!empty($data['id'])) ? $data['id'] : null;
$this->picture = (!empty($data['picture'])) ? $data['picture'] : null;
$this->shortDescription = (!empty($data['shortDescription'])) ? $data['shortDescription'] : null;
$this->longDescription = (!empty($data['longDescription'])) ? $data['longDescription'] : null;
$this->dimensions = (!empty($data['dimensions'])) ? $data['dimensions'] : null;
$this->price = (!empty($data['price'])) ? $data['price'] : null;
$this->sold = (!empty($data['sold'])) ? $data['sold'] : null;
}
}
答案 0 :(得分:1)
您需要公开属性,或在JsonSerializable
实体中实现Product
接口。它的所有属性都是受保护的,这很好,但这意味着当对象经过JSON编码时不会暴露它们。
以下是一些简短的示例:
class Example1 { protected $a = 1; }
class Example2 { public $b = 2; }
class Example3 implements JsonSerializable {
protected $c = 3;
public function jsonSerialize() {
return ['c' => $this->c];
}
}
echo json_encode([new Example1, new Example2, new Example3]);
结果:
[{},{"b":2},{"c":3}]
如果您选择实现JsonSerializable
,则具体取决于您的操作,但是您只需要一个jsonSerialize()
方法,该方法以JSON可访问的格式返回所需的JSON结果属性。 json_encode
(具有公共属性或数组的对象)。