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: 
> 
> Doctrine offers a way to alias entity namespaces to simpler, shorter names to be used in DQL queries or for Repository access.
> 
> 
> 

You can set the **alias** of the entity namespace inside the Doctrine config of .yaml

```
doctrine:
  orm:
    auto_generate_proxy_classes: true
    naming_strategy: doctrine.orm.naming_strategy.underscore
    auto_mapping: true
    mappings:
        Company:
            is_bundle: false
            type: annotation
            dir: '%kernel.project_dir%/src/Company/AccountingBundle/Entity'
            prefix: 'App\Company\AccountingBundle\Entity'
            alias: AC

```

Then in the DQL, you can use "**AC:Invoice**":

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

```

References

[Symfony bundle Mapping Configuration](https://www.doctrine-project.org/projects/doctrine-bundle/en/2.2/configuration.html#mapping-configuration)

[Symfony Doctrine ORM Configuration](https://symfony.com/doc/current/reference/configuration/doctrine.html#alias)
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>