简体   繁体   English

Zend_Db_Table_Select如何工作?

[英]How does Zend_Db_Table_Select work?

I'm trying to figure out how to use Zend_Db_Table_Abstract correctly. 我试图弄清楚如何正确使用Zend_Db_Table_Abstract。 I want to return just the name column from my query. 我只想从查询中返回name列。 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: 从下面@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());

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. http://framework.zend.com/manual/en/zend.db.table.html有关特定列选择的信息,请参见示例#25;有关使用find的更多信息,请参见示例“通过主键查找行”。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM