根据请求uri返回对象

时间:2016-12-30 16:40:36

标签: php zend-framework orm doctrine-orm uri

所以我有这个模块化的web应用程序,其中doctrine2与zend framework 1.12集成在一起。

所以我想在请求uri上返回一个对象,例如:url / api / people / 1,它会根据他们的id返回一个人。

*旁注,我可以返回所有人的对象,我只想在用户输入一个数字后选择一个对象。

我试过的是从网址中获取参数并将其传递给find函数,但是学说并不理解通过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);

***** EDIT ***** 所以我添加了代码,我可以手动找到一个人,但我想通过url这样做,我该怎么做到这一点?

1 个答案:

答案 0 :(得分:1)

尝试:

$peopleRepo = $em->getRepository('API\Entity\People')->findBy(array('id' => $id));

或者

$peopleRepo = $em->getRepository('API\Entity\People')->findById($id);

请参阅Doctrine文档: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#by-simple-conditions