Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Symfony 3, query without Entity but with doctrine

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?

like image 522
Claudio Ɯǝıs Mulas Avatar asked Jun 26 '17 14:06

Claudio Ɯǝıs Mulas


People also ask

Does Symfony use doctrine?

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.

What is Doctrine query?

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).

What is doctrine repository?

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.

How should be the process to add a new entity to the app in Symfony?

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.


1 Answers

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.

like image 177
Matko Đipalo Avatar answered Sep 20 '22 17:09

Matko Đipalo