The following answer is not mine, but OP, which was published in the question. I moved it to this community wiki answer.
I saved the link to the Connection object and used this to manually insert rows and update relationships. This avoids constant and identical generators. It is also possible to use a connection to transfer all this work into a transaction.
After you execute insert statements, you can update the relationship.
This is a good solution because it avoids any potential problems that may arise when replacing your configuration on a real server.
In your init function:
// Get the Connection $this->connection = $this->getContainer()->get('doctrine')->getEntityManager()->getConnection();
In your main body:
// Loop over my array of old data adding records $this->connection->beginTransaction(); foreach(array_slice($records, 1) as $record) { $this->addRecord($records[0], $record); } try { $this->connection->commit(); } catch(Exception $e) { $output->writeln($e->getMessage()); $this->connection->rollBack(); exit(1); }
Create this function:
// Add a record to the database using Connection protected function addRecord($columns, $oldRecord) { // Insert data into Record table $record = array(); foreach($columns as $key => $column) { $record[$column] = $oldRecord[$key]; } $record['id'] = $record['rkey']; // Insert the data $this->connection->insert('Record', $record); }
Leo lam
source share