2

I have created an Entity and generated the migration file. Now, I want to insert some data into the table in the MySQL database when doing the migration.

My up function is as follows.

public function up(Schema $schema) : void
{
  $data = array(
    'My Data 1',
    'My Data 2',
    'My Data 3',
  );

  $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

  $this->addSql('CREATE TABLE my_table (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');

  $this->addSql('INSERT INTO my_table (name) VALUES (?)', $data, \PDO::PARAM_STR);
}

I want to add all data in the $data array to my_data table. All elements in the $data array are strings.

When I run php bin/console doctrine:migrations:migrate, it gives the following error.

Argument 3 passed to Doctrine\Migrations\AbstractMigration::addSql() must be of the type array, int given...

How to fix this error and achieve what I want?

4
  • 1
    Check out the signature of addSql. Commented Nov 30, 2020 at 10:05
  • 1
    You might also want to take a look at an example of usage. Commented Nov 30, 2020 at 10:06
  • @El_Vanja Thank you for your comments. I managed to achieve what I wanted. I acknowledged you in my answer. :) Commented Nov 30, 2020 at 10:46
  • You're welcome. Glad to help. Commented Nov 30, 2020 at 10:49

2 Answers 2

1

I managed to achieve what I wanted as follows... This answer is inspired by the resources commented on my question by @El_Vanja.

public function up(Schema $schema) : void
{
  $data = array(
    'My Data 1',
    'My Data 2',
    'My Data 3',
  );

  $this->abortIf($this->connection->getDatabasePlatform()->getName() !== 'mysql', 'Migration can only be executed safely on \'mysql\'.');

  $this->addSql('CREATE TABLE my_table (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(100) NOT NULL, PRIMARY KEY(id)) DEFAULT CHARACTER SET utf8mb4 COLLATE `utf8mb4_unicode_ci` ENGINE = InnoDB');

  foreach($data as $d) {
    $this->addSql('INSERT INTO my_table (name) VALUES (?)', array($d));
  }
}
Sign up to request clarification or add additional context in comments.

Comments

0

Very late to the party, but perhaps this will help someone in the future. Using an associative array as input for the query described in Sennen Randika' answer will not work and throws the following error: Positional parameter at index 0 does not have a bound value.

So do not use an array structure as below:

  $data = array(
    'key1' => 'My Data 1',
    'key2' => 'My Data 2',
    'key3' => 'My Data 3',
  );

1 Comment

This should have been a comment on the referenced answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.