Symfony 4 / doctrine insert initial database data The 2019 Stack Overflow Developer Survey Results Are InGet a list of tables/models in Doctrine 1.2 - Symfony 1.4 and describe?Doctrine Migrations and Fixtures: Getting entities loaded in a migration from a fixtureHow do I write a Doctrine migration which can redistribute data into new tablesDoctrine migrations table collationHow to test Doctrine Migrations?Symfony + Doctrine Oracle DateTime format issueSymfony/Doctrine: Entity “schema” annotation not environment dependent?Symfony3 Doctrine Migration with multiple databasesHow to set initial data (not test data) in Symfony with doctrine ORMFunctional Testing Symfony with mulitple Doctrine EntityManagers

aging parents with no investments

Pristine Bit Checking

Extreme, unacceptable situation and I can't attend work tomorrow morning

Are USB sockets on wall outlets live all the time, even when the switch is off?

How to create dashed lines/arrows in Illustrator

Inversion Puzzle

Why is Grand Jury testimony secret?

Idiomatic way to prevent slicing?

Monty Hall variation

Does it makes sense to buy a new cycle to learn riding?

Understanding the implication of what "well-defined" means for the operation in quotient group

Is this food a bread or a loaf?

JSON.serialize: is it possible to suppress null values of a map?

Where to refill my bottle in India?

How to manage monthly salary

Does light intensity oscillate really fast since it is a wave?

Output the Arecibo Message

Spanish for "widget"

Patience, young "Padovan"

What does "sndry explns" mean in one of the Hitchhiker's guide books?

What do the Banks children have against barley water?

Should I use my personal or workplace e-mail when registering to external websites for work purpose?

Deadlock Graph and Interpretation, solution to avoid

Why can Shazam do this?



Symfony 4 / doctrine insert initial database data



The 2019 Stack Overflow Developer Survey Results Are InGet a list of tables/models in Doctrine 1.2 - Symfony 1.4 and describe?Doctrine Migrations and Fixtures: Getting entities loaded in a migration from a fixtureHow do I write a Doctrine migration which can redistribute data into new tablesDoctrine migrations table collationHow to test Doctrine Migrations?Symfony + Doctrine Oracle DateTime format issueSymfony/Doctrine: Entity “schema” annotation not environment dependent?Symfony3 Doctrine Migration with multiple databasesHow to set initial data (not test data) in Symfony with doctrine ORMFunctional Testing Symfony with mulitple Doctrine EntityManagers



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I am using symfony 4.2 and looking for a way to insert initial data to the database.
What I was trying to create is something like an initial setup script that do some sql inserts (like fixtures but for the production environment as well) that can be called by a console command.



I was thinking of using the postUp function in the doctrine migration class but I don’t want to rewrite this function for every production environment I set up.
Is there a way to use doctrines migration functionality for this purpose or is there a preferred way?



Example workflow:




  • Install symfony by cloning the git repository

  • Execute migrations to create / update the database tables

  • Execute the setup script to insert the needed default values into the database tables









share|improve this question

















  • 1





    So, create a command and run it.

    – u_mulder
    Mar 8 at 8:54











  • At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

    – igi
    Mar 8 at 14:23

















4















I am using symfony 4.2 and looking for a way to insert initial data to the database.
What I was trying to create is something like an initial setup script that do some sql inserts (like fixtures but for the production environment as well) that can be called by a console command.



I was thinking of using the postUp function in the doctrine migration class but I don’t want to rewrite this function for every production environment I set up.
Is there a way to use doctrines migration functionality for this purpose or is there a preferred way?



Example workflow:




  • Install symfony by cloning the git repository

  • Execute migrations to create / update the database tables

  • Execute the setup script to insert the needed default values into the database tables









share|improve this question

















  • 1





    So, create a command and run it.

    – u_mulder
    Mar 8 at 8:54











  • At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

    – igi
    Mar 8 at 14:23













4












4








4








I am using symfony 4.2 and looking for a way to insert initial data to the database.
What I was trying to create is something like an initial setup script that do some sql inserts (like fixtures but for the production environment as well) that can be called by a console command.



I was thinking of using the postUp function in the doctrine migration class but I don’t want to rewrite this function for every production environment I set up.
Is there a way to use doctrines migration functionality for this purpose or is there a preferred way?



Example workflow:




  • Install symfony by cloning the git repository

  • Execute migrations to create / update the database tables

  • Execute the setup script to insert the needed default values into the database tables









share|improve this question














I am using symfony 4.2 and looking for a way to insert initial data to the database.
What I was trying to create is something like an initial setup script that do some sql inserts (like fixtures but for the production environment as well) that can be called by a console command.



