Zend Framework:如何通过指定列的值查找表行?
我正在实现我的模型,就像快速入门指南。
在我的模型中,我尝试实现一个 findByToken()
方法。当前的 find()
方法接受 $id
参数,但我想通过不同列的值进行查找。
//excerpt from the quickstart guide
public function find($id, Default_Model_Guestbook $guestbook)
{
$result = $this->getDbTable()->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
$guestbook->setId($row->id)
->setEmail($row->email)
->setComment($row->comment)
->setCreated($row->created);
}
我尝试做这样的事情,但我认为它不起作用:
$db = $this->getDbTable();
$where = $db->getAdapter()->quoteInto('token = ?', $token);
$result = $db->find($where);
按指定列的值查找行的正确方法是什么?
I am implementing my model exactly like the quickstart guide.
In my model I am trying to implement a findByToken()
method. The current find()
method accepts an $id
parameter, but I want to find by the value of a different column.
//excerpt from the quickstart guide
public function find($id, Default_Model_Guestbook $guestbook)
{
$result = $this->getDbTable()->find($id);
if (0 == count($result)) {
return;
}
$row = $result->current();
$guestbook->setId($row->id)
->setEmail($row->email)
->setComment($row->comment)
->setCreated($row->created);
}
I tried doing something like this, but I don't think it worked:
$db = $this->getDbTable();
$where = $db->getAdapter()->quoteInto('token = ?', $token);
$result = $db->find($where);
What would be the proper way to find a row by the value of a specified column?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
或者如果您试图仅检索一行。
您还可以使用 Zend_Db_Select 对象,使适配器进一步抽象:
or if you are trying to retrieve only one row.
You could also use the Zend_Db_Select Object, keeping the adapter a little further abstracted:
这可以通过创建选择对象并使用该对象获取行来轻松完成。它在手册中有很好的描述: http: //framework.zend.com/manual/en/zend.db.select.html#zend.db.select.execute
您的代码可能如下所示:
This can be easily done by creating select object and fetching a row using this object. It's well described in manual: http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.execute
Your code could look like: