查询访问存储库而不返回数据

时间:2018-01-03 16:03:13

标签: symfony doctrine repository

Symfony / Doctrine - 存储库不返回数据。

我之前已成功从其他实体/存储库中检索数据,但似乎无法从此特定存储库获取数据。我已经在控制台中清除了缓存,并且已经使用实体类(带注释)注册了存储库。

测试DQL,因为SQL按预期返回结果,这意味着传递给查询的参数将通过SQL查询返回我想要的结果,而不是DQL查询。

与过去成功的存储库查询相比,访问此存储库的路由之间的一个区别是"参数包"正在传递请求"属性"在此存储库和成功的先前存储库查询中,查询参数通过请求"查询 - 参数包"在请求中。我不确定它是否只是存储库查询问题的一部分。

控制器

/**
 * Open the modal dialog for selecting strip width
 * @Route("/select/{materialId}/{gaugeId}/{rangeUpper}/{rangeLower}", defaults={"materialId" = 0, "gaugeId" = 0, "rangeUpper" = 0, "rangeLower" = 0}, name="material-stock_selectWidth")
 * @Method("GET")
 */
public function selectStripWidthAction (Request $request)
{
    $em = $this->getDoctrine()->getManager();

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



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

存储库

public function findAllByParams (Request $request)
{

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

    $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'])
            ;
        }
    }
    return $query->orderBy('ms.widthDecimal', 'DESC');
}

显示结果

<select>
    {% for MaterialStock in MaterialStock %}
        <option>{{ MaterialStock.widthDecimal }}</option>
    {% endfor %}
</select>

来自控制器的dump()显示queryBuilder。

MaterialStockController.php on line 139:
array:1 [▼
"materialStocks" => QueryBuilder {#613 ▼
-_em: EntityManager {#757 …11}
-_dqlParts: array:9 [▼
  "distinct" => false
  "select" => array:1 [▶]
  "from" => array:1 [▶]
  "join" => []
  "set" => []
  "where" => Andx {#642 ▼
    #separator: " AND "
    #allowedClasses: array:4 [▶]
    #preSeparator: "("
    #postSeparator: ")"
    #parts: array:4 [▼
      0 => "ms.material = :materialId"
      1 => "ms.gauge = :gaugeId"
      2 => "ms.widthDecimal <= :upperIdentifier"
      3 => "ms.widthDecimal >= :lowerIdentifier"
    ]
  }
  "groupBy" => []
  "having" => null
  "orderBy" => array:1 [▶]
]
-_type: 0
-_state: 0
-_dql: null
-parameters: ArrayCollection {#646 ▼
  -elements: array:4 [▼
    0 => Parameter {#645 ▼
      -name: "materialId"
      -value: "1"
      -type: 2
    }
    1 => Parameter {#644 ▼
      -name: "gaugeId"
      -value: "7"
      -type: 2
    }
    2 => Parameter {#648 ▼
      -name: "upperIdentifier"
      -value: "3.75"
      -type: 2
    }
    3 => Parameter {#643 ▼
      -name: "lowerIdentifier"
      -value: "3.5"
      -type: 2
    }
  ]
}
-_firstResult: null
-_maxResults: null
-joinRootAliases: []
#cacheable: false
#cacheRegion: null
#cacheMode: null
#lifetime: 0

} ]

在实体类中注册存储库

@ORM\Entity(repositoryClass="UniflyteBundle\Repository\MaterialStockRepository")

我在这里做错了什么?

我很感激投入这一挑战的任何时间/精力/想法/建议。先感谢您。

2 个答案:

答案 0 :(得分:2)

我想你错过了这个:->getQuery()->getResult()(这会返回querybuilder的数据)返回findAllByParams函数(在查询构建器上)。希望它有所帮助!

答案 1 :(得分:1)

您有查询,但没有得到结果。
findAllByParams方法看起来应该是这样的:

public function findAllByParams ($arguments)
{

  //quuery building goes here
  // ...
  $query->orderBy('ms.widthDecimal', 'DESC');
  return $query->getQuery()->getResult()
}

不要将Request传递给您的存储库方法,在外部获取参数(并验证它们),例如,在控制器中。