I was thinking of using the postUp function in the doctrine migration class but I don’t want to rewrite this function for every production environment I set up.
Is there a way to use doctrines migration functionality for this purpose or is there a preferred way?



Example workflow:




  • Install symfony by cloning the git repository

  • Execute migrations to create / update the database tables

  • Execute the setup script to insert the needed default values into the database tables






php symfony doctrine doctrine-migrations






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 8:44









igiigi

387




387







  • 1





    So, create a command and run it.

    – u_mulder
    Mar 8 at 8:54











  • At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

    – igi
    Mar 8 at 14:23












  • 1





    So, create a command and run it.

    – u_mulder
    Mar 8 at 8:54











  • At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

    – igi
    Mar 8 at 14:23







1




1





So, create a command and run it.

– u_mulder
Mar 8 at 8:54





So, create a command and run it.

– u_mulder
Mar 8 at 8:54













At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

– igi
Mar 8 at 14:23





At the end, I did it that way. I created a custom command to load the doctrine manager by an ContainerInterface. I wasn’t familiar with the creation of a custom command. Here is the reference I used: symfony.com/doc/current/console.html

– igi
Mar 8 at 14:23












1 Answer
1






active

oldest

votes


















1














Using the doctrine fixtures bundle in production



While this is discouraged because you can accidentally drop your production database with this, I do see some valid use cases for using the dotrine fixtures bundle in a production environment.



You can edit your config/bundles.php to enable the doctrine fixtures bundle in the production environment.



Change



DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['dev' => true, 'test' => true],


to



DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['all' => true],


Create your own command for fixture loading



Using doctrine functionality to manually load the fixtures is also discouraged, but that way you can add additional checks, for example only populate a fresh database.



$fixtures = (new DoctrineCommonDataFixturesLoader())->loadFromDirectory(__DIR__ . '/../src/DataFixture');
$loader = new DoctrineBundleFixturesBundleLoaderSymfonyFixturesLoader(new SymfonyComponentDependencyInjectionContainer());
$loader->addFixtures(
array_map(
function ($fixture)
return [
'fixture' => $fixture,
'groups' => []
];
,
$fixtures
)
);

$purger = new DoctrineCommonDataFixturesPurgerORMPurger($entityManager);

$executor = new DoctrineCommonDataFixturesExecutorORMExecutor($entityManager, $purger);
$executor->execute(
$loader->getFixtures()
);





