@@ -20,6 +20,7 @@ Highly inspired by [SgDatatablesBundle](https://github.com/stwe/DatatablesBundle
20205 . [ Configuration] ( #configuration )
2121 1 . [ Table Dataset Options] ( #table-dataset-options )
2222 2 . [ Table Options] ( #table-options )
23+ 6 . [ Custom Doctrine Queries] ( #custom-doctrine-queries )
2324
2425## Features
2526
@@ -253,6 +254,10 @@ Represents column with text. With formatter you can create complex columns.
253254#### Example
254255
255256``` php
257+ //use statements for search and sort option
258+ use Doctrine\ORM\Query\Expr\Orx;
259+ use Doctrine\ORM\QueryBuilder;
260+
256261->add('username', TextColumn::class, array(
257262 'title' => 'Username',
258263 'emptyData' => "No Username found.",
@@ -265,13 +270,29 @@ Represents column with text. With formatter you can create complex columns.
265270 'sort' => function (QueryBuilder $qb, $direction) { //execute if user sort this column
266271 $qb->addOrderBy('username', $direction);
267272 },
268- 'search' => function (QueryBuilder $qb, $search) { //execute if user use global search
269- $qb->orWhere('user.username LIKE :username')
270- ->setParameter('username', $search . '%');
273+ 'search' => function (Orx $orx, QueryBuilder $qb, $dql, $search, $key) {
274+ //first add condition to $orx
275+ //don't forget the '?' before $key
276+ $orx->add($qb->expr()->like($dql, '?' . $key));
277+
278+ //then bind search to query
279+ $qb->setParameter($key, '%' . $search . '%');
271280 }
272281))
273282```
274283
284+ ** search** Option:
285+
286+ The search option seems a bit complicated at first, but it allows full control over the query in the column.
287+
288+ | Paramenter name | Description |
289+ | ------------------ | ------------------------------------------------------------ |
290+ | ` Orx $orx ` | All columns are connected to the SQL query or. With ` $orx ` more parts can be added to the query. |
291+ | ` QueryBuilder $qb ` | ` $qb ` holds the use QueryBuilder. It is the same instance as can be queried with ` getQueryBuilder() ` in the table class. |
292+ | ` (string) $dql ` | ` $dql ` represents the "path" to the variable in the query (e.g. ` user.username ` or in case of a JOIN ` costCentre.name ` ) |
293+ | ` $search ` | The search in the type of a string. |
294+ | ` $key ` | The index of the columns already gone through. The index is used for parameter binding to the query. |
295+
275296
276297
277298### BooleanColumn
@@ -576,6 +597,39 @@ hello_bootstrap_table:
576597
577598
578599
600+ # # Custom Doctrine Queries
601+
602+ Sometimes you don't want to display all the data in a database table. For this you can "prefilter" the Doctrine query.
603+
604+ # ## Example
605+
606+ ` ` ` php
607+ /**
608+ * @Route("/", name="default")
609+ */
610+ public function index(Request $request, HelloBootstrapTableFactory $tableFactory)
611+ {
612+ //first create a instance of your table
613+ $table = $tableFactory->create(TestTable::class);
614+
615+ //then you can access the QueryBuilder from the table
616+ $table->getQueryBuilder()
617+ ->andWhere('department.name = :departmentName')
618+ ->setParameter('departmentName', 'IT');
619+
620+ $table->handleRequest($request);
621+ if ($table->isCallback()) {
622+ return $table->getResponse();
623+ }
624+
625+ return $this->render("index.html.twig", array(
626+ "table" => $table->createView()
627+ ));
628+ }
629+ ` ` `
630+
631+
632+
579633# # ToDo's
580634* Documentation
581635* Cookie Extension
0 commit comments