views:

29

answers:

1

I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. I want to return just the name column from my query. Can you please explain what's wrong with the following code?

class Model_DbTable_Foo extends Zend_Db_Table_Abstract
{
  protected $_name = 'foo';

  public function getFooById($id) {
    $select = $this->select(true)->columns('name')->where('id=' . $id);
    $row    = $this->fetchRow($select);
    print_r($row->toArray());
  }
}

Update:

From the example by @Joshua Smith below, I was able to figure out how to use select() to do this correctly:

$select = $this->select()
  ->from($this->_name, 'name') // The 2nd param here could be an array.
  ->where('id = ?', $id);
$row = $this->fetchRow($select);
print_r($row->toArray());
+3  A: 

Your code is very close to working:

class Model_DbTable_Foo extends Zend_Db_Table_Abstract
{
  protected $_name = 'foo';

  public function getFooById($id) {
    $row = $this->find($id)->current();
    return $row->name;
  }
}

http://framework.zend.com/manual/en/zend.db.table.html see example #25 for specific column selection and 'Finding Rows by Primary Key' for more about using find.

Joshua Smith
Thanks! With example #25 I was able to figure out how select() works. I'll post my code above.
jwhat