简体   繁体   中英

ZF2 - TableGateway with Mysqli driver

I am trying to use a TableGateway class in ZF2 with the Mysqli driver but am having loads of problems.

For some reason, any Where clause I add with a variable never gets quoted in and when the query is executed it throws the error:

Statement couldn't be produced with sql: ...

The code that is giving me problems is this:

<?php

class MyTable extends Zend\Db\TableGateway\TableGateway
{
    public function get_count($id)
    {
        $count = $this->select(function (Zend\Db\Sql\Select $select) use ($id) {
            $select->columns([
                'count' => 'COUNT(field)',
            ]);
            $select->join(['alias1' => 'other_table'], 'thing = alias1.thing', []);
            $select->where([
                'main_table.id' => $id,
                'alias1.active' => 1,
            ]);
        })->current();

        return $count;
    }
}

When I look at the error, it just shows ? where the actual values supplied should be.

If though, I select straight from the adapter like this:

// build select object similar to above example
$selectString = $sql->getSqlStringForSqlObject($select);
$results = $this->dbAdapter->query($selectString, Zend\Db\Adapter\Adapter::QUERY_MODE_EXECUTE);

Then it works perfectly. But I have to use this getSqlStringForSqlObject which I find odd. In my other projects I am using the PDO driver and I never had such an issue but for this project the mysqli driver is required.

How can I make the values supplied to where get quoted in properly?

Try using prepareStatementForSqlObject . Also, if it's possible, please show us your db config.

prepareStatementForSqlObject() will return a StatementInterface object.
getSqlStringForSqlObject() will get you you a plain sql string.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM