pixie icon indicating copy to clipboard operation
pixie copied to clipboard

Row Count

Open robertaodj opened this issue 9 years ago • 1 comments

Sorry my english.

How to know the number of rows affected after an operation INSERT, UPDATE or DELETE? Would use at PDO: PDOStatement::rowCount .

robertaodj avatar Sep 29 '16 04:09 robertaodj

It depends on operation. For example For INSERT 1 row:

$query = $qb
    ->table('dummy')
    ->insert(
        array('name' => 'aaa')
    )
;
var_dump($query);

It returns ID directly:

string(2) "23"

By carefull, returned ID is always string, so in many cases you have to map this value to int.

For INSERT >1 row:

$query = $qb
    ->table('dummy')
    ->insert(array(
        array('name' => 'aaa'),
        array('name' => 'bbb'),
    ))
;
var_dump($query);

It returns array of IDs:

array(2) {
  [0]=>
  string(2) "24"
  [1]=>
  string(2) "25"
}

For UPDATE/DELETE row For UPDATE/DELETE row(s) we have to use rowCount() method.

$query = $qb
    ->table('dummy')
    ->where('id', 4)
    ->update(
        array('name' => 'newValue')
    )
;
var_dump($query->rowCount());

It returns numbers of changed rows. Value can by equal 0 if row is not changed/deleted.

int(1)

mrcnpdlk avatar Nov 15 '16 20:11 mrcnpdlk