2015-10-21 52 views
0

我使用這個代碼讓所有用戶在數據庫排除某些領域中的findAll Symfony2的

$users= $this->getDoctrine() 
     ->getRepository('AppBundle:Users') 
     ->findAll(); 
     return $this->render('livre/users.html.twig',array(
      'users' => $users, 
     )); 

但我想只得到某些領域sush爲nameemail和隱藏像password .. 謝謝領域。

+1

你有沒有考慮過在視圖中不提供密碼? – jcroll

+0

對不起,我不明白 – user3057822

回答

4

您可以通過這種方式做到這一點:

1 /創建在UserRepository類的具體方法:

public function findByNameAndEmail() 
{ 
    $query = $this->createQueryBuilder('u') 
     ->select('u.name, u.email') // where name & email are properties of your User entity 
    ; 

    return $query->getQuery()->getResult(); 
} 

2 /和,稱它在你的控制器:

public function someAction()  
{ 
    $users = $this->getDoctrine()->getRepository('AppBundle:Users')->findByNameAndEmail(); 

    return $this->render('livre/users.html.twig',array(
     'users' => $users, 
    )); 
} 
+0

如果因爲任何原因你需要對象而不只是一個數組結果看看PARTIAL語法(這是部分水合對象):http://doctrine-orm.readthedocs.org/projects/學說-ORM/EN /最新/參考/局部objects.html – LBA