/** * Inserts SQL record into database. This record will be erased after the test. * * ``` php * <?php * $I->haveInDatabase('users', array('name' => 'miles', 'email' => '*****@*****.**')); * ?> * ``` * * @param $table * @param array $data * * @return integer $id */ public function haveInDatabase($table, array $data) { $query = $this->driver->insert($table, $data); $this->debugSection('Query', $query); $this->driver->executeQuery($query, array_values($data)); try { $lastInsertId = (int) $this->driver->lastInsertId($table); } catch (\PDOException $e) { // ignore errors due to uncommon DB structure, // such as tables without _id_seq in PGSQL $lastInsertId = 0; } $this->addInsertedRow($table, $data, $lastInsertId); return $lastInsertId; }
/** * Inserts SQL record into database. This record will be erased after the test. * * ``` php * <?php * $I->haveInDatabase('users', array('name' => 'miles', 'email' => '*****@*****.**')); * ?> * ``` * * @param $table * @param array $data * * @return integer $id */ public function haveInDatabase($table, array $data) { $query = $this->driver->insert($table, $data); $this->debugSection('Query', $query); $sth = $this->driver->getDbh()->prepare($query); if (!$sth) { $this->fail("Query '{$query}' can't be executed."); } $i = 1; foreach ($data as $val) { $sth->bindValue($i, $val); $i++; } $res = $sth->execute(); if (!$res) { $this->fail(sprintf("Record with %s couldn't be inserted into %s", json_encode($data), $table)); } try { $lastInsertId = (int) $this->driver->lastInsertId($table); } catch (\PDOException $e) { // ignore errors due to uncommon DB structure, // such as tables without _id_seq in PGSQL $lastInsertId = 0; } $this->insertedIds[] = ['table' => $table, 'id' => $lastInsertId, 'primary' => $this->driver->getPrimaryColumn($table)]; return $lastInsertId; }
/** * Inserts SQL record into database. This record will be erased after the test. * * ``` php * <?php * $I->haveInDatabase('users', array('name' => 'miles', 'email' => '*****@*****.**')); * ?> * ``` * * @param $table * @param array $data * @return integer $id */ public function haveInDatabase($table, array $data) { $query = $this->driver->insert($table, $data); $this->debugSection('Query', $query); $sth = $this->driver->getDbh()->prepare($query); if (!$sth) { $this->fail("Query '{$query}' can't be executed."); } $i = 1; foreach ($data as $val) { $sth->bindValue($i, $val); $i++; } $res = $sth->execute(); if (!$res) { $this->fail(sprintf("Record with %s couldn't be inserted into %s", json_encode($data), $table)); } $lastInsertId = (int) $this->driver->lastInsertId($table); $this->insertedIds[] = array('table' => $table, 'id' => $lastInsertId); return $lastInsertId; }