“没有办法直接告诉Doctrine将一个对象视为数组。但是,通过一些微调和一些有争议的设计决策,你可以实现这一点。
首先,你需要创建一个基类(供你的类继承),该基类实现了ArrayAccess接口。可以在
Doctrine cookbook中找到描述此过程的文章。它看起来像这样:”
abstract class DomainObject implements ArrayAccess
{
public function offsetExists($offset)
{
return isset($this->$offset);
}
public function offsetSet($offset, $value)
{
throw new BadMethodCallException(
"Array access of class " . get_class($this) . " is read-only!"
);
}
public function offsetGet($offset)
{
return $this->$offset;
}
public function offsetUnset($offset)
{
throw new BadMethodCallException(
"Array access of class " . get_class($this) . " is read-only!"
);
}
}
然后,当您创建模型类(或至少要将其视为数组的类)时,您需要扩展此DomainObject类。拼图的最后一块是将您的类属性设置为public,以便让json_encode函数能够检查您的类属性并将它们用作json对象的键。
NB:在类中使用公共属性可能会导致很多难以跟踪的错误,并且通常被认为是可疑的做法。这只是一个我快速想出来的示例,以说明如何实现。我相信有一种更优雅的方法可以实现这一点,而不需要公共属性。这个解决方案只是为了让事情开始运转。
一个示例域类可能如下所示:
class Tester extends DomainObject
{
public $foo;
public $bar;
public function __construct($foo, $bar)
{
$this->foo = $foo;
$this->bar = $bar;
}
}
现在,您将能够将 Tester 类的实例转换为数组,并将该数组传递给 json_encode 函数。
$test = new Tester('Hello', 'World');
echo json_encode((array)$test);
这将产生以下输出:
{"foo":"Hello","bar":"World"}
编辑:为了让你的代码片段回到上下文中,你不再需要使用HYDRATE_ARRAY,它应该像这样:
$results = $em->getRepository('MyBundle:Report')->findByEvaluation($evaluation_id);
foreach ($results as $result) {
echo json_encode((array)$result);
}
假设你的Report类扩展了上面定义的DomainObject类。