我有一个模型
/**
* @ORM\Table(name="polygon")
* @ORM\Entity(repositoryClass="MyBundle\Repository\PolygonRepository")
* @JMS\ExclusionPolicy("none")
*/
class Polygon {
/**
* @var string
*
* @ORM\Column(name="polygon", type="json_array")
* @JMS\Type("array<MyBundle\Model\Point>")
*/
private $points;
/***/
}
在 DB 中,它的存储方式类似于文本 '[{"x":1, "y":1} ...]'
在控制器中我有
/**
* Matches /polygon exactly
*
* @Route("/", name="polygon_list")
* @Method("GET")
*
* @Rest\Get("/")
*
*/
public function listAction()
{
return $this->container->get('doctrine.orm.entity_manager')
->getRepository('MyBundle:Polygon')
->findAll();
}
所以我得到 ReflectionProperty::getValue() 期望参数 1 是对象,给定数组
在 ...vendor/jms/metadata/src/Metadata/PropertyMetadata.php:51
如果只是为了得到结果,可以通过使用虚拟属性来解决
/**
* @JMS\Exclude
*/
private $points;
/**
* @JMS\VirtualProperty
* @JMS\Type("array<MyBundle\Model\Point>")
* @JMS\SerializedName("points")
* @JMS\Groups({"common"})
*
* @return array
*/
public function getMyPoints() {
return [new Point(), new Point()]
}
但是我需要从 POST 以 JSON 形式接收这些点,所以到目前为止我发现的唯一方法是类似于 https://github.com/sonata-project/sonata-doctrine-extensions的 Doctrine 自定义类型
唯一不同的是,在 convertToPHPValue 方法中,我添加了额外的类型转换来接收对象而不是 assoc 数组:
// pass my [{"x":1, "y":1} ...]
public function convertToPHPValue($value, AbstractPlatform $platform)
{
return array_map(function($a){ return (object)$a;}, json_decode($value));
}
有没有更干净的解决方案,而不添加自定义 Doctrine 序列化?
如果只有这个 ...vendor/jms/metadata/src/Metadata/PropertyMetadata.php:51 有
return $this->reflection->getValue((object)$obj);
但它是
return $this->reflection->getValue($obj); // :(