class TestRepository1
{
/**
* @var ObjectRepository
*/
private $repository;
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->repository = $em->getRepository(Test::class);
$this->em = $em;
}
}
class TestRepository2
{
/**
* @var ObjectRepository
*/
private $repository;
/**
* @var EntityManagerInterface
*/
private $em;
public function __construct(EntityManagerInterface $em)
{
$this->repository = $em->getRepository(Test::class);
$this->em = $em;
}
}
class UserController
{
private $users;
public function __construct(UserRepository $users)
{
$this->users = $users;
}
public function all()
{
return new Response($this->users->findAll());
}
}
class UserController
{
public function all(UserRepository $users)
{
return new Response($users->findAll());
}
}
app.user_repository:
class: App\Repository\UserRepository
autowire: true
arguments:
- '@doctrine.orm.default_entity_manager'
app.post_repository:
class: App\Repository\PostRepository
autowire: true
arguments:
- '@knp\component\pager\paginatorinterface'
class SamplesRepository
{
/**
* @var ObjectRepository
*/
private $repository;
/**
* @var Connection
*/
private $connection;
/**
* @var EntityManagerInterface
*/
private $em;
/**
* @var PaginatorInterface
*/
private $paginator;
public function __construct(EntityManagerInterface $em, Connection $connection, PaginatorInterface $paginator)
{
$this->em = $em;
$this->repository = $em->getRepository(Sample::class);
$this->connection = $connection;
$this->paginator = $paginator;
}
public function all(Query $query, int $page, int $size, string $sort, string $direction)
{
$qb = $this->connection->createQueryBuilder()
->select(
's.id',
's.name',
's.date',
'u.id'
)
->from('samples', 's')
->innerJoin('s', 'user', 'u', 's.user_id = u.id');
if ($query->name) {
$qb->andWhere($qb->expr()->like('s.name', ':name'));
$qb->setParameter(':name', '%' . mb_strtolower($query->name) . '%');
}
$stmt = $qb->execute()->fetchAll();
$samples = [];
foreach ($stmt as $sample) {
$samples[] = $this->repository->findOneBy(['id' => $sample['id']]);
}
$qb->orderBy($sort, $direction === 'desc' ? 'desc' : 'asc');
return $this->paginator->paginate($samples, $page, $size);
}