只需要了解ZF2中的一些简单数据库查询.在ZF1中我有这样简单的方法:
public function recordset()
{
// listing of all records
$db = Zend_Registry::get('db');
$sql = "SELECT " . $this->_selectlist() .
" from customer c";
$r = $db->fetchAll($sql);
return $r;
}
Run Code Online (Sandbox Code Playgroud)
在ZF2中,我该怎么做?我已经尝试了以下内容,但这只是返回看起来像"Result"对象的东西,但我想要的是像ZF1那样使用fetchAll执行的数组.如果我必须迭代结果对象只是为了稍后提供数组,然后必须重复迭代,它似乎只是一些重复工作.
无论如何,这是我到目前为止在ZF2中所拥有的:
//above the controller start I have: use Zend\Db\Adapter\Adapter as DbAdapter;
public function blaAction()
{
$db = new DbAdapter(
array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'root',
'password' => '',
)
);
$sql = 'select * from customer';
$stmt = $db->query($sql);
$results = $stmt->execute();
$this->view->data = $results; …Run Code Online (Sandbox Code Playgroud)