<?php

namespace Club\MatchBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * LeagueRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class LeagueRepository extends EntityRepository
{
  public function getTop(\Club\MatchBundle\Entity\League $league, $limit=4)
  {
    $rank = $this->_em->createQueryBuilder()
      ->select('lt')
      ->from('ClubMatchBundle:LeagueTable', 'lt')
      ->where('lt.league = :league')
      ->orderBy('lt.point', 'DESC')
      ->setMaxResults($limit)
      ->setParameter('league', $league->getId())
      ->getQuery()
      ->getResult();

    return $rank;
  }

  public function getUsersBySearch(\Club\MatchBundle\Entity\League $league, $query)
  {
    return $this->_em->createQueryBuilder()
      ->select(array('l', 'u'))
      ->from('ClubMatchBundle:League','l')
      ->leftJoin('l.users', 'u')
      ->leftJoin('u.profile','p')
      ->where('l.id = :league')
      ->andWhere("u.member_number = :number OR CONCAT(CONCAT(p.first_name,' '), p.last_name) LIKE :query")
      ->setParameter('league', $league->getId())
      ->setParameter('number', $query)
      ->setParameter('query', '%'.$query.'%')
      ->getQuery()
      ->getOneOrNullResult();
  }
}
