Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Doctrine2 Entity Namespace

i'm new to Doctrine2 and like to know how i can tell Doctrine which namespace my entities use. My current configuration is this.

All my entities are in namespace "project\entity". So, everytime i want to obtain the entity "Color", i have to write:

$em->getRepository("project\\entity\\Color")

How can i configure Doctrine to always use namespace "project\entity"?

like image 618
user2798515 Avatar asked Dec 03 '13 10:12

user2798515


People also ask

What is entity manager in doctrine?

The EntityManager is the central access point to ORM functionality. It can be used to find, persist, flush and remove entities.

What are doctrine proxies?

A Doctrine proxy is just a wrapper that extends an entity class to provide Lazy Loading for it. By default, when you ask the Entity Manager for an entity that is associated with another entity, the associated entity won't be loaded from the database, but wrapped into a proxy object.

How doctrine works?

Doctrine uses the Identity Map pattern to track objects. Whenever you fetch an object from the database, Doctrine will keep a reference to this object inside its UnitOfWork. The array holding all the entity references is two-levels deep and has the keys root entity name and id.

What is doctrine db?

The Doctrine Project is the home to several PHP libraries primarily focused on database storage and object mapping. The core projects are the Object Relational Mapper (ORM) and the Database Abstraction Layer (DBAL) it is built upon.


1 Answers

You can come close to what you want by using addEntityNamespace on your config object to create a namespace alias:

$em->getConfiguration()->addEntityNamespace('NS1', 'Project\Entity');

$colorRepo = $em->getRepository('NS1:Color');

Works for queries as well.

By the way, "project\\entity\\Color" can also be written as 'project\entity\Color'. I would also suggest capitalizing Project and Entity just to conform to standards.

like image 177
Cerad Avatar answered Sep 20 '22 05:09

Cerad