2017-04-17 82 views
0

我有必要建立一个下拉字段分组数据:Symfony的形式与QueryBuilder的参数

我的形式:

class RetailerDetailFilterType extends AbstractType 
{ 
    public function getActiveRetailerMetrics(): array 
    { 
     return range(25,36); 
    } 

    public function buildForm(FormBuilderInterface $builder, array $options) 
    { 
     $builder->add('month', EntityType::class, 
      [ 
       'class' => 'AppBundle:ConsolidatedOperatorCategoryLowData', 
       'query_builder' => function(ConsolidatedOperatorCategoryLowDataRepository $er){ 
        return $er->getMinMaxByMetricQueryBuilder($this->getActiveRetailerMetrics()); 
       } 
      ]); 

    } 


    public function getBlockPrefix() 
    { 
     return 'key_metric'; 
    } 
} 

我的仓库:

class ConsolidatedOperatorCategoryLowDataRepository extends \Doctrine\ORM\EntityRepository 
{ 
    public function getMinMaxByMetricQueryBuilder($metricRange) 
    { 
     $qb = $this->getEntityManager() 
      ->createQueryBuilder('d'); 
     $qb 
      ->select('d.id, YEAR(d.date) as dyear, MONTH(d.date) as dmonth') 
      ->from('AppBundle:ConsolidatedOperatorCategoryLowData','d') 
      ->where($qb->expr()->in('d.metric_id', $metricRange)) 
      ->groupBy('dyear') 
      ->addGroupBy('dmonth') 
      ->setMaxResults(10) 
      ; 
     return $qb; 

    } 

我越来越

Warning: spl_object_hash() expects parameter 1 to be object, integer given 


at UnitOfWork ->isScheduledForInsert (3182005) 
in vendor/doctrine/orm/lib/Doctrine/ORM/EntityManager.php at line 710  + 
at EntityManager ->contains (3182005) 
in  vendor/symfony/symfony/src/Symfony/Bridge/Doctrine/Form/ChoiceList/IdReader.php at line 116 + 
at IdReader ->getIdValue (3182005) 
at call_user_func (array(object(IdReader), 'getIdValue'), 3182005) 
in  vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 205 + 
at ArrayChoiceList ->flatten (array('id' => 3182005, 'dyear' => '2016', 'dmonth' => '12'), array(object(IdReader), 'getIdValue'), array(), array(), null) 
in  vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 200 + 
at ArrayChoiceList ->flatten (array(array('id' => 3182005, 'dyear' => '2016', 'dmonth' => '12'), array('id' => 3186685, 'dyear' => '2017', 'dmonth' => '1'), array('id' => 3191365, 'dyear' => '2017', 'dmonth' => '2'), array('id' => 3195595, 'dyear' => '2017', 'dmonth' => '3'), array('id' => 3200275, 'dyear' => '2017', 'dmonth' => '4')), array(object(IdReader), 'getIdValue'), array(), array(), array(null)) 
in vendor/symfony/symfony/src/Symfony/Component/Form/ChoiceList/ArrayChoiceList.php at line 91 
+0

您的querybuilder返回的不仅仅是一个实体(dyear,dmonth),我不认为FormType可以处理它。尝试用这个替换你的select-call:'select('d,YEAR(d.date)AS HIDDEN dyear,MONTH(d.date)HIDDEN dmonth')'。这当然假设你实际上并不需要除了groupBy以外的其他任何地方的dmonth和dyear值。 – ccKep

回答

0

我认为它有t o关于Doctrine ORM如何处理关系。当你想通过被引用的对象的id来检索一个实体时,比如说通过用户id来查找所有的帖子,那么你仍然必须将用户对象传递给QueryBuilder,而不仅仅是id。这是因为教义会解决这些实体之间的联系。

看来,metric_id实际上是对某种Metric-entity的引用,只是传递一个int数组而不是实际的对象似乎会让Doctrine的QueryBuilder发生变化。

您可以尝试将id映射到Metric的新实例,然后改为传递该数组。

另一种解决方案 - 我更喜欢 - 就是使用Native SQL