EbinPaulose
EbinPaulose

Reputation: 1139

how save multiple row of data with single query in cake php?

How make this Query in cake php? I used $this->saveAll(myData);. But this method used 5 query to insert this data. I want insert this data with one query.

INSERT INTO `table` (`id`, `value1`, `value2`) VALUES
(1, 1, 1,),
(2, 1, 2),
(3, 2, 2),
(4, 2, 3),
(5, 3, 3),

Upvotes: 2

Views: 3167

Answers (2)

jeremyharris
jeremyharris

Reputation: 7882

You can always use DboSource::insertMulti().

$data = array(
  'Model' => array(
    'field' => 'value',
    'other' => 'another value'
  )
);
$ds = $this->Model->getDatasource();
$ds->insertMuli($this->Model->table, array_keys($data['Model']), $data['Model']);

Upvotes: 1

Krishna
Krishna

Reputation: 1540

Try this method :

$this->Model->saveMany($data);

data should be in this format :

Array
(
[1] => Array
    (
        [Model] => Array
            (
                [field] => value
                [field] => value
            )

    )

[2] => Array
    (
        [Model] => Array
            (
                [field_1] => value
                [field_2] => value
            )

    )

)

Upvotes: 0

Related Questions