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 BV260Y用MQTT向阿里云发布主题消息一直错误
  • ¥20 求个正点原子stm32f407开发版的贪吃蛇游戏
  • ¥15 正弦信号发生器串并联电路电阻无法保持同步怎么办
  • ¥15 划分vlan后,链路不通了?
  • ¥20 求各位懂行的人,注册表能不能看到usb使用得具体信息,干了什么,传输了什么数据
  • ¥15 Vue3 大型图片数据拖动排序
  • ¥15 Centos / PETGEM
  • ¥15 划分vlan后不通了
  • ¥20 用雷电模拟器安装百达屋apk一直闪退
  • ¥15 算能科技20240506咨询(拒绝大模型回答)