doctrine / DoctrineFixturesBundle

Symfony integration for the doctrine/data-fixtures library
MIT License
2.45k stars 202 forks source link

Argument 1 passed to Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader::addFixture() must be an instance of Doctrine\Common\DataFixtures\FixtureInterface #252

Closed sterichards closed 6 years ago

sterichards commented 6 years ago

Full error - Symfony\Component\Debug\Exception\FatalThrowableError: Type error: Argument 1 passed to Doctrine\Bundle\FixturesBundle\Loader\SymfonyFixturesLoader::addFixture() must be an instance of Doctrine\Common\DataFixtures\FixtureInterface, instance of Abstrat\OAuthBundle\Controller\TokenController given, called in /var/www/html/vendor/doctrine/doctrine-fixtures-bundle/Loader/SymfonyFixturesLoader.php on line 36 in /var/www/html/vendor/doctrine/doctrine-fixtures-bundle/Loader/SymfonyFixturesLoader.php on line 40

services.yml:

  abstrat.oauth.token_controller:
    class: Abstrat\OAuthBundle\Controller\TokenController
    arguments: ['@fos_oauth_server.server']
    calls:
      - [setContainer, ["@service_container"]]
    tags: [doctrine.fixture.orm]

How do stop TokenController being passed?

stof commented 6 years ago

Well, why are you tagging your controller as doctrine.fixture.orm ? That tag is the one making it being passed to the fixtures loader.

sterichards commented 6 years ago

This is a custom library that works fine with 2.7

I'm struggling to find any documentation that helps with migrating services.yml from 2.7 to 3.4

My struggle is that if I don't tag doctrine.fixture.orm then the fixtures don't run. If I do tag then I get the error in the title.

I'm struggling to find out how to get the fixtures of the library to run without problems and there isn't anybody else that's sharing my problem nor does the documentation at https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html help. I've tried implementing DependentFixtureInterface but that's doesn't auto tag it seems, as the documentation states

stof commented 6 years ago

Well, in older versions of DoctrineFixturesBundle, the doctrine.fixture.orm tag was doing nothing, as it was a new feature in DoctrineFixturesBundle 3.0. that's why you might not have this issue if you were also using an older version of the bundle.

sterichards commented 6 years ago

Can you understand why "This command looks for all services tagged with doctrine.fixture.orm. If you're using the default service configuration, any class that implements ORMFixtureInterface (for example, those extending from Fixture) will automatically be registered with this tag." wouldn't work for me and i've seen other people on Stack mentioning that it doesn't work for them either. This seems the only sensible solution I can find to get the DF's to run, if the documentation was true

Source of quote - https://symfony.com/doc/master/bundles/DoctrineFixturesBundle/index.html

stof commented 6 years ago

The issue you have is that you are manually adding the tag on a service which is not a fixture. and so, when looking for services having this tag, it will also find your controller (but trying to use it as a fixtures does not work).

You should not be tagging random services as doctrine.fixture.orm. that won't work (that's actually true for almost any tag which is being used for some effect actually)

stof commented 6 years ago

in your quote, there is 2 sentences. The second sentence does not apply at all in your case (you don't implement the interface, so the bundle will not automatically add the tag). But the first sentence applies. And it says all services tagged with doctrine.fixture.orm. It does not care about whether the tag was added automatically by the autoconfiguration system or explicitly in the service definition (and that's actually one of the strength of the way autoconfiguration works in Symfony)

sterichards commented 6 years ago

I've managed to define the DF's and tag them using the following:

Abstrat\OAuthBundle\DataFixtures\:
    resource: '../.././DataFixtures'
    tags: ['doctrine.fixture.orm']

This was only worked out because of a random Stack post I found. There is zero documentation that informs me that I need to do this :(

stof commented 6 years ago

@sterichards the issue you reported above was not related at all to not having the tag on your actual fixtures. It was caused by you adding the tag on a totally unrelated service.