2017-03-08 33 views
-1

我想創建一個API來檢索數據庫中使用symfony2原則的字段,但查詢總是返回未找到的結果。這是我嘗試使用symfony教條檢索數據庫表中的字段

/** 
* @Route("/get/{email}") 
*/ 
public function emailAction($email) 
{ 
    $singleresult = $this->getDoctrine()->getRepository('Api3Bundle:Influ')->find($email); 
    if ($singleresult === null) { 
    return new View("user not found", Response::HTTP_NOT_FOUND); 
    } 

請我怎麼能修改上面的網址,以便在調用URI端點讓它在參數返回的電子郵件。

+1

請閱讀[我該怎麼辦問一個好問題?](http://stackoverflow.com/help/how-to-ask)並相應地更新你的問題。 –

+0

Try - > findOneBy(array('email'=> $ email))而不是 - > find($ email) –

+0

Thanks Mathieu .... – Float

回答

0

如果您實體有一個名爲「電子郵件」字段,你可以使用findOneByEmail(),它是一個動態方法名基於列的值來找到一個對象:

->getRepository('Api3Bundle:Influ')findOneByEmail($email); 

你也可以使用findOneBy()方法來輕鬆獲取基於多個條件的對象,比如你要檢索電子郵件,但只是當它是積極的,在這種情況下,你可以通過和數組:

->getRepository('Api3Bundle:Influ')findOneBy(
     array('email' => $email, 'active' => 1) 
); 
相關問題