Like this my table is filled data from an Array:
public function showAction(Request $request)
{
$table = $this->createDataTable()
->add('firstName', TextColumn::class)
->add('lastName', TextColumn::class)
->createAdapter(ArrayAdapter::class, [
['firstName' => 'Cat', 'lastName' => 'Duck'],
['firstName' => 'Monkey', 'lastName' => 'Dog'],
])
->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('list.html.twig', ['datatable' => $table]);
}
But what I need is to get the data directly from the mySQL database. I tried this with doctrine:
public function showAction(Request $request)
{
$articles = $this->getDoctrine()->getRepository(Article::class)->findAll()->handleRequest($request);
if ($articles ->isCallback()) {
return $articles ->getResponse();
}
return $this->render('list.html.twig', ['datatable' => $articles]);
}
But I get an ERROR:
Uncaught PHP Exception Symfony\\Component\\Debug\\Exception\\FatalThrowableError: "Call to a member function handleRequest() on array" at /Users/work/project/src/Controller/DataTableController.php line 27
I also tried to write it like this:
public function showAction(Request $request)
{
$articles = $this->getDoctrine()->getRepository(Article::class)->findAll();
return $this->render('list.html.twig', ['datatable' => $articles]);
}
But here I get the error:
Argument 1 passed to Omines\\DataTablesBundle\\Twig\\DataTablesExtension::Omines\\DataTablesBundle\\Twig{closure}() must be an instance of Omines\\DataTablesBundle\\DataTable, array given, called in /Users/work/project/var/cache/dev/twig/0b/0bf4881c934fbecf72f2dfcacd298733196c8daa0e22d77f67fcdf0fee9f33e4.php on line 185
According to documentation ( https://omines.github.io/datatables-bundle/#doctrine-orm )
In your first exemple, you need to do this :
public function showAction(Request $request)
{
$table = $this->createDataTable()
->add('firstName', TextColumn::class)
->add('lastName', TextColumn::class)
->createAdapter(ORMAdapter::class, [
'entity' => Article::class,
])
->handleRequest($request);
if ($table->isCallback()) {
return $table->getResponse();
}
return $this->render('list.html.twig', ['datatable' => $table]);
}
Don't forget to add this on top of your class :
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.