drzfz9995 2019-02-14 07:46
浏览 36

如何将Query分隔到另一个类/文件?

I created a seperate class for one of my DataTables (lets say MyTableType). Inside ORMAdapter I've created a custom query, which gets quite long and complicated. This is why I'd like to seperate it from the code that's responsible for creating a table itself and move it into some other class (like MyTableQuery).

I am using Symfony 4.2 and omines/datatables-bundle bundle.

What should that class for queries return? And what's the best way then to get the returned query in MyTableType?

class MyTableType implements DataTableTypeInterface
{
    public function configure(DataTable $dataTable, array $options)
    {
        $request = $options['request'];

        $dataTable
            ->add('id',
                NumberColumn::class,
                array(
                    'label' => 'ID',
                    'globalSearchable' => false
                )
            )

Some more columns

->createAdapter(ORMAdapter::class,
                array(
                    'entity' => MyEntity::class,
                    'query' => function (QueryBuilder $builder) use ($request)
                    { 
                        //Long query I'd prefer to get from somewhere else
                    }
                )
            )
        ;
    }
}

Inside my query option I'd like to get the return from that custom class MyTypeQuery. It'd make MyTableType class much cleaner. What's the best way to accomplish that?

  • 写回答

1条回答 默认 最新

  • dongtan9253 2019-02-14 12:43
    关注

    Ok, I don't know if this is the best solution, but what I've done is:

    * Pass Doctrine Manager to Table Building class
    
    * Inside my query option pass $builder into repository and build a query on top passed variable.
      And it works.
    
    class MyTableType implements DataTableTypeInterface
    {
        public function configure(DataTable $dataTable, array $options)
        {
            $request = $options['request'];
            $em = $options['entityManager'];
    
            $dataTable
                ->add('id',
                    NumberColumn::class,
                    array(
                        'label' => 'ID',
                        'globalSearchable' => false
                    )
                )
    
    ... Some more columns ...
    
    ->createAdapter(ORMAdapter::class,
                    array(
                        'entity' => MyEntity::class,
                        'query' => function (QueryBuilder $builder) use ($request, $em)
                        { $em->getRepository(MyClass::class)->find($request, $builder) }
                    )
                )
            ;
        }
    }
    

    Inside my repository I return passed QueryBuilder variable and that's all.

    评论

报告相同问题?

悬赏问题

  • ¥20 怎么用dlib库的算法识别小麦病虫害
  • ¥15 华为ensp模拟器中S5700交换机在配置过程中老是反复重启
  • ¥15 java写代码遇到问题,求帮助
  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算