11/02/2018 - DOCTRINE, SYMFONY
Symfony ve Doctrine kullanalı bir kaç yıl oldu ve bu güne kadar Doctrine ile birlikte gelen find
, findAll
, findBy
ve findOneBy
methodları kullandığım günü hatırlamıyorum. Bunun yerine her zaman kendi repository classımı yaratıp EntityRepository
classı enjekte ettim. Bu örneğimizde composition yerine inheritance kullanacağız ve Dependency Inversion prensibini adapte edeceğiz.
Bunu kullanmayacağız çünkü istemediğiniz her şey birlikte geliyor. Sadece nasıl yapıldığını gösteriyorum.
namespace CustomerBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="customer")
* @ORM\Entity(repositoryClass="CustomerBundle\Repository\CustomerRepository")
*/
class Customer
{
...
}
namespace CustomerBundle\Repository;
use Doctrine\ORM\EntityRepository;
class CustomerRepository extends EntityRepository
{
public function findAll()
{
return $this->createQueryBuilder('c')
->getQuery()
->getArrayResult();
}
public function findOneById($id)
{
return $this->createQueryBuilder('c')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}
}
services:
customer.repository.customer:
class: CustomerBundle\Repository\CustomerRepository
factory: [ "@doctrine.orm.entity_manager", getRepository ]
arguments:
- CustomerBundle\Entity\Customer
namespace CustomerBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="customer")
*/
class Customer
{
...
}
Bunu V2 yerine tercih ediniz.
namespace CustomerBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
use Doctrine\ORM\EntityRepository;
abstract class AbstractRepository
{
private $entityManager;
private $entityName;
public function __construct(EntityManagerInterface $entityManager, string $entityName)
{
$this->entityManager = $entityManager;
$this->entityName = $entityName;
}
protected function getEntityManager(): EntityManagerInterface
{
return $this->entityManager;
}
protected function getRepository(): EntityRepository
{
return $this->entityManager->getRepository($this->entityName);
}
}
namespace CustomerBundle\Repository;
use Doctrine\ORM\EntityManagerInterface;
class CustomerRepository extends AbstractRepository
{
public function __construct(EntityManagerInterface $entityManager)
{
parent::__construct($entityManager, Customer::class);
}
public function findAll()
{
return $this->getRepository()->createQueryBuilder('c')
->getQuery()
->getArrayResult();
}
public function findOneById($id)
{
return $this->getRepository()->createQueryBuilder('c')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}
}
CustomerBundle\Repository\CustomerRepository: ~
CustomerBundle\Repository\CustomerRepositoryInterface: '@CustomerBundle\Repository\CustomerRepository'
namespace CustomerBundle\Repository;
interface CustomerRepositoryInterface
{
public function findAll();
public function findOneById($id);
}
namespace CustomerBundle\Repository;
use Doctrine\ORM\EntityRepository;
class CustomerRepository implements CustomerRepositoryInterface
{
private $entityRepository;
public function __construct(EntityRepository $entityRepository)
{
$this->entityRepository = $entityRepository;
}
public function findAll()
{
return $this->entityRepository->createQueryBuilder('c')
->getQuery()
->getArrayResult();
}
public function findOneById($id)
{
return $this->entityRepository->createQueryBuilder('c')
->where('c.id = :id')
->setParameter('id', $id)
->getQuery()
->getOneOrNullResult();
}
}
services:
customer.doctrine.entity_repository:
class: Doctrine\ORM\EntityRepository
factory: [ "@doctrine.orm.entity_manager", getRepository ]
arguments:
- CustomerBundle\Entity\Customer
customer.repository.customer:
class: CustomerBundle\Repository\CustomerRepository
arguments:
- "@customer.doctrine.entity_repository"