Lumen + Doctrine findAll返回空对象数组

时间:2016-06-20 10:38:32

标签: serialization doctrine lumen

我用Doctrine(laravel-doctrine / orm)设置了Lumen

当我尝试在HTTP路由上获取结果(即$ this-&gt; em-&gt; getRepository(Student :: class) - &gt; findAll();)时,我得到一个空的大括号数组。< / p>

如何正确运行序列化?

1 个答案:

答案 0 :(得分:1)

首先,您需要使用Illuminate\Support\Collection类包装结果,例如:

use  Illuminate\Support\Collection;

return Collection::make(
    $this->em->getRepository(Student::class)->findAll()
);

之后,修改您的Student课程,将其分配给Illuminate\Contracts\Support\Arrayable合同。

use Illuminate\Contracts\Support\Arrayable;

class Student implements Arrayable
{
    // Your code here
}

接下来,您应该为toArray课程实施Student方法:

use Illuminate\Contracts\Support\Arrayable;

class Student implements Arrayable
{
    public function toArray()
    {
        return [
            'id' => $this->getId(),
            // ... and so on ...
        ];
    }
}