jcropp
jcropp

Reputation: 1246

how to insert records into a table with Doctrine

I am using zf2 and Doctrine for a project I am developing, and I need to create a function that takes all of the records from a SELECT query and inserts or appends them into another table (or entity). The Doctrine documentation says that QueryBuilder only offers SELECT, DELETE and UPDATE functionality, and I remember reading somewhere that INSERT and APPEND are not included because Doctrine doesn't want to lose control of related records that might go missing by only updating one table that has relationships with others.

How can I use Doctrine to add a group of records to a table?

Upvotes: 0

Views: 1360

Answers (1)

danopz
danopz

Reputation: 3408

You can create enities from records and push them to db:

foreach ( $records as $record ) {
    $someEntity = new SomeTable();

    $someEntity->setName($record['name']);
    $someEntity->setFoo($record['bar']);

    $em->persist($someEntity);
}

$em->flush();

Upvotes: 2

Related Questions