user3057822
user3057822

Reputation: 55

Exclude some fields in findAll Symfony2

I use this code for getting all users in the database

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

But me I want get only some fields sush as name,email and hide fields like password.. Thanks.

Upvotes: 1

Views: 3033

Answers (1)

scoolnico
scoolnico

Reputation: 3135

You can do it by this way:

1/ Create a specific method in the UserRepository class:

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/ And, call it in your controller:

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

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

Upvotes: 6

Related Questions