<?php

namespace VGM\WOBundle\Repository;

use Doctrine\ORM\EntityRepository;

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

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

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

        $query = $qb->getQuery();

        return $query->getResult();
    }

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

        $query = $qb->getQuery();

        return $query->getSingleScalarResult();
    }

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

            $query = $qb->getQuery();

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

    public function getAll()
    {
        $qb = $this->createQueryBuilder('wt');

        $query = $qb->getQuery();

        return $query->getResult();
    }
}
