omines / datatables-bundle

DataTables bundle for Symfony
https://omines.github.io/datatables-bundle/
MIT License
265 stars 113 forks source link

Undefined method 'createDataTableFromType'.intelephense(P1013) #325

Closed csms-pf closed 10 months ago

csms-pf commented 10 months ago

Hi,

Using Symfony 6.3 Installed omines/datatables-bundle using this documentation: https://omines.github.io/datatables-bundle/#introduction

Created UnitTableType

<?php

namespace App\DataTable\Type;

use App\Entity\Unit;
use Omines\DataTablesBundle\Adapter\Doctrine\ORMAdapter;
use Omines\DataTablesBundle\Column\TextColumn;
use Omines\DataTablesBundle\DataTable;
use Omines\DataTablesBundle\DataTableTypeInterface;

class UnitTableType implements DataTableTypeInterface
{
    /**
     * @param DataTable $dataTable
     * @param array $options
     */
        public function configure(DataTable $dataTable, array $options): void    {
        $dataTable
            ->add('name', TextColumn::class, ['field' => 'unit.name'])
            ->add('alias', TextColumn::class, ['field' => 'unit.alias'])
            ->add('imoNumber', TextColumn::class, ['field' => 'unit.imoNumber'])
            ->add('category', TextColumn::class, ['field' => 'unit.category'])
            ->createAdapter(ORMAdapter::class, [
                'entity' => Unit::class,
            ]);
    }
}

New controller

<?php

namespace App\Controller;

use App\Entity\Unit;
use Sonata\Exporter\Handler;
use App\Repository\UnitRepository;
use Sonata\Exporter\Writer\CsvWriter;
use App\DataTable\Type\UnitTableType;
use Sonata\Exporter\Handler\ExportHandler;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\Response;
use Sonata\Exporter\Source\ArraySourceIterator;
use Symfony\Component\Routing\Annotation\Route;
use Symfony\Component\HttpFoundation\StreamedResponse;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;

class UnitController extends AbstractController
{
    #[Route('/units', name: 'app_unit')]
    public function index(Request $request): Response
    {
        $table = $this->createDataTableFromType(UnitTableType::class)
        ->handleRequest($request);

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

        return $this->render('unit/unit.list.html.twig', ['datatable' => $table]);
    }
}
Call to undefined method App\Controller\UnitController::createDataTableFromType()
Error
in src/Controller/UnitController.php (line 23)
class UnitController extends AbstractController{    #[Route('/units', name: 'app_unit')]    public function index(Request $request): Response    {        $table = $this->createDataTableFromType(UnitTableType::class)        ->handleRequest($request);        if ($table->isCallback()) {            return $table->getResponse();
in vendor/symfony/http-kernel/HttpKernel.php -> index (line 181)
        $this->dispatcher->dispatch($event, KernelEvents::CONTROLLER_ARGUMENTS);        $controller = $event->getController();        $arguments = $event->getArguments();        // call controller        $response = $controller(...$arguments);        // view        if (!$response instanceof Response) {            $event = new ViewEvent($this, $request, $type, $response, $event);            $this->dispatcher->dispatch($event, KernelEvents::VIEW);
in vendor/symfony/http-kernel/HttpKernel.php -> handleRaw (line 76)
        $request->headers->set('X-Php-Ob-Level', (string) ob_get_level());        $this->requestStack->push($request);        $response = null;        try {            return $response = $this->handleRaw($request, $type);        } catch (\Throwable $e) {            if ($e instanceof \Error && !$this->handleAllThrowables) {                throw $e;            }
in vendor/symfony/http-kernel/Kernel.php -> handle (line 197)
        $this->boot();        ++$this->requestStackSize;        $this->resetServices = true;        try {            return $this->getHttpKernel()->handle($request, $type, $catch);        } finally {            --$this->requestStackSize;        }    }
in vendor/symfony/runtime/Runner/Symfony/HttpKernelRunner.php -> handle (line 35)
        $this->request = $request;    }    public function run(): int    {        $response = $this->kernel->handle($this->request);        $response->send();        if ($this->kernel instanceof TerminableInterface) {            $this->kernel->terminate($this->request, $response);        }
in vendor/autoload_runtime.php -> run (line 29)
$app = $app(...$args);exit(    $runtime        ->getRunner($app)        ->run());
require_once('/home/mf/symfony/csms/vendor/autoload_runtime.php')
in public/index.php (line 5)

Seems the createDataTableFromType is missing.

<?php

return [
    Symfony\Bundle\FrameworkBundle\FrameworkBundle::class => ['all' => true],
    Doctrine\Bundle\DoctrineBundle\DoctrineBundle::class => ['all' => true],
    Doctrine\Bundle\MigrationsBundle\DoctrineMigrationsBundle::class => ['all' => true],
    Symfony\Bundle\SecurityBundle\SecurityBundle::class => ['all' => true],
    Symfony\Bundle\TwigBundle\TwigBundle::class => ['all' => true],
    Twig\Extra\TwigExtraBundle\TwigExtraBundle::class => ['all' => true],
    Vich\UploaderBundle\VichUploaderBundle::class => ['all' => true],
    Symfony\Bundle\MakerBundle\MakerBundle::class => ['dev' => true],
    Sylius\Bundle\GridBundle\SyliusGridBundle::class => ['all' => true],
    Nelmio\CorsBundle\NelmioCorsBundle::class => ['all' => true],
    Sonata\Exporter\Bridge\Symfony\SonataExporterBundle::class => ['all' => true],
    Symfony\Bundle\WebProfilerBundle\WebProfilerBundle::class => ['dev' => true, 'test' => true],
    Omines\DataTablesBundle\DataTablesBundle::class => ['all' => true],
];

May someone help me?

curry684 commented 10 months ago

Sorry the DatatablesTrait was deprecated, but there's an issue with the documentation site not rebuilding.

Inject the DataTableFactory in your controller and use the same method on there instead.

csms-pf commented 10 months ago

Thanks for your reply