lexik / LexikFormFilterBundle

This Symfony bundle aim to provide classes to build some form filters and then build a doctrine query from this form filter.
MIT License
389 stars 119 forks source link

Hi #342

Closed jorgeale90 closed 3 years ago

jorgeale90 commented 3 years ago

Service "doctrine.orm.entity_manager" not found: even though it exists in the app's container, the container inside "App\Controller\DashboardController" is a smaller service locator that only knows about the "doctrine", "form.factory", "http_kernel", "parameter_bag", "request_stack", "router", "security.authorization_checker", "security.csrf.token_manager", "security.token_storage", "serializer", "session" and "twig" services. Try using dependency injection instead.

class DashboardController extends AbstractController { /**

gilles-g commented 3 years ago

Can I closed that issue?

I really dont understand, plz Ask a question or I will close the issue.

jorgeale90 commented 3 years ago

First of all, forgive me, my English is not very good. My question is how can I solve the error shown above.

jorgeale90 commented 3 years ago

The solution I made I don't know if it's all right

/**
 * @var EntityManagerInterface
 */
private $em;

public function __construct(EntityManagerInterface $em)
{
    $this->em = $em;
}

/**
 * @Route("/posts/", name="admin_posts", methods={"GET"})
 * @IsGranted("IS_AUTHENTICATED_FULLY")
 */
public function posts(Request $request, PaginatorInterface $paginator, FilterBuilderUpdaterInterface $query_builder_updater)
{
    $filterBuilder = $this->em->getRepository(Post::class)
        ->createQueryBuilder('c')
        ->orderBy('c.date_created', 'DESC');

    $form = $this->get('form.factory')->create(PostFilterType::class);

    if ($request->query->has($form->getName())) {
        $form->submit($request->query->get($form->getName()));
        $query_builder_updater->addFilterConditions($form, $filterBuilder);
    }

    $query = $filterBuilder->getQuery();

    $pagination = $paginator->paginate(
        $query,
        $request->query->get('page', 1)/*page number*/,
        10/*limit per page*/
    );

    return $this->render('dashboard/posts.html.twig', array(
        'form' => $form->createView(),
        'pagination' => $pagination
    ));
}