Model Class
insertAll()
Signature
Section titled “Signature”insertAll() — returns struct
Available in: model
Category: Create Functions
Description
Section titled “Description”Inserts multiple records into the database in a single batch operation.
Accepts an array of structs where each struct represents a record to insert.
All structs must have the same set of keys (property names).
Batches in groups of up to 1000 rows, fewer where the database limits the parameters per
statement (SQL Server). All batches run in one
transaction (see transaction), so a failing batch rolls back the ones before it. No
validations or callbacks run.
Parameters
Section titled “Parameters”| Name | Type | Required | Default | Description |
|---|---|---|---|---|
records | array | yes | — | Array of structs, each containing property name/value pairs to insert. |
timestamps | boolean | no | true | Set to false to skip automatic createdAt/updatedAt timestamping. |
transaction | string | no | [runtime expression] | Set this to commit to update the database, rollback to run all the database queries but not commit them, or none to skip transaction handling altogether. |
parameterize | any | no | true | Set to true to use cfqueryparam on all columns, or pass in a list of property names to use cfqueryparam on those only. |
Examples
Section titled “Examples”// 1. Insert multiple user records in a single batch
newUsers = [
{firstName: "Alice", lastName: "Smith", email: "alice@example.com"},
{firstName: "Bob", lastName: "Jones", email: "bob@example.com"},
{firstName: "Carol", lastName: "White", email: "carol@example.com"}
];
result = model("User").insertAll(records=newUsers);
// result -> {insertedCount: 3}
// 2. Insert records without automatic createdAt/updatedAt timestamps
rows = [
{username: "imported_1", score: 9800},
{username: "imported_2", score: 7450}
];
result = model("HighScore").insertAll(records=rows, timestamps=false);
// result -> {insertedCount: 2}
// 3. Insert a large dataset wrapped in a single transaction, using selective cfqueryparam
products = [];
for (i = 1; i <= 2500; i++) {
arrayAppend(products, {name: "Product #i#", price: RandRange(1, 999), stock: RandRange(0, 500)});
}
// Batches automatically in groups of 1000; all batches share one transaction.
result = model("Product").insertAll(
records = products,
transaction = "commit",
parameterize = "price,stock"
);
// result -> {insertedCount: 2500}