Zend\Db\TableGateway\AbstractTableGateway won't work with Sqlsrv db adapter #2885

Closed
dkidd opened this Issue Nov 2, 2012 · 4 comments

Comments

Projects
None yet
5 participants
@dkidd
Contributor

dkidd commented Nov 2, 2012

Performing a select using a TableGateway results in the following exception: 'createStatement() only accepts an SQL string or a Sqlsrv resource'

The executeSelect method of Zend\Db\TableGateway\AbstractTableGateway includes the following line:

// prepare and execute
$statement = $this->sql->prepareStatementForSqlObject($select);

Which calls the prepareStatementForSqlObject method of Zend\Db\Sql\Sql:

public function prepareStatementForSqlObject(PreparableSqlInterface $sqlObject, StatementInterface $statement = null)
    {
        $statement = ($statement) ?: $this->adapter->createStatement();
        ...

Since AbstractTableGateway doesn't pass a second parameter, $statement will always be null. But calling the createStatement method of Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv without an argument throws the exception given above.

public function createStatement($sqlOrResource = null)
    {
        $statement = clone $this->statementPrototype;
        if (is_string($sqlOrResource)) {
            $statement->setSql($sqlOrResource);
            if (!$this->connection->isConnected()) {
                $this->connection->connect();
            }
            $statement->initialize($this->connection->getResource());
        } elseif (is_resource($sqlOrResource)) {
            $statement->initialize($sqlOrResource); // will check the resource type
        } else {
            throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource');
        }
        return $statement;
}
@ridzeal

This comment has been minimized.

Show comment Hide comment
@ridzeal

ridzeal Nov 28, 2012

I managed to fixed this with modifying file Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv in function createStatement, replacing this line

throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource');

with

if (!$this->connection->isConnected()) {
    $this->connection->connect();
}
$statement->initialize($this->connection->getResource());

I don't know what should do with testing part, though

ridzeal commented Nov 28, 2012

I managed to fixed this with modifying file Zend\Db\Adapter\Driver\Sqlsrv\Sqlsrv in function createStatement, replacing this line

throw new Exception\InvalidArgumentException('createStatement() only accepts an SQL string or a Sqlsrv resource');

with

if (!$this->connection->isConnected()) {
    $this->connection->connect();
}
$statement->initialize($this->connection->getResource());

I don't know what should do with testing part, though

@Maks3w

This comment has been minimized.

Show comment Hide comment

@ghost ghost assigned ralphschindler Nov 28, 2012

@BenjaminNolan

This comment has been minimized.

Show comment Hide comment
@BenjaminNolan

BenjaminNolan Dec 10, 2012

Contributor

zendframework#3166 is the fix for this issue. (Posting it here for cross-referencing purposes.)

Contributor

BenjaminNolan commented Dec 10, 2012

zendframework#3166 is the fix for this issue. (Posting it here for cross-referencing purposes.)

@ralphschindler

This comment has been minimized.

Show comment Hide comment
@ralphschindler

ralphschindler Dec 19, 2012

Member

Fixed with pull request #3166 and associated changes.

Member

ralphschindler commented Dec 19, 2012

Fixed with pull request #3166 and associated changes.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment