访问 symfony 中的存储库

问题描述 投票:0回答:1

我正在使用 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,
    ]);
}

在存储库中,我有查询设置来按范围接受和查询。如何传递请求对象并从存储库检索结果集?控制器中的 FindAllByParams 调用不起作用。

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

预先感谢您的时间和精力。

php symfony doctrine
1个回答
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{
    // [...]
}
© www.soinside.com 2019 - 2024. All rights reserved.