2016-12-30 2 views
0

私はdoctrine2にzend framework 1.12と統合されたこのモジュラーウェブアプリケーションを持っています。要求に応じてオブジェクトベースを返すuri

私はリクエストURIに1つのオブジェクトベースを戻したいと思います。例えば、url/api/people/1は、自分のidに1つのベースを返します。

*注釈、私は人のすべてのオブジェクトを返すことができます、私はちょうど1つのオブジェクトを選択したいと思います。

私が試したことは、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); 

***** ***** EDIT だから私はどのように私はこれを達成することができ、私は手動で一人を見つけることができますが、私はURLからそれをやりたいのコードを追加していますか?

答えて

0

試してみてください。

$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

+0

どちらも、質問が持っているものと同じです。 – Cerad

+0

@ Kerad Doctrineがエンティティを見つけることができないが、要求の$ idが正しいという問題があると私は推測します。 Paul Chuさん、もっと情報をくれますか? – Hokusai

+0

@hokusaiですので、あなたの質問にうまく答えるために投稿を編集しました。 –

関連する問題