PHP [SOLVED]: Cannot autowire service: Argument references class but no such service exists

PHP [SOLVED]: Cannot autowire service: Argument references class but no such service exists

Home Forums Scripting PHP Tutorials PHP [SOLVED]: Cannot autowire service: Argument references class but no such service exists

Viewing 2 posts - 1 through 2 (of 2 total)
  • Author
    Posts
  • #245536

    Cloudy Point
    Keymaster

    QuestionQuestion

    I’m upgrading a project from Symfony 3 to Symfony 4
    (https://github.com/symfony/symfony/blob/master/UPGRADE-4.0.md) and I have many repository/services like this:

    namespace AppEntity;
    
    use AppEntityActivation;
    use DoctrineORMEntityRepository;
    use PredisClient;
    
    class ActivationRepository extends EntityRepository
    {
        // ...
    }
    

    And when I try to run the project in the browser like this:

    http://localhost:8000/login
    

    I get this error:

    (1/1) RuntimeException
    Cannot autowire service "AppEntityActivationRepository": 
    argument "$class" of method 
    "DoctrineORMEntityRepository::__construct()" 
    references class "DoctrineORMMappingClassMetadata" 
    but no such service exists.
    

    Does this mean you have to create a service for “DoctrineORMMappingClassMetadata” in your services.yaml file?

    Thanks to autowiring my new services.yaml file is fairly small compared to the old one, which had 2000+ lines.
    The new services.yaml just has several of these (so far):

    App:
        resource: '../src/*'
    
    # Controllers
    AppController:
        resource: '../src/Controller'
        autowire: true
        public: true
        tags: ['controller.service_arguments']
    
    # Models
    AppModel:
        resource: '../src/Model/'
        autowire: true
        public: true
    
    // etc
    

    Question:
    Do you really need to add service definitions to services.yaml for third party vendor classes? And if so, can I get an example of how to do that please?
    Any advice from anyone who has already upgraded from Symfony 3 to Symfony 4 would be great.

    PHP 7.2.0-2+ubuntu16.04.1+deb.sury.org+2 (cli) (built: Dec 7 2017 20:14:31) ( NTS )
    Linux Mint 18, Apache2 Ubuntu.

    EDIT / FYI:

    This is the “DoctrineORMEntityRepository::__construct()” which the ActivationRepository extends:

    /**
         * Initializes a new <tt>EntityRepository</tt>.
         *
         * @param EntityManager         $em    The EntityManager to use.
         * @param MappingClassMetadata $class The class descriptor.
         */
        public function __construct(EntityManagerInterface $em, MappingClassMetadata $class)
        {
            $this->_entityName = $class->name;
            $this->_em         = $em;
            $this->_class      = $class;
        }
    

    which is located here:

    /vendor/doctrine/orm/lib/Doctrine/ORM/EntityRepository.php
    

    #245537

    Cloudy Point
    Keymaster

    Accepted AnswerAnswer

    Starting from the 1.8 version of DoctrineBundle, you can extends your class using DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository instead of DoctrineORMEntityRepository. The result will be the same, but this support the autowire.

    Example:

    use AppEntityActivation;
    use DoctrineBundleDoctrineBundleRepositoryServiceEntityRepository;
    use DoctrineCommonPersistenceManagerRegistry;
    
    class ActivationRepository extends ServiceEntityRepository
    {
        public function __construct(RegistryInterface $registry)
        {
            parent::__construct($registry, Activation::class);
        }
    
        // ...
    }
    

    Source: https://stackoverflow.com/questions/48024235/cannot-autowire-service-argument-references-class-but-no-such-service-exists
    Author: Federkun
    Creative Commons License
    This work is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 4.0 International License.

Viewing 2 posts - 1 through 2 (of 2 total)

You must be logged in to reply to this topic.