0

I'm using symfony2 and doctrine. I just want to check if specific row in table exist or not. no need to get back object of that Entity if exist. for example if I use something like this to check if someone with ip of $clientIp visit video I JUST WANT TRUE OR FALSE not VideoVisit instance.

$query = $this->getEntityManager()->createQuery(
                'SELECT v FROM MjVideoBundle:VideoVisit v'
                . ' WHERE v.video=:videoId'
                . ' AND v.ip=:clientIp')
                ->setParameters(array(
                    'videoId'=> $videoId,
                    'clientIp' => $clientIp));

        try{
        $result = $query->getSingleResult();
    } catch (\Doctrine\Orm\NoResultException $ex) {
        $result = false;
    }

    if($result == false){
        //do something
     }
     else{
        //do something else
     }
mohsenJsh
  • 2,048
  • 3
  • 25
  • 49
  • 2
    Use getScalerResult() to count the number of rows matching your conditions. Check my answer here: http://stackoverflow.com/questions/9214471/count-rows-in-doctrine-querybuilder/9215880#9215880 – Cerad Jun 23 '14 at 20:06

1 Answers1

0

That's a lot of work you're doing. Try using the much simpler Doctrine functions that are already provided to you:

$video = $this->getDoctrine()->getRepository('MjVideoBundle:Video')->find($videoId);
$result = $this->getDoctrine()->getRepository('MjVideoBundle:VideoVisit')->findOneBy(
    array('video' => $video, 'clientIp' => $clientIp)
);

if ($result) {
    // do something if found
} else {
    // do something if not found
}

Notice how you have to search using the video object too, you don't just search by video ID in the where conditions (unless you haven't set up your association mapping properly.) I've assumed the name of your Video entity and also assumed the $videoId is always valid.

sjagr
  • 15,983
  • 5
  • 40
  • 67