Symfony2 get user id in entity repository
Asked Answered
P

2

6

I have coded a page which displays all administrators of the system. What I want to do is customize my query so that it would exclude the currently authenticated user from the list.
Now I know I can get the user_id from the controller and pass it to the entity repository, but I was wondering if there is a way to access that directly through the Entity Repository?

For example:

use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\NoResultException;

use Symfony\Component\Security\Core\User\UserInterface;
use Symfony\Component\Security\Core\User\UserProviderInterface;
use Symfony\Component\Security\Core\Exception\UsernameNotFoundException;
use Symfony\Component\Security\Core\Exception\UnsupportedUserException;

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
   public function getAdmins($int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc')){
        $admin_id = fetch the admin id ?;
        $query = $this->createQueryBuilder('admins')
            ->where("admins.admin_id != '".$admin_id."'")
            ->orderBy('admins.'.$orderBy[0], $orderBy[1])
            ->setFirstResult($offset)
            ->setMaxResults($int)
            ->getQuery()
            ->getResult();
        return $query;
   }
}
Pederast answered 7/12, 2012 at 0:36 Comment(0)
J
1

So, you have a current User ID (let's say, $currentUserId), and you want to write a query for all Users with an ID that is not equal to $currentUserId?

Try this:

class SomeController
{
    public function someAction()
    {
        ...

        $qb = $userRepository->createQueryBuilder("user")
            ->where("user.id != ?1")
            ->setParameter(1, $currentUserId);

        $usersExcludingCurrent = $qb->getQuery()->getResult();

    ...
    }
}

edit

Ahh, I see what you're saying...

Well, the repository really should be blind to anything outside of itself. That is, it's not container-aware, and should therefore not know who the current user is.

My suggestion would be to give your repository a function that gets everything except for a particular User or User ID. Something like:

class AdminUserRepository extends EntityRepository implements UserProviderInterface
{
    public function getAllAdminsExcept($userId, $int = 10, $offset = 0, array $orderBy = array('admin_id', 'asc'))
        {
            ...
        }
}

And then put the "current User" logic into your controller. You could even define a service that has access to both @security and @doctrine, and house all the logic there. Your call, but I'd say that you should definitely keep your repository unaware of anything going on in the security service.

Jasso answered 7/12, 2012 at 0:47 Comment(2)
I updated my question. I know I could do it the way you're saying, but I'm looking if it's possible to be done in the EntityRepository itself.Pederast
I was thinking the same thing but it's kind of logical (for me) to be able to access authenticated user data from within a repository. Thanks for your help :)Pederast
P
-4

You can retrieve user object by

public function indexAction()
{
    $user = $this->getUser();
}

trough controller or view by Twig Template:

<p>Username: {{ app.user.username }}</p>

this sample come from symfony 2 doc.

Peyton answered 7/12, 2012 at 0:40 Comment(1)
You can't in the repositoryLehet

© 2022 - 2024 — McMap. All rights reserved.