public function findSameCategoryProduct($product, $direction)
    {
        $qb = $this->getQueryBuilder()
            ->select('p')
            ->andWhere('p.category = :category')
            ->setMaxResults(1)
            ->setParameter('category', $product->getCategory());

        if ('next' == $direction) {
            $qb->andWhere('p.id > :id')
                ->orderBy('p.id', 'asc');
        } else {
            $qb->andWhere('p.id < :id')
                ->orderBy('p.id', 'desc');
        }

        $qb->setParameter('id', $product->getId());

        if (0 == count($qb->getQuery()->getResult())) {
            return null;
        }

        return $qb->getQuery()
            ->getSingleResult();
    }
public function findSameCategoryProduct($product, $direction)
    {
        $qb = $this->getQueryBuilder()
            ->select('p')
            ->andWhere('p.category = :category')
            ->setMaxResults(1)
            ->setParameter('category', $product->getCategory());

        if ('next' == $direction) {
            $qb->andWhere('p.id > :id')
                ->orderBy('p.id', 'asc');
        } else {
            $qb->andWhere('p.id < :id')
                ->orderBy('p.id', 'desc');
        }

        $qb->setParameter('id', $product->getId());

        if (0 == count($qb->getQuery()->getResult())) {
            return null;
        }

        return $qb->getQuery()
            ->getSingleResult();
    }
public function findSameCategoryProduct ( $ product , $ direction ) { $ qb = $ this -> getQueryBuilder ( ) -> select ( 'p' ) -> andWhere ( 'p.category = :category' ) -> setMaxResults ( 1 ) -> setParameter ( 'category' , $ product -> getCategory ( ) ) ; if ( 'next' == $ direction ) { $ qb -> andWhere ( 'p.id > :id' ) -> orderBy ( 'p.id' , 'asc' ) ; } else { $ qb -> andWhere ( 'p.id < :id' ) -> orderBy ( 'p.id' , 'desc' ) ; } $ qb -> setParameter ( 'id' , $ product -> getId ( ) ) ; if ( 0 == count ( $ qb -> getQuery ( ) -> getResult ( ) ) ) { return null ; } return $ qb -> getQuery ( ) -> getSingleResult ( ) ; }
Find a product from the same category

@param Product $product
@param string  $direction

@return Product|null
Find a product from the same category