Sub zapytanie symfony

 /**
    * get oldest client category by date import having clientsource
    * 
    * @param int $clientId
    * @param array $categories (values example ->  [ 159 => 182 , 160 => "182_167"] )
    * @param \DateTime $datetimeBegin
    * @param \DateTime $datetimeEnd
    * 
    * @return array|null
    */
   public function getOldestClientCategoryByDateImportDateOfEntryHavingClientSource(int $clientId, array $categories, \DateTime $datetimeBegin, \DateTime $datetimeEnd) {
        $query = $this->createQueryBuilder('kk')
                ->where('kk.client = :client')->setParameter('client', $clientId)
                ->andWhere('kk.client_source IS not NULL');

        $categoriesWhere = '';

        if (!empty($categories)) {
            foreach ($categories as $category) {
                if (is_integer($category)) {
                    $categoriesWhere .= ($categoriesWhere != '' ? ' or ' : '') . '( kk.kategorie_nadrzedna = \'' . $category . '\') ';
                } else {
                    $categoryPart = explode('_', $category);
                    if (count($categoryPart) == 2) {
                        $categoriesWhere .= ($categoriesWhere != '' ? ' or ' : '') . '( kk.kategorie_nadrzedna = \'' . $categoryPart[0] . '\' and kk.kategorie = \'' . $categoryPart[1] . '\') ';
                    }
                }
            }

            if ($categoriesWhere != '') {
                $query->andWhere($categoriesWhere);
            }
        }
        
        $query->andWhere('kk.data_wpisania >= :DatetimeBegin')->setParameter('DatetimeBegin', $datetimeBegin)
                ->andWhere('kk.data_wpisania <= :DatetimeEnd')->setParameter('DatetimeEnd', $datetimeEnd);

        $query->orderBy('kk.data_importu', 'asc')->setMaxResults(1);

        try {
            return $query->getQuery()->getOneOrNullResult();
        } catch (\Doctrine\ORM\NoResultException $ex) {
            return null;
        }
    }
Komentarze wyłączone