访问symfony中的存储库

时间:2018-06-07 16:40:44

标签: symfony doctrine

我正在使用symfony并且在控制器内部尝试使用范围(rangeUpper,rangeLower)返回一组记录。

我正在传递请求对象中的参数。但是当在控制器中路由并尝试访问存储库类时,我感到很茫然。我的存储库看起来像;

    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();
}

我当前的控制器看起来像

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,
    ]);
}

在Repository中,我有一个查询设置来接受Range的查询。如何传递请求对象并从存储库中检索结果集?控制器中的FindAllByParams调用无效。

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

提前感谢您的时间和精力。

1 个答案:

答案 0 :(得分:2)

检查您的MaterialStock是否有存储库注释。

<?php

namespace UniflyteBundle\Entity;

use Doctrine\ORM\Mapping as ORM;

/**
 * MaterialStock
 *
 * @ORM\Table(name="material_stock")
 * @ORM\Entity(repositoryClass="UniflyteBundle\Repository\MaterialStockRepository")
 */
class MaterialStock{
    // [...]
}