somnambulist-tech/cte-builder

Can not use ClassName::class as table name

jmgiaever opened this issue · 3 comments

Hi,

In Doctrine you're able to use mapping to tables. E.g

$qb = $this->_em->createQueryBuilder()
    ->select('a')
    ->from(EntityClassName::class, 'a');

$result = $qb->getQuery()->getResult();

or alternatively, for an DTO

$qb = $this->_em->createQueryBuilder()
    ->select('NEW \App\Entity\DTO\DTOClassName(a.field_1, a.field_n, ..., a.field_n+1')
    ->from(EntityClassName::class, 'a');

The result will consist of either EntityClassName-classes (first example) or the DTO class.

I haven't found a way of doing this with the CTE-builder, but I'm wondering if the getSQL method should call $this->query->getQuery()->getSQL() instead of $this->query->getSQL() to translate the mapping from EntityClassName::class to table_name.

....or is there any other way?

Thank you in advance.

Realized it's QueryBuilder from DBAL and not ORM that is used, and guess I wont be able to use mapping

@jmgiaever CTE Builder works with DBAL\QueryBuilder and generates pure SQL. It cannot be used with the ORM query builder. Remember DQL is not SQL and many operations are better done purely in SQL.

If you wanted to get the mapping data for a specific object (e.g. so the table info is entirely dynamic), you could access the EntityManager and get the MetaData class. That could then be fed into the CTE Builders select and wheres etc; but you'd still end up with an SQL query that is executed outside of the ORM (but using the same connection since it's DBAL under-the-hood).

@jmgiaever I am closing this issue now since it's not a bug. If you do decide to use the CTE Builder and experience other issues or need some help, then please make new issues and tag me so I get notified! I am more than happy to help out.

Thanks 😃