1

Zend_Db_Table_Abstract を正しく使用する方法を理解しようとしています。nameクエリから列だけを返したい。次のコードの何が問題なのか説明していただけますか?

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());
  }
}

アップデート:

以下の @Joshua Smith の例から、select() を使用してこれを正しく行う方法を理解できました。

$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());
4

1 に答える 1

3

あなたのコードはほとんど機能しています:

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 特定の列の選択については例 #25 を参照し、find の使用方法の詳細については「主キーによる行の検索」を参照してください。

于 2010-03-12T04:01:50.590 に答える