in zf2, want save data coming html form database. have 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 id
column (int, auto_increment, primary key), , created
column (timestamp).
once submit button in html form pressed, mapper's save
method called. after execution of $stmt->execute()
, should see new line in parties
table. check db, there's nothing. problem, likely, resides in $stmt->execute()
because if put \zend\debug\debug::dump($action)
+ die()
after $stmt->execute()
, page doesn't die()
, shows original form. works, however, if execute raw query without preparation, this:
$sql = 'insert parties (created) values (now());'; $this->dbadapter->query($sql, \zend\db\adapter\adapter::query_mode_execute);
could tell what's wrong?
just in case, below 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 `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) }
you can write :
use zend\db\sql\expression; $action->values(['created' => new expression('now()')]);
Comments
Post a Comment