Zend Framework:如何通过指定列的值查找表行? [英] Zend Framework: How to find a table row by the value of a specified column?
本文介绍了Zend Framework:如何通过指定列的值查找表行?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在像快速入门一样实现我的模型指南.
在我的模型中,我试图实现一个 findByToken()
方法.当前的 find()
方法接受一个 $id
参数,但我想通过不同列的值进行查找.
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?
推荐答案
$result = $db->fetchAll($where);
或者如果您只想检索一行.
or if you are trying to retrieve only one row.
$result = $db->fetchRow($where);
您也可以使用 Zend_Db_Select 对象,使适配器更加抽象:
You could also use the Zend_Db_Select Object, keeping the adapter a little further abstracted:
$db = $this->getDbTable();
$select = $db->select()->where('token = ?', $token);
$result = $db->fetchAll($select);
这篇关于Zend Framework:如何通过指定列的值查找表行?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文