Executing Symfony 4 Custom Command in fixtures

I have a custom command in my symfony project to populate the database with the default data that the application need to work in both dev and prod environments.

For the dev environment I have a fixture script that depends on these default common data.
I’m trying to call my custom Symfony command in the fixture script so that I’m sure to have the required data to properly load my fixtures.

This is my custom command app:db:populate in “pseudo script”, just creating a bunch of entities, persit & flush. My custom command works fine when I call it through php bin/console app:db:populate

protected function execute(InputInterface $input, OutputInterface $output)
{
    // Creating a bunch of default entities, persist them and flush

    $data = new MyDefaultEntity();
    // ...
    $this->manager->persist($data);
    // ...
    $this->manager->flush();
}

Then, in my fixture script, I want to call app:db:populate first, because fixtures depends on these data. So I tried to use the Process class to execute my script this way :

public function load(ObjectManager $manager)
{
    // Execute the custom command
    $cmd = 'php bin/console app:db:populate';
    $process = new Process($cmd);
    $process->run(function ($type, $buffer) {
        if (Process::ERR === $type) {
            echo 'ERR > '.$buffer;
        } else {
            echo 'OUT > '.$buffer;
        }
    });

    // Then load the fixtures !
    // ...
}

The custom command seems to execute well until the $this->manager->flush();

I have the following error in my console (Data is obfuscated for the post):

In AbstractMySQLDriver.php line 36:

  An exception occurred while executing 'INSERT INTO ....(..., ..., ...) VALUES (?, ?, ?)' with params ["...", "...", "..."]:

  SQLSTATE[HY000]: General error: 1205 Lock wait timeout exceeded; try restarting transaction

I don’t know what to do regarding this error … Why the command is working normally when used through a classic console call and why it is not working in a Process ?

If you have any other strategy to help me achieve my goal, I’m listening 🙂

Have a nice day !

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *