I've created an API system. Everything is perfect, it works with Elastic as Db. Unfortunately a single function need to add/edit/retrieve a row in an old mysql db.
I don't want to connect to db anytime but just when I need it. I'd like to use createQueryBuilder but without entity. Is it possible?
Symfony provides all the tools you need to use databases in your applications thanks to Doctrine, the best set of PHP libraries to work with databases. These tools support relational databases like MySQL and PostgreSQL and also NoSQL databases like MongoDB.
Doctrine Query Language (DQL) is an Object Query Language created for helping users in complex object retrieval. You should always consider using DQL (or raw SQL) when retrieving relational data efficiently (eg. when fetching users and their phonenumbers).
A repository in a term used by many ORMs (Object Relational Mappers), doctrine is just one of these. It means the place where our data can be accessed from, a repository of data. This is to distinguish it from a database as a repository does not care how its data is stored.
With the doctrine:database:create command we create a new database from the provided URL. With the make entity command, we create a new entity called City . The command creates two files: src/Entity/City. php and src/Repository/CityRepository.
You could use the DBAL to make a plain old query, which will return an array:
<?php
namespace AppBundle\Entity;
use Doctrine\ORM\EntityRepository;
class MyRepository extends EntityRepository
{
public function findSmth()
{
$conn = $this->getEntityManager()->getConnection();
$sql = 'SELECT * FROM my_table';
$stmt = $conn->prepare($sql);
$stmt->execute();
var_dump($stmt->fetchAll());die;
}
}
QueryBuilder just converts down to DQL, and DQL provides querying capabilities over your object model. So, I think you cannot use QB without entity.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With