问题描述
只需要了解一些简单的数据库查询在ZF2。在ZF1中,我有如下简单的方法:
Just need a hand understanding some simple database queries in ZF2. In ZF1 I have simple methods like this:
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;
}
在ZF2中,我该如何做?我试过以下,但这只是返回看起来像一个结果对象,但我想要的是一个像ZF1的数组与fetchAll。如果我不得不迭代结果对象只提供数组以后,然后必须重复迭代,它看起来像一些重复的努力。
In ZF2, how would I do the same? I have tried the following, but this just returns what looks like a "Result" object, but all I want is an array like ZF1 did with fetchAll. If I have to iterate the result object only to provide the array later, which then must be iterated over again, it just seems like some duplication of effort.
无论如何,这里我在ZF2到目前为止:
Anyway, here's what I have in ZF2 so far:
//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;
return $this->view;
}
在输出中,我得到:
object(Zend\Db\Adapter\Driver\Pdo\Result)#197 (8) {
["statementMode":protected]=> string(7) "forward" ["resource":protected]=> object(PDOStatement)#195 (1) {
["queryString"]=> string(22) "select * from customer"
} ["options":protected]=> NULL ["currentComplete":protected]=> bool(false) ["currentData":protected]=> NULL ["position":protected]=> int(-1) ["generatedValue":protected]=> string(1) "0" ["rowCount":protected]=> NULL
}
但是,如果我将$ results更改为 $ results-> count();
我确实看到一个记录计数。我如何获得数据,虽然作为一个数组? (一个完整的记录集)
However, if I change $results to $results->count();
I do indeed see a record count. How do I get to the data though as an array? (a full recordset)
在某一时刻,我看到了一些像: $ results-> current()
但是只返回了一条记录。
At one point I did see something like: $results->current()
But that only returned a single record.
只是一个旁注。我看到所有的表抽象类,我可以使用,但在这一点上我的学习,我不想这样做。我只想要一些简单的按需查询,返回数组,就像他们在ZF1。在ZF2,似乎有太多的布线的东西在配置和东西,只是似乎过分。但是,作为一个框架,我喜欢的灵活性和主要的应用程序,我正在ZF1中真正受益于ZF2的模块化。 (否则我可能会和其他框架一起使用)
Just a side note. I do see all the table abstract classes I could use, but at this point in my learning, I don't want to do that. I just want some simple on-demand queries that return arrays like they did in ZF1. In ZF2, there's seems to be way too much "wiring up" of things in configs and stuff that just seem like overkill. But, as a framework, I like the flexibility and the main app I am working on in ZF1 could really benefit from the modularity of ZF2. (otherwise I'd probably go with other framework)
请原谅我的无知,非常感谢任何帮助。
Please forgive my ignorance, and thanks very much for any help!
推荐答案
好吧,我想我已经有了。至少这会做的工作,暂时。基本上,您必须添加一个额外的步骤,并将结果对象馈送到具有toArray方便方法的ResultSet对象。我想这可以做一百万种其他的方式,但...这工作。
Ok, I think I've got it. At least this will do the job for the time being. Basically, you have to add one extra step and feed the result object into a ResultSet object which has a toArray convenience method. I suppose this could be done a million other ways, but... this works.
请记住,我不会在控制器,甚至在这个确切的方法,但它只是在这一点上的一个测试。有时候我想要这个可用,这是如何ZF2 可以做,如果有人想要的。
Keep in mind, I wouldn't do this in a controller, or even in this exact way, but its only a test at this point. There's times when I want this available, and this is how ZF2 can do it, if one desired. (never minding good/bad habits)
在控制器的顶部添加/使用ResultSet:
In the top of the Controller add/use the ResultSet:
use Zend\Db\ResultSet\ResultSet;
以下是测试动作:
public function blaAction()
{
$db = new DbAdapter(
array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=mydb;host=localhost',
'username' => 'root',
'password' => '',
)
);
$sql = 'select * from customer
where cust_nbr > ? and cust_nbr < ?';
$sql_result = $db->createStatement($sql, array(125000, 125200))->execute();
if($sql_result->count() > 0){
$results = new ResultSet();
$this->view->data = $results->initialize($sql_result)->toArray();
}
return $this->view;
}
toArray正在为你做一个foreach循环,所以,添加额外的数组循环我想避免,但没有看看ZF1版本的代码,也许它做同样的反正。
toArray is just doing a foreach loop for you, so, I guess its still adding extra array loops I wanted to avoid, but not having looked at ZF1 version of their code, maybe its doing the same anyway.
我可能做的是创建一个Zend \Db的简单的数据库包装器类,它替换了ZF1中的Zend_Registry语句,并添加了一个fetchAll和fetchOne方法,这样我可以很容易地将一堆ZF1代码移植到ZF2上。
What I will probably do is create a simple db wrapper class for Zend\Db that replaces my Zend_Registry statement from ZF1 and adds a fetchAll and fetchOne method, that way I can quickly port over a bunch of ZF1 code to ZF2 much easier.
感谢您在评论中的意见,我很感激。 :)
Thanks for your input in the comments, I appreciate it. :)
哦,我也想提一下。我遇到了这个桥类创建的人,这也许是有帮助的:
Oh, I also wanted to mention. I ran into this bridge class someone created, which might also be helpful:https://github.com/fballiano/zfbridge
EDIT:
因此返回的适配器结果是可迭代的结果。我不知道我采取了什么步骤导致我的困惑,但$ db-> query中的结果作为一个Pdo \Result对象返回,并且可以循环在foreach很容易。让我搞砸的事实是,如果你var_dump它,它不显示数组数据,只是对象。
So the adapter results returned are iterable it turns out. I am not sure what steps I took that led to my confusion, but the results in $db->query are returned as a Pdo\Result object and that can be looped in foreach easy enough. What messed me up was the fact that if you var_dump it,it doesn't show the array data, just the object. That led me down a totally confusing path.
现在,即使上面的工作,这是更好的IMO,因为我们可以采取该对象,发送它在我们想要的地方迭代后。 (而不是循环遍历整个事情,首先创建数组,只是迭代另一个循环,浪费时间的方式)
Now, even though the above works, this is better IMO, because we can take that object, send it where we want for iteration later. (rather than loop over the whole thing to create an array first, only to iterate another loop, waste of time that way)
这里是一个工作示例,我更喜欢。你只是循环对象,并有你的数据!呃!不知道我有时错过简单的事情。 :)
Here's a working example I like better. you just loop the object, and there's your data! duh! Not sure how I miss the simple things sometimes. :)
public function blaAction()
{
$db = new DbAdapter(
array(
'driver' => 'Pdo',
'dsn' => 'mysql:dbname=gwdb;host=localhost',
'username' => 'root',
'password' => '',
)
);
$sql = 'select * from customer
where cust_nbr > ? and cust_nbr < ?';
$rs = $db->query($sql)->execute(array(125000, 125200));
// Source of confusion: this doesn't dump the array!!!
// It dumps the object properties for Pdo\Result
Debug::dump($rs);
// but it is still able to iterate records directly
// without toArray
foreach ($rs as $row){
Debug::dump($row);
}
return $this->view;
}
这篇关于Zend Framework 2 Db\Adapter\Adapter查询结果集如ZF1的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!