Zend Framework: how to find a table row by the value of a specified column?

I implement my model in the same way as the quick start guide .

In my model, I am trying to implement the findByToken() method. The current find() method takes the $id parameter, but I want to find the value of another 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 to do something similar, but I don't think it worked:

 $db = $this->getDbTable(); $where = $db->getAdapter()->quoteInto('token = ?', $token); $result = $db->find($where); 

How can I find a row by the value of the specified column?

+4
source share
2 answers
 $result = $db->fetchAll($where); 

or if you are trying to get only one row.

 $result = $db->fetchRow($where); 

You can also use the Zend_Db_Select object while holding the adapter a little further:

 $db = $this->getDbTable(); $select = $db->select()->where('token = ?', $token); $result = $db->fetchAll($select); 
+8
source

This is easy to do by creating a select object and retrieving a string using this object. This is well described in the manual: http://framework.zend.com/manual/en/zend.db.select.html#zend.db.select.execute

Your code might look like this:

 $select = $this->getDbTable()->select()->where('token = ?', (string) $token); $row = $this->getDbTable()->fetchRow($select); 
+4
source

All Articles