Objectiphy
  • Introduction
  • Quick Start
  • Basic Fetching
  • Basic Saving
  • Deleting
  • Mapping Providers
    • Attributes
    • Objectiphy Mapping
    • Doctrine Mapping
  • Defining Relationships
    • One to one
    • One to Many
    • Many to one
    • Many to Many
    • Scalar Joins
  • Query Builder
    • Running a Query
    • Select Queries
    • Update Queries
    • Insert Queries
    • Delete Queries
    • Criteria
      • Operators
      • Filtering by Child Objects
      • Filtering by Aggregates
    • Joins
  • Pagination and Sorting
  • Embedded Value Objects
  • Serialization Groups
  • Late Binding and Lazy Loading
  • Streaming Results
  • Custom Repositories
  • Optimisation
  • Configuration Options
  • Mapping Overrides
  • Caching
  • Comparison with Doctrine
  • Code Generation etc.
  • Troubleshooting
  • Licence, Copyright, Credits
Powered by GitBook
On this page

Was this helpful?

  1. Query Builder

Insert Queries

Perform single or bulk inserts without creating any entities

PreviousUpdate QueriesNextDelete Queries

Last updated 3 years ago

Was this helpful?

If you create a new entity, you can save it by calling saveEntity as described on the page. There might be times however, when you want to quickly save one or more new records without necessarily creating new entities for each record. You can achieve this using insert queries.

$query = QB::create()
    ->insert(Contact::class)
    ->set([
        'firstName' => 'Anna', 
        'lastName' => 'Skywalker'
    ])
    ->buildInsertQuery();
$repository->executeQuery($query);
$lastInsertId = $this->objectRepository->getLastInsertId();

You can also insert multiple records in a single query, which will send a single SQL statement to the database (unlike the saveEntities method, which has to loop through the entities and send a query to the database for each one):

$query = QB::create()
    ->insert(TestContact::class)
    ->set(['firstName' => 'Englebert', 'lastName' => 'Skywalker'])
    ->set(['firstName' => 'Jemima', 'lastName' => 'Skywalker'])
    ->set(['firstName' => 'Jedediah', 'lastName' => 'Skywalker'])
    ->set(['firstName' => 'Trixie', 'lastName' => 'Skywalker'])
    ->buildInsertQuery();
$insertCount = $repository->executeQuery($query);
Basic Saving