share|improve this answer

























    Your Answer






    StackExchange.ifUsing("editor", function ()
    StackExchange.using("externalEditor", function ()
    StackExchange.using("snippets", function ()
    StackExchange.snippets.init();
    );
    );
    , "code-snippets");

    StackExchange.ready(function()
    var channelOptions =
    tags: "".split(" "),
    id: "1"
    ;
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function()
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled)
    StackExchange.using("snippets", function()
    createEditor();
    );

    else
    createEditor();

    );

    function createEditor()
    StackExchange.prepareEditor(
    heartbeatType: 'answer',
    autoActivateHeartbeat: false,
    convertImagesToLinks: true,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: 10,
    bindNavPrevention: true,
    postfix: "",
    imageUploader:
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    ,
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059543%2fsymfony-4-doctrine-insert-initial-database-data%23new-answer', 'question_page');

    );

    Post as a guest















    Required, but never shown

























    1 Answer
    1






    active

    oldest

    votes








    1 Answer
    1






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    Using the doctrine fixtures bundle in production



    While this is discouraged because you can accidentally drop your production database with this, I do see some valid use cases for using the dotrine fixtures bundle in a production environment.



    You can edit your config/bundles.php to enable the doctrine fixtures bundle in the production environment.



    Change



    DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['dev' => true, 'test' => true],


    to



    DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['all' => true],


    Create your own command for fixture loading



    Using doctrine functionality to manually load the fixtures is also discouraged, but that way you can add additional checks, for example only populate a fresh database.



    $fixtures = (new DoctrineCommonDataFixturesLoader())->loadFromDirectory(__DIR__ . '/../src/DataFixture');
    $loader = new DoctrineBundleFixturesBundleLoaderSymfonyFixturesLoader(new SymfonyComponentDependencyInjectionContainer());
    $loader->addFixtures(
    array_map(
    function ($fixture)
    return [
    'fixture' => $fixture,
    'groups' => []
    ];
    ,
    $fixtures
    )
    );

    $purger = new DoctrineCommonDataFixturesPurgerORMPurger($entityManager);

    $executor = new DoctrineCommonDataFixturesExecutorORMExecutor($entityManager, $purger);
    $executor->execute(
    $loader->getFixtures()
    );





    share|improve this answer





























      1














      Using the doctrine fixtures bundle in production



      While this is discouraged because you can accidentally drop your production database with this, I do see some valid use cases for using the dotrine fixtures bundle in a production environment.



      You can edit your config/bundles.php to enable the doctrine fixtures bundle in the production environment.



      Change



      DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['dev' => true, 'test' => true],


      to



      DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['all' => true],


      Create your own command for fixture loading



      Using doctrine functionality to manually load the fixtures is also discouraged, but that way you can add additional checks, for example only populate a fresh database.



      $fixtures = (new DoctrineCommonDataFixturesLoader())->loadFromDirectory(__DIR__ . '/../src/DataFixture');
      $loader = new DoctrineBundleFixturesBundleLoaderSymfonyFixturesLoader(new SymfonyComponentDependencyInjectionContainer());
      $loader->addFixtures(
      array_map(
      function ($fixture)
      return [
      'fixture' => $fixture,
      'groups' => []
      ];
      ,
      $fixtures
      )
      );

      $purger = new DoctrineCommonDataFixturesPurgerORMPurger($entityManager);

      $executor = new DoctrineCommonDataFixturesExecutorORMExecutor($entityManager, $purger);
      $executor->execute(
      $loader->getFixtures()
      );





      share|improve this answer



























        1












        1








        1







        Using the doctrine fixtures bundle in production



        While this is discouraged because you can accidentally drop your production database with this, I do see some valid use cases for using the dotrine fixtures bundle in a production environment.



        You can edit your config/bundles.php to enable the doctrine fixtures bundle in the production environment.



        Change



        DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['dev' => true, 'test' => true],


        to



        DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['all' => true],


        Create your own command for fixture loading



        Using doctrine functionality to manually load the fixtures is also discouraged, but that way you can add additional checks, for example only populate a fresh database.



        $fixtures = (new DoctrineCommonDataFixturesLoader())->loadFromDirectory(__DIR__ . '/../src/DataFixture');
        $loader = new DoctrineBundleFixturesBundleLoaderSymfonyFixturesLoader(new SymfonyComponentDependencyInjectionContainer());
        $loader->addFixtures(
        array_map(
        function ($fixture)
        return [
        'fixture' => $fixture,
        'groups' => []
        ];
        ,
        $fixtures
        )
        );

        $purger = new DoctrineCommonDataFixturesPurgerORMPurger($entityManager);

        $executor = new DoctrineCommonDataFixturesExecutorORMExecutor($entityManager, $purger);
        $executor->execute(
        $loader->getFixtures()
        );





        share|improve this answer















        Using the doctrine fixtures bundle in production



        While this is discouraged because you can accidentally drop your production database with this, I do see some valid use cases for using the dotrine fixtures bundle in a production environment.



        You can edit your config/bundles.php to enable the doctrine fixtures bundle in the production environment.



        Change



        DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['dev' => true, 'test' => true],


        to



        DoctrineBundleFixturesBundleDoctrineFixturesBundle::class => ['all' => true],


        Create your own command for fixture loading



        Using doctrine functionality to manually load the fixtures is also discouraged, but that way you can add additional checks, for example only populate a fresh database.



        $fixtures = (new DoctrineCommonDataFixturesLoader())->loadFromDirectory(__DIR__ . '/../src/DataFixture');
        $loader = new DoctrineBundleFixturesBundleLoaderSymfonyFixturesLoader(new SymfonyComponentDependencyInjectionContainer());
        $loader->addFixtures(
        array_map(
        function ($fixture)
        return [
        'fixture' => $fixture,
        'groups' => []
        ];
        ,
        $fixtures
        )
        );

        $purger = new DoctrineCommonDataFixturesPurgerORMPurger($entityManager);

        $executor = new DoctrineCommonDataFixturesExecutorORMExecutor($entityManager, $purger);
        $executor->execute(
        $loader->getFixtures()
        );






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Mar 8 at 9:03

























        answered Mar 8 at 8:56









        kalehmannkalehmann

        2,7631124




        2,7631124





























            draft saved

            draft discarded
















































            Thanks for contributing an answer to Stack Overflow!


            • Please be sure to answer the question. Provide details and share your research!

            But avoid


            • Asking for help, clarification, or responding to other answers.

            • Making statements based on opinion; back them up with references or personal experience.

            To learn more, see our tips on writing great answers.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55059543%2fsymfony-4-doctrine-insert-initial-database-data%23new-answer', 'question_page');

            );

            Post as a guest















            Required, but never shown





















































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown

































            Required, but never shown














            Required, but never shown












            Required, but never shown







            Required, but never shown







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко