schmittjoh / JMSDiExtraBundle

Provides Advanced Dependency Injection Features for Symfony2
http://jmsyst.com/bundles/JMSDiExtraBundle
330 stars 130 forks source link

Entity Repository as service dont work ? #114

Open patie opened 11 years ago

patie commented 11 years ago

im trying

<?php namespace Acme\CoreBundle\Repository;

use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Inject;

use Acme\CoreBundle\Entity\User;

/**
 * @Service("core.repository.user")
 */
class UserRepository
{

    /**
     * @Inject("doctrine.orm.entity_manager")
     */
    private $entityManager;

    public function create(User $user)
    {
        $this->entityManager->persist($user);
        $this->entityManager->flush();
    }

}

_@Inject dont work in this case_

im tried also:

<?php namespace Acme\CoreBundle\Repository;

use Doctrine\ORM\EntityRepository;
use JMS\DiExtraBundle\Annotation\Service;
use JMS\DiExtraBundle\Annotation\Inject;

use Acme\CoreBundle\Entity\User;

/**
 * @Service("core.repository.user")
 */
class UserRepository extends EntityRepository
{

    public function create(User $user)
    {
        $this->_em->persist($user);
        $this->_em->flush();
    }

}

dont work too - but this is probably ok - because parent has container injection

what i need is Repository as service, because im using controllers as services.. its possible with DI Extra or i must register service standard way ? thanks !

stof commented 10 years ago

As explained in the documentation, @Inject on private and protected properties only works in controllers, not in services: http://jmsyst.com/bundles/JMSDiExtraBundle/master/usage#property-injection

ebuildy commented 7 years ago

I am not sure you can, since Repository need another service factory (doctrine.orm.default_entity_manager) and JMSDiExtraBundle dont provide a way to config factory_service.

Ping https://github.com/schmittjoh/JMSDiExtraBundle/issues/65