Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

zf2 insert using $db->insert($table, $data); style

Hello is there any way to insert data in a db table using the zf1 style on zf2?

$db->insert('tablename', $data);

where $data is an associative array contains (columns, values)

thanks

like image 265
Luis Avatar asked Dec 04 '22 12:12

Luis


1 Answers

To make an insert in zf2:

    use Zend\Db\Sql\Sql;

    $sql = new Sql($this->dbAdapter);
    $insert = $sql->insert('table');
    $newData = array(
    'col1'=> 'val1',
    'col2'=> 'val2',
    'col3'=> 'val3'
    );
    $insert->values($newData);
    $selectString = $sql->getSqlStringForSqlObject($insert);
    $results = $this->dbAdapter->query($selectString, Adapter::QUERY_MODE_EXECUTE);
like image 147
Luis Avatar answered Dec 07 '22 23:12

Luis