duandingcu7010 2016-01-24 21:45
浏览 32
已采纳

Zend框架prepareStatementForSqlObject并执行不起作用

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)
}
  • 写回答

2条回答 默认 最新

  • doufuhao0566 2016-01-25 16:30
    关注

    You can write :

    use Zend\Db\Sql\Expression;
    
    $action->values(['created' => new Expression('NOW()')]);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 单通道放大电路的工作原理
  • ¥30 YOLO检测微调结果p为1
  • ¥20 求快手直播间榜单匿名采集ID用户名简单能学会的
  • ¥15 DS18B20内部ADC模数转换器
  • ¥15 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题