Search code examples
nette

Nette - database INSERT - number of affected rows


I would like to find out the number of affected (inserted) rows after inserting into the table. I didn't figure out how to do it in the documentation. The update returns the number of affected rows. The insert returns Nette\Database\Table\ActiveRow How do I get it?

$affected = $context->table('author')->insert([
    [
        'name' => 'Sansa Stark',
        'born' => null
    ], [
        'name' => 'Arya Stark',
        'born' => null
    ]
]);
bdump($affected); //  Nette\Database\Table\ActiveRow - I need the number of inserted records

Solution

  • Nette Database Explorer doesn't return count after insert(). It is not useful information as long as you can count data before insert by yourself.

    $data = [...];
    $count = count($data);
    $context->table('author')->insert($data);
    

    It works only with update and delete as is mentioned in documentation.

    $count = $context->table('author')
        ->where('id', 10)
        ->delete();
    

    It might be possible with getRowCount() over query in Nette Database Core