Repository/ProductRepository.php
public function findForSearchFilter(ProductFilter $filter, OrderCollection $orderCollection = null)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->from('HarvestCloudCoreBundle:Product', 'p')
            ->select('p')
            ->addSelect('GEO(p.latitude = :latitude, p.longitude = :longitude) AS distance')
            ->setParameter('latitude', $filter->getLatitude())
            ->setParameter('longitude', $filter->getLongitude())
            ->orderBy('distance')
            ->setMaxResults(20)
        ;

        if ($filter->getRange())
        {
            $qb->where('GEO(p.latitude = :latitude, p.longitude = :longitude) <= :range');
            $qb->setParameter('range', $filter->getRange());
        }

        if ($filter->getCategory())
        {
            $qb->join('p.category', 'c');
            $qb->andWhere('c.lft >= :lft');
            $qb->andWhere('c.rgt <= :rgt');
            $qb->setParameter('lft', $filter->getCategory()->getLft());
            $qb->setParameter('rgt', $filter->getCategory()->getRgt());
        }

        $qb->andWhere('p.quantity_available > 0');

        $q = $qb->getQuery();

        $products =  $q->execute();


        if ($orderCollection)
        {
            // Set quantity in cart
            $lineItemQuantities = $orderCollection->getLineItemQuantitiesIndexedByProductId();

            foreach ($products as $product)
            {
                $id = $product[0]->getId();

                if (array_key_exists($id, $lineItemQuantities))
                {
                    $product[0]->setQuantityInCart($lineItemQuantities[$id]);
                }
            }
        }

        return $products;
    }
public function findForSearchFilter(ProductFilter $filter, OrderCollection $orderCollection = null)
    {
        $qb = $this->getEntityManager()->createQueryBuilder()
            ->from('HarvestCloudCoreBundle:Product', 'p')
            ->select('p')
            ->addSelect('GEO(p.latitude = :latitude, p.longitude = :longitude) AS distance')
            ->setParameter('latitude', $filter->getLatitude())
            ->setParameter('longitude', $filter->getLongitude())
            ->orderBy('distance')
            ->setMaxResults(20)
        ;

        if ($filter->getRange())
        {
            $qb->where('GEO(p.latitude = :latitude, p.longitude = :longitude) <= :range');
            $qb->setParameter('range', $filter->getRange());
        }

        if ($filter->getCategory())
        {
            $qb->join('p.category', 'c');
            $qb->andWhere('c.lft >= :lft');
            $qb->andWhere('c.rgt <= :rgt');
            $qb->setParameter('lft', $filter->getCategory()->getLft());
            $qb->setParameter('rgt', $filter->getCategory()->getRgt());
        }

        $qb->andWhere('p.quantity_available > 0');

        $q = $qb->getQuery();

        $products =  $q->execute();


        if ($orderCollection)
        {
            // Set quantity in cart
            $lineItemQuantities = $orderCollection->getLineItemQuantitiesIndexedByProductId();

            foreach ($products as $product)
            {
                $id = $product[0]->getId();

                if (array_key_exists($id, $lineItemQuantities))
                {
                    $product[0]->setQuantityInCart($lineItemQuantities[$id]);
                }
            }
        }

        return $products;
    }
public function findForSearchFilter ( ProductFilter $ filter , OrderCollection $ orderCollection = null ) { $ qb = $ this -> getEntityManager ( ) -> createQueryBuilder ( ) -> from ( 'HarvestCloudCoreBundle:Product' , 'p' ) -> select ( 'p' ) -> addSelect ( 'GEO(p.latitude = :latitude, p.longitude = :longitude) AS distance' ) -> setParameter ( 'latitude' , $ filter -> getLatitude ( ) ) -> setParameter ( 'longitude' , $ filter -> getLongitude ( ) ) -> orderBy ( 'distance' ) -> setMaxResults ( 20 ) ; if ( $ filter -> getRange ( ) ) { $ qb -> where ( 'GEO(p.latitude = :latitude, p.longitude = :longitude) <= :range' ) ; $ qb -> setParameter ( 'range' , $ filter -> getRange ( ) ) ; } if ( $ filter -> getCategory ( ) ) { $ qb -> join ( 'p.category' , 'c' ) ; $ qb -> andWhere ( 'c.lft >= :lft' ) ; $ qb -> andWhere ( 'c.rgt <= :rgt' ) ; $ qb -> setParameter ( 'lft' , $ filter -> getCategory ( ) -> getLft ( ) ) ; $ qb -> setParameter ( 'rgt' , $ filter -> getCategory ( ) -> getRgt ( ) ) ; } $ qb -> andWhere ( 'p.quantity_available > 0' ) ; $ q = $ qb -> getQuery ( ) ; $ products = $ q -> execute ( ) ; if ( $ orderCollection ) { // Set quantity in cart $ lineItemQuantities = $ orderCollection -> getLineItemQuantitiesIndexedByProductId ( ) ; foreach ( $ products as $ product ) { $ id = $ product [ 0 ] -> getId ( ) ; if ( array_key_exists ( $ id , $ lineItemQuantities ) ) { $ product [ 0 ] -> setQuantityInCart ( $ lineItemQuantities [ $ id ] ) ; } } } return $ products ; }
Find Products for a given filter

@author Tom Haskins-Vaughan <tom@harvestcloud.com>
@since  2012-04-15
@todo   Create SearchFilter class

@param  ProductFilter    $filter
@param  OrderCollection  $orderCollection
Find Products for a given filter