简体   繁体   中英

How can I get a user(who is the current login) points from database Symfony2

I need to show just the points from the current user logged with doctrine and symfony2. I have a User and a table Points with user_id. I need to show just the user logged. With this code, i'm showing all the points from the table.

Here is my controller

public function indexAction()
{   
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('AtividadeBundle:Points')->findAll();

    return array(
        'entities' => $entities,
    );

}

Thanks!

inside your controller:

$id= $this->get('security.context')->getToken()->getUser()->getId();
$entities = $em->getRepository('AtividadeBundle:Points')->findBy(array('user'=>$id));
$id= $this->getUser()->getId();//more simple and short way to get the current user
$entities = $em->getRepository('AtividadeBundle:Points')->findBy(array('user'=>$id));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM