<?php
namespace App\Repository;
use App\Entity\EStateAd;
use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
use Doctrine\Common\Persistence\ManagerRegistry;
use Doctrine\ORM\Query;
use Doctrine\ORM\Query\ResultSetMapping;
/**
* @method EStateAd|null find($id, $lockMode = null, $lockVersion = null)
* @method EStateAd|null findOneBy(array $criteria, array $orderBy = null)
* @method EStateAd[] findAll()
* @method EStateAd[] findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
*/
class EStateAdRepository extends ServiceEntityRepository
{
public function __construct(ManagerRegistry $registry)
{
parent::__construct($registry, EStateAd::class);
}
public function getHomeAds()
{
return $query = $this->createQueryBuilder('e')
->innerJoin('e.owner_id', 'a')
->addSelect('a')
->where('e.status > 0')
->andWhere('e.is_home_display = 1')
->orderBy('e.created_at', 'DESC')
->setMaxResults(2)
->getQuery()
->getResult()
;
}
public function getListing()
{
return $query = $this->createQueryBuilder('e')
->innerJoin('e.owner_id', 'a')
->addSelect('a')
->where('e.status > 0')
->orderBy('e.mandate_type', 'ASC')
->addOrderBy('e.created_at', 'DESC')
->getQuery()
->getResult()
;
}
public function getStringListing()
{
return $query = $this->createQueryBuilder('e')
->innerJoin('e.owner_id', 'a')
->addSelect('a')
->where('e.status > 0')
->orderBy('e.mandate_type', 'ASC')
->addOrderBy('e.created_at', 'DESC')
->getQuery()
->getArrayResult()
;
}
public function getTotalAdsNb()
{
return $this->createQueryBuilder('e')
->where('e.status > 0')
->getQuery()
->getResult()
->count()
;
}
public function getNbAdsByAgencyId($ownerId)
{
return $this->createQueryBuilder('e')
->select('count(e.id)')
->where('e.status = 1')
->andWhere('e.owner_id = :ownerId')
->setParameter('ownerId', $ownerId)
->orderBy('e.created_at', 'DESC')
->getQuery()
->getSingleScalarResult()
;
}
public function getNbExclAdsByAgencyId($ownerId)
{
return $this->createQueryBuilder('e')
->select('count(e.id)')
->where('e.status = 1')
->andWhere('e.mandate_type = 0')
->andWhere('e.owner_id = :ownerId')
->setParameter('ownerId', $ownerId)
->orderBy('e.created_at', 'DESC')
->getQuery()
->getSingleScalarResult()
;
}
public function getNbSuboffersByAgencyId($ownerId)
{
return $this->createQueryBuilder('e')
->select('count(e.id)')
->where('e.status = 3')
->andWhere('e.owner_id = :ownerId')
->setParameter('ownerId', $ownerId)
->orderBy('e.created_at', 'DESC')
->getQuery()
->getSingleScalarResult()
;
}
public function getExpiredAds()
{
return $this->createQueryBuilder('e')
->where('e.mandate_type <= 1')
->andWhere('e.expiration_date <= :now')
->setParameter('now', new \DateTime('now', new \DateTimeZone('Europe/Paris')))
->getQuery()
->getResult()
;
}
public function getFilteredListing($data)
{
$cities = [];
if(isset($data['search_field']) && "" !== $data['search_field']){
$tagify = json_decode($data['search_field']);
foreach($tagify as $city){
array_push($cities, $city->value);
}
}
$entityManager = $this->getEntityManager();
$sql = "";
foreach($cities as $key => $city){
$sql .= " WHERE e.city = :city".$key." ";
}
$query = $this->createQueryBuilder('e')
->innerJoin('e.owner_id', 'a')
->addSelect('a')
->where('e.city = :city')
->andWhere('e.status > 0')
->setParameter('city', 'Paris');
/*$query = $entityManager->createQuery(
'SELECT e, a
FROM App\Entity\EStateAd e
INNER JOIN e.owner_id a' . $sql
);
dd('SELECT e, a
FROM App\Entity\EStateAd e
INNER JOIN e.owner_id a' . $sql);*/
return $query->getQuery()->getResult();
}
public function findOneByReference($ref)
{
return $this->createQueryBuilder('e')
->where('e.reference = :ref')
->setParameter('ref', $ref)
->getQuery()
->getSingleResult()
;
}
public function findByIds($ids)
{
$qb = $this->createQueryBuilder('e');
return $qb
->where($qb->expr()->in('e.id', $ids))
->getQuery()
->getResult()
;
}
// /**
// * @return EStateAd[] Returns an array of EStateAd objects
// */
/*
public function findByExampleField($value)
{
return $this->createQueryBuilder('e')
->andWhere('e.exampleField = :val')
->setParameter('val', $value)
->orderBy('e.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}
*/
/*
public function findOneBySomeField($value): ?EStateAd
{
return $this->createQueryBuilder('e')
->andWhere('e.exampleField = :val')
->setParameter('val', $value)
->getQuery()
->getOneOrNullResult()
;
}
*/
}