Question: Is there a way to write DQL queries without specifying the fully qualified name of the class?

Is there a way to create the following query:

```
return $this->getEntityManager()->createQuery(
    'SELECT i
     FROM Company\AccountingBundle\Entity\Invoice i
     WHERE i.id = :id'
)->setParameter('id', 1)
    ->getResult();

```

Like this :

```
use Company\AccountingBundle\Entity\Invoice;
....

return $this->getEntityManager()->createQuery(
    'SELECT i
     FROM Invoice i
     WHERE i.id = :id'
)->setParameter('id', 1)
    ->getResult();

```

Or something similar. I really would avoid to set the full FQN in the DQL.

But I didn't find any "setClass" or another method to do this.

**EDIT FOR CONTEXT**

In fact, it is a Symfony project and the query is inside in a Repository class. I have a Doctrine XML definition of the entity, an entity and the entity repository also defined in the XML, but I always need to explicitly define the FQN, Any ideas?

Answer: 
The Doctrine way would be to use entity repositories. If your entity is configured so that it's tied to a repository, e.g.:

```php
/**
 * @ORM\Entity(repositoryClass=InvoiceRepository::class)
 */
class Invoice

```

then in that repository, if you utilize a query builder, that entity is [automatically tied to the query](https://symfony.com/doc/current/doctrine.html#querying-with-the-query-builder):

```
$qb = $this->createQueryBuilder('i') // 'i' is just an alias here
    ->where('i.id = :id')
    ->setParameter('id', $id);

```

To see all the options it offers, you can read more on the query builder [here](https://www.doctrine-project.org/projects/doctrine-orm/en/2.7/reference/query-builder.html).

---

Quick alternatives (not including Doctrine functionalities), which I'll leave here for demonstrative purposes:

**1. Using `sprintf`**

```php
$q = sprintf('SELECT * FROM %s WHERE id = :id', Invoice::class);

return $this->getEntityManager()
    ->createQuery($q)
    ->setParameter('id', 1)
    ->getResult();

```

**2. Using `HEREDOC` syntax**

```php
$entity = Invoice::class;
$q = <<<SQL
    SELECT *
    FROM {$entity}
    WHERE id = :id
SQL;

return $this->getEntityManager()
    ->createQuery($q)
    ->setParameter('id', 1)
    ->getResult();

```

In both cases the output of the query variable is `SELECT * FROM App\Entity\Invoice WHERE id = :id`.

Note that you must initialize a variable for `HEREDOC`, as it cannot directly evaluate `{Invoice::class}`. That option would definitely be suitable for longer queries (especially since it highlights SQL keywords), while `sprintf` can be utilized for short, straightforward ones.
I apologize if you explicitly need DQL, but I haven't seen anyone mention DBAL QueryBuilder.

Here is your query, in Repository class, using DBAL QueryBuilder:

```
return $this->getEntityManager()
->getConnection() //
->createQueryBuilder() // This is different query builder that doesn't automatically assume Entity
->select('i')
->from('invoice', 'i') // Specify the table
->where('i.id = :id') // Rest of the syntax should be similar to DQL but you need to query against table columns, not properties (like in DQL) - for example, in DQL: ... where('firstName = :firstName') ... in DBAL: where('first_name = :firstName')
->setParameter('id', 1)
->execute()->fetchAllAssociative(); // Double check if you can fetch as stdClass

```

In any case, DBAL QueryBuilder is different from Doctrine Query Builder by being closer to the database layer.

More about DBAL QueryBuilder: <https://www.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/query-builder.html#sql-query-builder>