Метод getRepository
Метод getRepository класса EntityManager возвращает объект репозитория для указанного класса сущности. Первым параметром передаётся имя класса сущности (FQCN). Репозиторий предоставляет готовые методы для выборки записей: find, findAll, findBy, findOneBy и другие.
Синтаксис
$repository = $em->getRepository(EntityClass::class);
Пример
Давайте получим репозиторий сущности Article и найдём запись по её id:
<?php
namespace App\Controller;
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ArticleController extends AbstractController
{
public function show(EntityManagerInterface $em): Response
{
$repo = $em->getRepository(Article::class);
$article = $repo->find(1);
return new Response($article->getTitle());
}
}
?>
Результат выполнения кода:
"article"
Пример
Давайте получим все записи сущности Article с помощью метода findAll:
<?php
namespace App\Controller;
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ArticleController extends AbstractController
{
public function index(EntityManagerInterface $em): Response
{
$repo = $em->getRepository(Article::class);
$articles = $repo->findAll();
$res = [];
foreach ($articles as $article) {
$res[] = $article->getTitle();
}
return new Response(implode(', ', $res));
}
}
?>
Результат выполнения кода:
"article 1, article 2, article 3"
Пример
Давайте найдём записи сущности Article по условию с помощью метода findBy:
<?php
namespace App\Controller;
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ArticleController extends AbstractController
{
public function index(EntityManagerInterface $em): Response
{
$repo = $em->getRepository(Article::class);
$articles = $repo->findBy(['published' => true]);
$res = [];
foreach ($articles as $article) {
$res[] = $article->getTitle();
}
return new Response(implode(', ', $res));
}
}
?>
Результат выполнения кода:
"article 1, article 2"
Пример
Давайте найдём одну запись сущности Article по условию с помощью метода findOneBy:
<?php
namespace App\Controller;
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Bundle\FrameworkBundle\Controller\AbstractController;
use Symfony\Component\HttpFoundation\Response;
class ArticleController extends AbstractController
{
public function show(EntityManagerInterface $em): Response
{
$repo = $em->getRepository(Article::class);
$article = $repo->findOneBy(['title' => 'article']);
return new Response($article->getTitle());
}
}
?>
Результат выполнения кода:
"article"
Смотрите также
-
класс
EntityManager,
который управляет сущностями Doctrine -
метод
find,
который находит запись по её идентификатору -
метод
persist,
который ставит сущность в очередь на сохранение -
метод
createQueryBuilder,
который создаёт построитель DQL-запросов