JMSDiExtraBundle icon indicating copy to clipboard operation
JMSDiExtraBundle copied to clipboard

Entity Repository as service dont work ?

Open patie opened this issue 12 years ago • 2 comments
trafficstars

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 !

patie avatar Jun 17 '13 08:06 patie

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

stof avatar Aug 20 '14 20:08 stof

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

ebuildy avatar Apr 22 '17 08:04 ebuildy