Declare the entity classes in config files, inject the repositories with DI

parameters:
    my.entity.class: MyNamespace\MyEntity

service:
    my.repository:
        class: MyRepo
        factory_service: doctrine.orm.entity_manager
        factory_method: getRepository
        arguments:
            - %my.entity.class%

Le jeudi 4 septembre 2014 16:05:59 UTC+2, Sebastian Brandner a écrit :
>
> Hi!
>
> I don't relly like the usage of strings when the same value is used all 
> over the project.
> To write 100x 'MyNamespace\Model\MyEntity' is quite bad if you want to 
> refactor things some times.
>
> From doctrine manual:
>
> $user = $em->find('MyProject\Domain\User', $id);
>
>
> From symfony manual:
>
> $repository = $this->getDoctrine()->getRepository(
> 'AcmeStoreBundle:Product');
>
> As long as your entity lives under the Entity namespace of your bundle, 
> this will work.
>
>
> My best idea is to implement a constant and use this when requesting a 
> repository.
>
> namespace MyNamespace;
>
> class MyEntity {
>     const ENTITY_NAME = __CLASS__;
> }
>
> and then call
>
> $this->getDoctrine()->getRepository(MyNamespace\MyEntity::ENTITY_NAME);
>
> With this construct I'm able to refactor the classes because the 
> MyNamespace\MyEntity will be updated or will be invalid.
>
> *Another possiblity would be a trait with "public static function 
> entityName() { return __CLASS__; }" and use this in every entity, but i 
> think this is the same solution. :) *
>
>
>
>
> Does anyone have a better idea?
>
>
>
> kr sebastian
>

-- 
You received this message because you are subscribed to the Google Groups 
"doctrine-user" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/doctrine-user.
For more options, visit https://groups.google.com/d/optout.

Reply via email to