Return 基于请求 uri 的对象
Return object base on request uri
所以我有这个模块化的 Web 应用程序,它有 doctrine2 与 zend 框架 1.12 集成。
所以我想return一个基于请求uri的对象,例如:url/api/people/1,其中return一个人基于他们的id。
*旁注,我可以 return 人的所有对象,一旦用户输入数字,我只想 select 一个对象。
我尝试过的是从 url 中获取参数并通过 find 函数传递它,但是 doctrine 不理解这些通过 uri 传递的数字,并认为它们是动作.
if($this->getRequest()->isGet())
{
$request = $this->getRequest();
$id = $request->getParam('peopleId');
$em = $this->getEntityManager();
$peopleRepo = $em->getRepository('API\Entity\People');
$people = $peopleRepo->find(3); //$id goes into find function
$resultArray[] =
[
'id' => $people->getId(),
'firstname' => $people->getFirstName(),
'lastname' => $people->getLastName(),
"food" => $people->getFavoriteFood()
];
echo json_encode($resultArray, JSON_PRETTY_PRINT);
var_dump($people);
*****编辑*****
所以我添加了代码,我可以手动找到一个人,但我想通过 url 来实现,我该如何实现?
尝试:
$peopleRepo = $em->getRepository('API\Entity\People')->findBy(array('id' => $id));
或者
$peopleRepo = $em->getRepository('API\Entity\People')->findById($id);
所以我有这个模块化的 Web 应用程序,它有 doctrine2 与 zend 框架 1.12 集成。
所以我想return一个基于请求uri的对象,例如:url/api/people/1,其中return一个人基于他们的id。
*旁注,我可以 return 人的所有对象,一旦用户输入数字,我只想 select 一个对象。
我尝试过的是从 url 中获取参数并通过 find 函数传递它,但是 doctrine 不理解这些通过 uri 传递的数字,并认为它们是动作.
if($this->getRequest()->isGet())
{
$request = $this->getRequest();
$id = $request->getParam('peopleId');
$em = $this->getEntityManager();
$peopleRepo = $em->getRepository('API\Entity\People');
$people = $peopleRepo->find(3); //$id goes into find function
$resultArray[] =
[
'id' => $people->getId(),
'firstname' => $people->getFirstName(),
'lastname' => $people->getLastName(),
"food" => $people->getFavoriteFood()
];
echo json_encode($resultArray, JSON_PRETTY_PRINT);
var_dump($people);
*****编辑***** 所以我添加了代码,我可以手动找到一个人,但我想通过 url 来实现,我该如何实现?
尝试:
$peopleRepo = $em->getRepository('API\Entity\People')->findBy(array('id' => $id));
或者
$peopleRepo = $em->getRepository('API\Entity\People')->findById($id);