Zend PHP - sqlsrv insert和带有fetchOne的Select_Scope()

时间:2012-06-06 10:46:36

标签: php sql-server zend-framework zend-db

我正在使用PHP和Zend Framework创建一个网站。我使用sqlsrv适配器与我的数据库通信。我正在尝试编写插入查询并存储新行的ID。我写了一个像这样的SQL查询:

$db = Zend_Db_Table::getDefaultAdapter();               
$id = $db->fetchOne("
    INSERT INTO    MyTable
    (Title, CreatedBy, Created, LastUpdatedBy, LastUpdated)
    SELECT  'MY Title',
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["Created"]."', 120),
        UserTable.UserId,
        CONVERT(datetime, '".$this->details["LastUpdated"]."', 120)
    FROM    UserTable
    WHERE   UserName = '".$this->details["CreatedBy"]."'
    SELECT  CAST(SCOPE_IDENTITY() AS int) AS Id"
);

但它会产生此错误:

'Zend_Db_Statement_Sqlsrv_Exception' with message 'The active result for the query contains no fields.

如何避免此错误并运行此查询并检索插入的行ID?

1 个答案:

答案 0 :(得分:0)

你似乎在这里使用了错误的方法。

您应该使用Zend_Db_Table的insert()方法: -

$table = new Zend_DB_Table(array('name' => 'mytable', primary => 'myPrimaryKey'));
$data = array()// of data to be inserted
$id = $table->insert($data);