<?php

namespace AppBundle\Entity;
use Doctrine\ORM\Query\Expr\OrderBy;

/**
 * FewoLodgingRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class FewoLodgingRepository extends \Doctrine\ORM\EntityRepository
{
    /**
     * @param FewoLodging $lodging
     *
     * @return FewoSeason|null
     */
    public function findLatestSeasonEndForLodging($lodging)
    {
        $qb = $this->getEntityManager()->createQueryBuilder();
        $qb->select('s');
        $qb->from('AppBundle:FewoSeason', 's');
        $qb->innerJoin('s.prices', 'p');
        $qb->where($qb->expr()->eq('p.lodging', $lodging->getId()));
        $qb->addOrderBy('s.toDate', 'DESC');
        $qb->setMaxResults(1);
        return $qb->getQuery()->getOneOrNullResult();
    }

}
