Search code examples
phpsymfonydoctrine

accessing repository in symfony


I am using symfony and am inside the controller trying to return a set of records by using a range (rangeUpper, rangeLower).

I am passing the parameters in the request object fine. But when going to route in the controller and trying to access the repository class I am at a loss. My repository looks like;

    public function findAllByParams (Request $request)
{

    $criteria = $request->query->get('uniflytebundle_material-stock_select');

    $criteria = ($request->get('materialId') == 0 ? [] : ['materialId' => $request->get('materialId')]);
    $criteria = array_merge(($request->get('gaugeId') == 0 ? [] : ['gaugeId' => $request->get('gaugeId')]), $criteria);
    $criteria = array_merge(($request->get('rangeUpper') == 0 ? [] : ['rangeUpper' => $request->get('rangeUpper')]), $criteria);
    $criteria = array_merge(($request->get('rangeLower') == 0 ? [] : ['rangeLower' => $request->get('rangeLower')]), $criteria);
    $criteria = array_filter($criteria);

    $query = $this->createQueryBuilder('ms');

    if (!empty($criteria)) {
        if (!empty($criteria['materialId']) && !empty($criteria['gaugeId']) && !empty($criteria['rangeUpper']) && !empty($criteria['rangeLower'])) {
            $query
              ->where('ms.material = :materialId')
              ->andWhere('ms.gauge = :gaugeId')
              ->andWhere('ms.widthDecimal <= :upperIdentifier')
              ->andWhere('ms.widthDecimal >= :lowerIdentifier')
              ->setParameter('materialId', $criteria['materialId'])
              ->setParameter('gaugeId', $criteria['gaugeId'])
              ->setParameter('upperIdentifier', $criteria['rangeUpper'])
              ->setParameter('lowerIdentifier', $criteria['rangeLower'])
            ;
        }
    }
    $query->orderBy('ms.widthDecimal', 'DESC');

    return $query->getQuery()->getResult();
}

My current controller looks like

public function selectStripWidthAction (Request $request)
{
    $em = $this->getDoctrine()->getManager();

    $materialId = $request->get('materialId');
    $gaugeId = $request->get('gaugeId');

    $materialStocks = $em->getRepository('UniflyteBundle:MaterialStock')->findAllByParams($request);

    return $this->render('materialstock/dialog/select.html.twig', [
        'MaterialStocks' => $materialStocks,
    ]);
}

In the Repository I have the query setup to accept and query by the Range. How do I pass the request object and retrieve the result set from the Repository? FindAllByParams call in the controller is not working.

Undefined method 'findAllByParams'. The method name must start with either findBy or findOneBy!

Thanks in advance for your time and effort.


Solution

  • Check that your MaterialStock have annotation for repository.

    <?php
    
    namespace UniflyteBundle\Entity;
    
    use Doctrine\ORM\Mapping as ORM;
    
    /**
     * MaterialStock
     *
     * @ORM\Table(name="material_stock")
     * @ORM\Entity(repositoryClass="UniflyteBundle\Repository\MaterialStockRepository")
     */
    class MaterialStock{
        // [...]
    }