ZF2: How to do this simple query with Zend\Db

馋奶兔 提交于 2019-12-12 03:09:02

问题


I have the following statements but they return an empty result set:

    $sql = 'SELECT * FROM `industry` WHERE `code` LIKE ?';
    $statement = $this->getAdapter()->createStatement($sql, array('A_'));
    $statement->execute();

What am I doing wrong? I really don't want to use the Zend\Db\Sql\Sql, as it is very verbose.

On a related point, where can I go to find out more about the theory of operation for Zend\Db? It's absolutely maddening. Why does it sometimes return a driver result? Sometimes a ResultSet? How can you view the complete SQL (after quoting, but before execution?) Etc...


回答1:


OK, I had missed the fact that a Result is iterable (as well as a ResultSet). Therefore, assigning the result of $statement->execute() to a variable, then iterating that variable, sorted out the problem.

Futhermore, you can call getResource() on the result object, and from there access the underlying object (in this case, a PDO Statement). This means you can do things like result->getResource()->fetchAll();




回答2:


Try with this query.

use Zend\Db\TableGateway\TableGateway;
use Zend\Db\Sql\Sql;

    class tableNameTable
    {

        protected $tableGateway;

        public function __construct(TableGateway $tableGateway)
        {
            $this->tableGateway = $tableGateway;
        }

        Publice function getIndustry(){
            $adapter = $this->tableGateway->getAdapter();
            $statement = $adapter->query("Your Query");
            $results = $statement->execute();

            return $results;
       }

    }


来源:https://stackoverflow.com/questions/20320790/zf2-how-to-do-this-simple-query-with-zend-db

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!