简体   繁体   中英

Symfony2, EntityManager::getRepository(Namespace\To\Some\Class::class OR '**Bundle:Entity')

Symfony2 docs say that I should use alias shortcut 'ByBundle:myEntity' for entity path:

$em->getRepository('ByBundle:myEntity');

But this string literal is not usefull - no refactoring, no fast and auto renaming of the entity class in IDE.

I use magic method ::class

$em->getRepository(\ByBundle\Entity\myEntity::class);

The Question: am I doing this right?

In fact the Symfony2 core team is using the ::class method for example for adding form field types like so: $builder->add('name',TextType::class,array(...)) since Symfony v2.8 i guess there's nothing wrong the way you do it.

UPDATE: This allows your IDE to throw an exception if the linked entity class namespace would change and you will be able to recognize this while the development process. That is definitely a "it's better to be safe than sorry" way of how to map the entity instead of having the full qualified namespace or even the alias as a string.

Yes. Using class reference is always better than string refence.

Using string usually means there is algorithm, that converts string to the class.

IDE friendly is also desired.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM