src/Repository/PropertyRepository.php line 82

Open in your IDE?
  1. <?php
  2. namespace App\Repository;
  3. use App\Entity\Property;
  4. use App\Entity\PropertySearch;
  5. use Doctrine\Bundle\DoctrineBundle\Repository\ServiceEntityRepository;
  6. use Doctrine\Common\Persistence\ManagerRegistry;
  7. use Doctrine\ORM\Query;
  8. use Doctrine\ORM\QueryBuilder;
  9. //use DoctrineExtensions\Query\Mysql;
  10. /**
  11.  * @method Property|null find($id, $lockMode = null, $lockVersion = null)
  12.  * @method Property|null findOneBy(array $criteria, array $orderBy = null)
  13.  * @method Property[]    findAll()
  14.  * @method Property[]    findBy(array $criteria, array $orderBy = null, $limit = null, $offset = null)
  15.  */
  16. class PropertyRepository extends ServiceEntityRepository
  17. {
  18.     public function __construct(ManagerRegistry $registry)
  19.     {
  20.         parent::__construct($registryProperty::class);
  21.     }
  22.     /**
  23.      * @param PropertySearch $search
  24.      * @return Query
  25.      */
  26.     public function findAllNotSoldPropertiesQuery(PropertySearch $search): Query {
  27.         $query $this->findNotSoldPropQuery();
  28.         if( $search->getMaxPrice() ){
  29.             $query $query->where('p.price < :maxprice')
  30.                     ->setParameter('maxprice'$search->getMaxPrice());
  31.         }
  32.         if( $search->getMinSurface() ){
  33.             $query $query->andWhere('p.surface > :minsurface')
  34.                     ->setParameter('minsurface'$search->getMinSurface());
  35.         }
  36.         /**
  37.          * N'ajoute le filtre dans la recherche que si il y a des options
  38.          */
  39.         if( $search->getPropertyOptions()->count() > ){
  40.             /**
  41.              * On boucle sur les options disponibles
  42.              * ATTENTION : :option$k permet de nommer différemment pour éviter l'écrasement de la donnée dans la recherche
  43.              * MAIS
  44.              * ne pas utiliser comme clé car faille de sécurité -> &propertyOptions[azeazeaze]=3
  45.              */
  46.             $k 0;
  47.             foreach( $search->getPropertyOptions() as $option ){
  48.                 $k++;
  49.                 dump($k);
  50.                 $query $query
  51.                     ->andWhere(":option$k MEMBER OF p.propertyOptions")
  52.                     ->setParameter(":option$k"$option);
  53.             }
  54.         }
  55.         if ($search->getLat() && $search->getLng() && $search->getDistance()) {
  56.             $query $query
  57.                 ->andWhere('(6353 * 2 * ASIN(SQRT( POWER(SIN((p.lat - :lat) *  pi()/180 / 2), 2) +COS(p.lat * pi()/180) * COS(:lat * pi()/180) * POWER(SIN((p.lng - :lng) * pi()/180 / 2), 2) ))) <= :distance')
  58.                 ->setParameter('lng'$search->getLng())
  59.                 ->setParameter('lat'$search->getLat())
  60.                 ->setParameter('distance'$search->getDistance());
  61.         }
  62.         return $query->getQuery();
  63.     }
  64. //    /**
  65. //     * @return Property[]
  66. //     */
  67. //    public function findAllNotSoldProperties(): array {
  68. //        return $this->findNotSoldPropQuery()
  69. //                    ->getQuery()
  70. //                    ->getResult();
  71. //    }
  72.     public function findLatest(): array {
  73.         return $this->findNotSoldPropQuery()
  74.                     ->setMaxResults(4)
  75.                     ->getQuery()
  76.                     ->getResult();
  77.     }
  78.     /**
  79.      * Used to some other queries
  80.      * @return QueryBuilder
  81.      */
  82.     private function findNotSoldPropQuery(): QueryBuilder{
  83.         return $this->createQueryBuilder('p')
  84.                     ->where('p.sold = 0')
  85.                     ->andWhere('p.deleted = 0')
  86.                     ->orderBy('p.updated_at''DESC');
  87.     }
  88.     // /**
  89.     //  * @return Property[] Returns an array of Property objects
  90.     //  */
  91.     /*
  92.     public function findByExampleField($value)
  93.     {
  94.         return $this->createQueryBuilder('p')
  95.             ->andWhere('p.exampleField = :val')
  96.             ->setParameter('val', $value)
  97.             ->orderBy('p.id', 'ASC')
  98.             ->setMaxResults(10)
  99.             ->getQuery()
  100.             ->getResult()
  101.         ;
  102.     }
  103.     */
  104.     /*
  105.     public function findOneBySomeField($value): ?Property
  106.     {
  107.         return $this->createQueryBuilder('p')
  108.             ->andWhere('p.exampleField = :val')
  109.             ->setParameter('val', $value)
  110.             ->getQuery()
  111.             ->getOneOrNullResult()
  112.         ;
  113.     }
  114.     */
  115. }