Love Fuel?    Donate

FuelPHP Forums

Ask your question about FuelPHP in the appropriate forum, or help others by answering their questions.
Insert multiple rows with DB::insert
  • Is it possible to insert multiple models while opening a single connection?

    I have an array of user data, and I don't want to do $model->save() for each of them as this would mean opening and closing connection multiple times.

    In plain mysql I'm currently doing:
    "INSERT INTO `table` (`val1`, `val2`, ...) VALUES
    ('value1', 'value2', .....)  // row1
    ('value1', 'value2', .....)  // row2
    ........

    I also tried DB::insert, but it does not seem like you can add an array of arrays as argument in the ->values() method...

    Thanks.
  • ->columns(array(
        'col1',
        'col2',
    ))->values(array(
        array('row1val1', 'row1val2'),
        array('row2val1', 'row2val2'),
    ));

    And so on should work fine.
  • BTW, Fuel doesn't open and close the connection for every query.

    It opens the connection when you execute the first DB command, and doesn't close it until the connection object is destroyed, which happens on shutdown. Assuming you don't call disconnect() off course...

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

In this Discussion