<?php

namespace VGM\WOBundle\Repository;

use Doctrine\ORM\EntityRepository;

/**
 * ClientRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class ClientRepository extends EntityRepository
{
    public function getClients($max = null, $offset = null)
    {
        $qb = $this->createQueryBuilder('c')->orderBy('c.name');

        if($max)
        {
            $qb->setMaxResults($max);
        }

        if($offset)
        {
            $qb->setFirstResult($offset);
        }

        $query = $qb->getQuery();

        return $query->getResult();
    }

    public function getbyId($id = null)
    {
        if($id){
            $qb = $this->createQueryBuilder('cl')
                ->where("cl.id =:id")
                ->setParameter('id', $id);

            $query = $qb->getQuery();

            return $query->getSingleResult();
        }
        else
            null;
    }

    public function countClients()
    {
        $qb = $this->createQueryBuilder('c')->select('count(c.id)');

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }
}
