-
Notifications
You must be signed in to change notification settings - Fork 0
Processing data
This guide is about fetching data from your database and sending data to your database using Symfony.
"The EntityManager is your friend. He can fetch data, send data, a really good guy overall." - Bryan Kho
This is the EntityManager, it can fetch data and send data from and to your database. You can access the EntityManager from your controller.
$entityManager = $this->getDoctrine()->getManager();You can also access the EntityManager via the EntityManagerInterface object, which you can add as an argument to your method inside your Controller.
// don't forget the import
use Doctrine\ORM\EntityManagerInterface;
yourMethod(EntityManagerInterface $entityManager) {
}You can access the EntityManager from inside a Repository as well
// EntityManager from inside the Repository
$entityManager = $this->getEntityManager();You can use the getRepository() method of the EntityManager to find an object.
$product = $entityManager->getRepository(Product::class)->find($id);besides the find() method on the Repository, you can also use findBy(), findAll() and findOneBy().
$product = $entityManager->getRepository(Product::class)->findOneBy(['id' => $id])You can also access all your custom queries that you have made inside your Repository.
$products = $entitymanager->getRepository(Product::class)->findAllGreaterThanPrice($minPrice);A new Entity object you create must be persisted and then flushed in order to be sent to the database.
$product = new Product();
// prepare the new product for inserting it into the database
$entityManager->persist($product);
// execute the INSERT
$entityManager->flush();If your object already exists, you don't have to call persist(), you can immediately flush().
$product = $entityManager->getRepository(Product::class)->find($id);
// execute the UPDATE
$entityManager->flush();If you want to remove an existing object you have to call the remove() method.
$entityManager->remove($product);There are different ways to make queries in Symfony.
Inside the Repository class, you can call the EntityManager.
$entityManager = $this->getEntityManager();You need the Entitymanager to create a query. The following example looks for Products with a price larger than the price parameter.
/**
* @return Product[]
*/
public function findAllGreaterThanPrice($price): array
{
$entityManager = $this->getEntityManager();
$query = $entityManager->createQuery(
'SELECT p
FROM App\Entity\Product p
WHERE p.price > :price
ORDER BY p.price ASC'
)->setParameter('price', $price);
// returns an array of Product objects
return $query->getResult();
}You can also create a query from inside the Controller. One way to do this is with the QueryBuilder. You will need to call the createQueryBuilder() method on your Controller, which you can access with $this.
$qb = $this->createQueryBuilder('p')
->where('p.price > :price')
->setParameter('price', $price)
->orderBy('p.price', 'ASC');
$query = $qb->getQuery();
$query->execute();Another way to create a query is to not use Doctrine's QueryBuilder, but instead use a SQL query. This can be used in both the Repository and the Controller. Once again you can rely on good old EntityManager.
$conn = $entitymanager->getConnection();
$sql = '
SELECT * FROM product p
WHERE p.price > :price
ORDER BY p.price ASC
';
$stmt = $conn->prepare($sql);
$stmt->execute(['price' => $price]);
$stmt->fetchAll();Kerntaak 2 & 3
Symfony
= About code maintained by Symfony and not a third party
-
Home
-
Project Setup
-
Users
-
Unit testing
-
PDF
-
File upload
-
Text editing
-
Miscellaneous
-
Laravel
Work in progress.
ASP.NET MVC
= About code maintained or officially supported by Microsoft
-
Project Setup
-
ASP.NET Core MVC setup
- Model
- Controller
- View
-
- Unit Testing
- Inversion of control
ASP.NET Razor Pages
= About code maintained or officially supported by Microsoft
-
Project Setup
- TBA