Symfony和Omines DataTable组件

时间:2018-12-07 19:35:42

标签: symfony datatable

我有以下问题:

->createAdapter(ORMAdapter::class, [
    'entity' => Systems::class,
    'query' => function (QueryBuilder $builder) {
    $builder
    ->select('systems')
    ->from(Systems::class, 'systems')
    ->orderBy('systems.level', 'ASC')
    ->addorderBy('systems.label', 'ASC')
    ->andwhere('systems.territory = :SearchTerritory')
    ->setParameter('SearchTerritory', 'Independent');
    },

这样的代码是有效的,但是当我以变量$ territory为例替换“ Independent”时,它不起作用,并且消息是未声明该变量。在创建数据表之前转储该变量会向我显示该变量已正确设置,因此以某种方式无法将全局控制器变量发送到该数据表,并且我不知道该怎么做。

这是完整的代码:

public function territoryList($territory, Environment $twig, Request $request)
{

    $table = $this->createDataTable()
    ->add('label', TextColumn::class, ['label' => 'Name'])
    ->add('level', TextColumn::class, ['label' => 'Level'])
    ->add('coordinates', TextColumn::class, ['label' => 'Coordinates'])
    ->add('territory', TextColumn::class, ['label' => 'Territory'])
    ->add('missions', BoolColumn::class, ['label' => 'M', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('parsteel', BoolColumn::class, ['label' => 'PS', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('tritanium', BoolColumn::class, ['label' => 'TT', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('dilithium', BoolColumn::class, ['label' => 'DL', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('rawgas', BoolColumn::class, ['label' => 'RG', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('rawcrystal', BoolColumn::class, ['label' => 'RC', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])
    ->add('rawore', BoolColumn::class, ['label' => 'RO', 'searchable' => false, 'trueValue' => '<i class="fas fa-check"></i>', 'falseValue' => '<i class="fas fa-times"></i>'])

    ->createAdapter(ORMAdapter::class, [
        'entity' => Systems::class,
        'query' => function (QueryBuilder $builder) {
        $builder
        ->select('systems')
        ->from(Systems::class, 'systems')
        ->orderBy('systems.level', 'ASC')
        ->addorderBy('systems.label', 'ASC')
        ->andwhere('systems.territory = :SearchTerritory')
        ->setParameter('SearchTerritory', 'Independent');
        },


        ])
        ->handleRequest($request);

        if ($table->isCallback()) {
            return $table->getResponse();
        }

没有树枝渲染等

1 个答案:

答案 0 :(得分:0)

在PHP中,匿名函数(例如,作为query参数提供的回调)是私有作用域的,并且不能隐式访问外部变量。有关参考,请参见PHP文档中的the section about inheriting variables from the parent scope

要导入$territories变量,您将必须use,即

'query' => function (QueryBuilder $builder) use ($territories) { ...