Symfony Doctrine Nasıl Kullanılır ?

Murat Çakmak
2 min readJul 4, 2022

--

Doctrine’de CRUD işlemlerini basitçe ele alacağım ve bazı kullanım biçimlerini göstereceğim.

Symfony Doctrine

KULLANIM BİÇİMLERİ

1) Injection ile Model Yöntemi

public function createProduct(ManagerRegistry $doctrine): Response
{
//ManagerRegistry injection yapılarak EntityManager oluşturulur.
$entityManager = $doctrine->getManager();
//Model oluşturularak veriler aktarılır.
$product = new Product();
$product->setName('Keyboard');
$product->setPrice(1999);
$product->setDescription('Ergonomic and stylish!');

//EntityManager, Model bilgilerine göre yapılacak
işlemleri belirler.
$entityManager->persist($product);
//Persist'de hazırlanan işlemleri çalıştırır asıl sorgu çalışırır
$entityManager->flush();
}

2) QueryBuilder ile Sorgu Yapma

$this->createQueryBuilder('u')
->from('User', 'u')
->update()
->set('u.password', ':password')
->where('u.id = :id')
->setParameter('id', $user->getId())
->setParameter('password', $this->userPasswordHasher->hashPassword($user, $newHashedPassword))
->getQuery()
->execute();

3) SQL ile Sorgu Yapma

$conn = $this->getEntityManager()->getConnection();$sql = 'SELECT * FROM product p
WHERE p.price > :price
ORDER BY p.price ASC
';
$stmt = $conn->prepare($sql);
$resultSet = $stmt->executeQuery(['price' => $price]);
return $resultSet->fetchAllAssociative();

CRUD ÖRNEKLERİ

ADD

$entityManager = $doctrine->getManager();$task = new Task();
$task->setUser($user);
$task->setTitle($request->request->get('title'));
$task->setDescription($request->request->get('description'));
$task->setStatus($request->request->get('status'));
$entityManager->persist($product);
$entityManager->flush();

UPDATE

$entityManager = $doctrine->getManager();
$repository = $entityManager->getRepository(Product::class);
$product = $repository->find($id);
$product->setName('New product name!');
$entityManager->persist($product);
$entityManager->flush();

Injection ile Model’in Repository Sınıfı Yoksa Kullanım

public function update(ManagerRegistry $doctrine, int $id): Response
{
//ManagerRegistry injection yapılarak EntityManager oluşturulur.
$entityManager = $doctrine->getManager();
//Eğer modele ait bir repository'niz yoksa bu yöntem tercih edilebilir.
//getRepository ile model bilgisini vererek default olarak kullanabilecek hazır fonksiyonlar oluşturur.
$product = $entityManager
->getRepository(Product::class)->find($id);
if (!$product) {
throw $this->createNotFoundException(
'No product found for id '.$id
);
}
$product->setName('New product name!');
$entityManager->flush();
return $this->redirectToRoute('product_show', [
'id' => $product->getId()
]);
}

DELETE

$entityManager = $doctrine->getManager();
$repository = $entityManager->getRepository(Product::class);
$product = $repository->find($id);
$entityManager->remove($product);
$entityManager->flush();

SELECT

$repository = $doctrine->getRepository(Product::class);// genellikle aradığı id sütunu üzerinden tek satır çekmek için.
$product = $repository->find($id);
// Birden fazla şarta göre tek veri çekme
$product = $repository->findOneBy([
'name' => 'Keyboard',
'price' => 1999,
]);
// Birden fazla şarta göre birden fazla veri çekme
$products = $repository->findBy(
['name' => 'Keyboard'],
['price' => 'ASC']
);
// Tüm verileri çekme
$products = $repository->findAll();

Kaynak

--

--