Zend Db查询选择所有ID

时间:2012-05-04 11:04:35

标签: zend-framework zend-db

如何编写Zend DB查询以从列ID中选择所有内容?

到目前为止,我已经尝试过:

public function getLatestUserID()
    {
        $ids = $this->select()
             ->where('id = ?');
        return $ids;
    }

但无济于事。

2 个答案:

答案 0 :(得分:1)

您只需要id列,
您无法调用执行命令。

尝试:

//assuming you are using a DbTable model
public function getLatestUserID()
    {
        $ids = $this->fetchAll('id');
        return $ids;
    }

我会这样做,因为我使用select()对象:

public function getLatestUserID()
    {
        $select = $this->select();
        //I'm not sure if $this will work in this contex but you can out the table name
        $select->from(array($this), array('id'));
        $ids = $this->fetchAll($select);
        return $ids;
    }

前两个示例应该只返回表格的id列,现在,如果您确实要查询特定的id

 public function getLatestUserID($id)
        {
            $select = $this->select();
            $select->where('id = ?', $id);
            //fetchAll() would still work here if we wanted multiple rows returned
            //but fetchRow() for one row and fetchRowset() for multiple rows are probably
            //more specific for this purpose.
            $ids = $this->fetchRow($select);
            return $ids;
        }

答案 1 :(得分:1)

确保包含getLatestUserID的类确实扩展了Zend_Db_Table_Abstract:

$ids = $this->select()->where('id = ?');无效,因为where('id =?');需要一个像where('id = ?', $id);

这样的id值

如果你想要的是最新插入的行的Id使用:

$lastInsertId = $this->getAdapter()->lastInsertId();

(但是如果你使用的是oracle数据库,这将无效,你应该使用$lastInsertId = $this->getAdapter()->lastSequenceId('USER_TABLE_SEQUENCE');