douqi2571 2015-06-17 00:48 采纳率: 0%
浏览 30
已采纳

too long

Info

I have an application where I want to query a table to be able to get out the record with the smallest id. To simplify my understanding I composed two queries in two table methods.

I am able to query the database with a hard coded scalar but can not query based on the queried scalar from another query.

Question

How do I get a scalar value extracted from from a query in Zend Framework 2 and use the value as an input to the where clause of another (main) query?

Code

   public function getMinActionItemID()
    {
        $select = new Select();
        $select->from('actionitems', array('MinActionItemID'))->columns(array('MinActionItemID' => new Expression('min(ActionItemID)')));
        $resultSet = $this->tableGateway->selectWith($select);
        $resultSet->buffer();
        return $resultSet;
    }

public function getFirstActionItem()
{       
    var_dump($this->getMinActionItemID());
    $select = new Select();
    $select->from('actionitems')->where(array('ActionItemID' => $this->getMinActionItemID()));
    $row = $this->tableGateway->selectWith($select);        

    if (!$row) {
        throw new Exception("Action Item Not Found");
    }

    return $row;

}

I am able to get the following from the query but am unable to get out the scalar value from the object below which is returned from getFirstActionItemID() and pass into the where clause of the getFirstActionItem() method.

  object(Zend\Db\ResultSet\ResultSet)[288]
  protected 'count' => int 1
  protected 'dataSource' => 
    object(Zend\Db\Adapter\Driver\Pdo\Result)[266]
      protected 'statementMode' => string 'forward' (length=7)
      protected 'fetchMode' => int 2
      protected 'resource' => 
        object(PDOStatement)[267]
          public 'queryString' => string 'SELECT min(ActionItemID) AS `MinActionItemID` FROM `actionitems`' (length=64)
      protected 'options' => null
      protected 'currentComplete' => boolean true
      protected 'currentData' => 
        array (size=1)
          'MinActionItemID' => string '1' (length=1)
      protected 'position' => int 0
      protected 'generatedValue' => string '0' (length=1)
      protected 'rowCount' => int 1
  protected 'fieldCount' => int 1
  protected 'position' => int 0

EDIT

I ended up abandoning my approach and used the answers provided. I also preserved the getMinActionItemID() so that I would use it for boundary checking when I navigate to the first and last record and try to go next or previous in my application.

With some investigation through the intellisense, I finally found a way to get out the desired scalar. In hindsight I found both the answer and this code to solve my problem of navigation (one record at a time).

How To Get Scalar From Query

Answer

use Zend\Db\Adapter\Driver\Pdo\Result;

/* @var $resultSet ResultSet */
    /* @var $dataSource Result */
    public function getMinActionItemID()
    {
        $select = new Select();
        $select->from('actionitems', array('MinActionItemID'))->columns(array('MinActionItemID' => new Expression('min(ActionItemID)')));
        $resultSet = $this->tableGateway->selectWith($select);
        $dataSource = $resultSet->getDataSource();
        return $dataSource->getResource()->fetchAll()[0]['MinActionItemID'];
    }

Usage

public function getNextActionItem($id)
{

    if ($id == $this->getMaxActionItemID())
    {
        return $this->getActionItem($id);
    }

    $select = new Select();
    $select->from('actionitems')
           ->where("ActionItemID > $id")
           ->order('ActionItemID')
           ->limit(1);
    $row = $this->tableGateway->selectWith($select);

    if (!$row) {
        throw new Exception ("Action Item Not Found");
    }

    return $row;
}
  • 写回答

3条回答 默认 最新

  • dongyuntao2000 2015-06-30 15:44
    关注

    How to Extract Scalar From an Arbitrary Query from $resultSet in two steps.

     $dataSource = $resultSet->getDataSource();
     $minActionItemID =  $dataSource->getResource()->fetchAll()[0]['MinActionItemID'];  //Note: [0] is the first record and ['MinActionItemID'] is the field name of the custom query
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥20 Java-Oj-桌布的计算
  • ¥15 请问如何在openpcdet上对KITTI数据集的测试集进行结果评估?
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路