-
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);
// call some methods here, modify your entity, have some fun.
// execute the UPDATE
$entityManager->flush();If you want to remove an existing object you have to call the remove() method.
$entityManager->remove($product);
// execute Order 66
$entityManager->flush();There are different ways to make queries in Symfony.
Inside the Repository, you can use the QueryBuilder.
Via QueryBuilder:
/**
* @return Product[]
*/
public function findByExampleField($value)
{
return $this->createQueryBuilder('p')
->andWhere('p.price = :val')
->setParameter('val', $value)
->orderBy('p.id', 'ASC')
->setMaxResults(10)
->getQuery()
->getResult()
;
}You can also use a query, with good old 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.
Via query:
/**
* @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.
// No thoughts, head empty
$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();You may have noticed this $sql variable is just a string, you can implement this dirty shortcut where you don't have to add parameters. If you can access what would be the parameters, you can just concat the values inside the string. This is not recommended if you want to make your application not vulnerable to injection.
// Dirty Deeds Done Dirt Cheap
$price = "Look mum! I'm injecting SQL";
$sql = '
SELECT * FROM product p
WHERE p.price > '.$price.'
ORDER BY p.price ASC
';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