In ZF2, I want to save the data coming from an html form into the database. I have a data mapper class (simplified version):
class OrganizationMapper
{
protected $dbAdapter;
public function __construct(
AdapterInterface $dbAdapter,
) {
$this->dbAdapter = $dbAdapter;
}
public function save()
{
$action = new Insert('parties');
$action->values(['created' => 'NOW()']);
$sql = new Sql($this->dbAdapter);
$stmt = $sql->prepareStatementForSqlObject($action);
$result = $stmt->execute();
}
}
The table Parties
has the id
column (int, auto_increment, primary key), and created
column (timestamp).
Once the submit button in the html form is pressed, the mapper's save
method is called. After execution of $stmt->execute()
, I should see a new line in the Parties
table. But I check the DB, there's nothing. The problem, most likely, resides in $stmt->execute()
because if I put \Zend\Debug\Debug::dump($action)
+ die()
after $stmt->execute()
, the page doesn't die()
and shows the original form. Everything works, however, if I execute just a raw query without preparation, like this:
$sql = 'insert into parties (created) values (NOW());';
$this->dbAdapter->query($sql, \Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);
Could you tell what's wrong?
Just in case, below is the result of \Zend\Debug\Debug::dump($stmt)
+ die()
object(Zend\Db\Adapter\Driver\Pdo\Statement)#409 (9) {
["pdo":protected] => object(PDO)#405 (0) {
}
["profiler":protected] => NULL
["driver":protected] => object(Zend\Db\Adapter\Driver\Pdo\Pdo)#255 (4) {
["connection":protected] => object(Zend\Db\Adapter\Driver\Pdo\Connection)#256 (8) {
["driver":protected] => *RECURSION*
["resource":protected] => object(PDO)#405 (0) {
}
["dsn":protected] => string(62) "mysql:dbname=SampleDatabase;host=localhost;charset=utf8"
["connectionParameters":protected] => array(5) {
["driver"] => string(3) "Pdo"
["username"] => string(7) "MyUserName"
["password"] => string(12) "MySecretPassword"
["dsn"] => string(62) "mysql:dbname=SampleDatabase;host=localhost;charset=utf8"
["driver_options"] => array(3) {
[1002] => string(16) "SET NAMES 'UTF8'"
[1003] => string(16) "SET NAMES 'UTF8'"
[1004] => string(16) "SET NAMES 'UTF8'"
}
}
["driverName":protected] => string(5) "mysql"
["inTransaction":protected] => bool(false)
["nestedTransactionsCount":protected] => int(0)
["profiler":protected] => NULL
}
["statementPrototype":protected] => object(Zend\Db\Adapter\Driver\Pdo\Statement)#257 (9) {
["pdo":protected] => NULL
["profiler":protected] => NULL
["driver":protected] => *RECURSION*
["sql":protected] => string(0) ""
["isQuery":protected] => NULL
["parameterContainer":protected] => NULL
["parametersBound":protected] => bool(false)
["resource":protected] => NULL
["isPrepared":protected] => bool(false)
}
["resultPrototype":protected] => object(Zend\Db\Adapter\Driver\Pdo\Result)#258 (9) {
["statementMode":protected] => string(7) "forward"
["fetchMode":protected] => int(2)
["resource":protected] => NULL
["options":protected] => NULL
["currentComplete":protected] => bool(false)
["currentData":protected] => NULL
["position":protected] => int(-1)
["generatedValue":protected] => NULL
["rowCount":protected] => NULL
}
["features":protected] => array(0) {
}
}
["sql":protected] => string(51) "INSERT INTO `parties` (`created`) VALUES (:created)"
["isQuery":protected] => NULL
["parameterContainer":protected] => object(Zend\Db\Adapter\ParameterContainer)#400 (4) {
["data":protected] => array(1) {
["created"] => string(5) "NOW()"
}
["positions":protected] => array(1) {
[0] => string(7) "created"
}
["errata":protected] => array(0) {
}
["maxLength":protected] => array(0) {
}
}
["parametersBound":protected] => bool(false)
["resource":protected] => NULL
["isPrepared":protected] => bool(false)
}