Skip to content
Advertisement

Attempted to call an undefined method named “getEntityManager” of class

I’m learning symfony and I would like to have a search bar to show user with email. But I got and error

Attempted to call an undefined method named "getEntityManager" of class "AppRepositorySearchRepository".

If someone can help me or explain me how to do it’s would be very nice. Thanks

In SearchRepository

class SearchRepository
{

    public function findAllWithSearch($email){
        $entityManager = $this->getEntityManager();

        $query = $entityManager->createQuery(
            'SELECT u
            FROM AppEntityUser u
            WHERE u.email :email'
        )->setParameter('email', $email);
        return $query->execute();
    }
}

In SearchController

class SearchController extends AbstractController
{
    /**
     * @Route("/admin/search/", name="admin_search")
     * @Security("is_granted('ROLE_ADMIN')")
     */
    public function searchUser(SearchRepository $repository, Request $request)
    {
        $q = $request->query->get('search');
        $comments = $repository->findllWithSearch($q);
        return $this->render('admin/search/search.html.twig',
        [            'user' => $repository,
        ]);
    }
}

and search.twig.html

    <form action="" method="get">
        <input type="search" name="search" value="" placeholder="Recherche.." />
        <input type="submit" value="Valider" />
    </form>

Advertisement

Answer

From the doctrine documentation:

A repository object provides many ways to retrieve entities of the specified type. By default, the repository instance is of type DoctrineORMEntityRepository. You can also use custom repository classes.

So, if your search is going to deal with User objects, you can use the standard UserRepository by doing the following in your Controller:

/**
 * @Route("/admin/search/", name="admin_search")
 * @Security("is_granted('ROLE_ADMIN')")
 */
public function searchUser(Request $request)
{
    $q = $request->query->get('search');
    // Get standard repository
    $user = $this->getDoctrine()->getManager()
        ->getRepository(User::class)
        ->findBy(['email' => $q]); // Or use the magic method ->findByEmail($q);

    // Present your results
    return $this->render('admin/search/search_results.html.twig', 
        ['user' => $user]);
}

There is no need for a custom repository for your use case, but if you want to create one and use it for autowiring you must extend ServiceEntityRepository, a container-friendly base repository class provided by Symfony. You can get more details in the documentation. In this case you might want to also review how to annotate your entity to tell the EntityManager that you’ll be using a custom repository.

Sidenote: By default, the action attribute of the form defaults to the same route you are visiting, so if that fragment is part of a layout you’ll have to set it explicitly to your SearchController action: action="{{ path('admin_search') }}"

User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement