2013-01-04 70 views
4

如何將SQL_CALC_FOUND_ROWSZend\Db\TableGateway一起使用,而不對原始SQL使用直接低級查詢?如何在Zend Db TableGateway中使用SQL_CALC_FOUND_ROWS

class ProductTable { 
    protected $tableGateway; 

    /** 
    * Set database gateway 
    * 
    * @param TableGateway $tableGateway - database connection 
    * @return void 
    */ 
    public function __construct(TableGateway $tableGateway) { 
     $this->tableGateway = $tableGateway; 
    } 


    /** 
    * Fetch all products 
    * 
    * @param integer $page - page of records 
    * @param integer $perpage - records per page 
    * @return void 
    */ 
    public function fetchAll($page = 1, $perpage = 18) { 
     return $this->tableGateway->select(function (Select $select) use ($page, $perpage) { 
      $select 
       ->limit($perpage) 
       ->offset(($page - 1) * $perpage); 
     }); 
    } 
} 

我希望獲得在fetchAll中使用的同一查詢中的記錄總數。

回答

6

看起來像Zend Framework 2.1.4支持指定量詞。這使您可以在select對象中使用SQL_CALC_FOUND_ROWS。我遇到的一件事情很棘手,就是如果你沒有指定一個表,Zend的Zend \ Db \ Sql \ Select類將不會爲你生成正確的SQL。當執行後續選擇來檢索FOUND_ROWS()時,這變成並且問題。我已經更新了您的代碼,以包含我將要使用的內容。我將我的項目實現合併到你的代碼中,所以如果有什麼不起作用,可能是因爲我輸錯了一些東西,但總體來說它對我很有用(不像我想要的那樣合意)。

use Zend\Db\Sql\Expression; 
use Zend\Db\Sql\Select; 

class ProductTable { 
protected $tableGateway; 

/** 
* Set database gateway 
* 
* @param TableGateway $tableGateway - database connection 
* @return void 
*/ 
public function __construct(TableGateway $tableGateway) { 
    $this->tableGateway = $tableGateway; 
} 


/** 
* Fetch all products 
* 
* @param integer $page - page of records 
* @param integer $perpage - records per page 
* @return void 
*/ 
public function fetchAll($page = 1, $perpage = 18) { 
    $result = $this->tableGateway->select(function (Select $select) use ($page, $perpage) { 
     $select 
      ->quantifier(new Expression('SQL_CALC_FOUND_ROWS')) 
      ->limit($perpage) 
      ->offset(($page - 1) * $perpage); 
    }); 

    /* retrieve the sql object from the table gateway */ 
    $sql = $this->tableGateway->getSql(); 

    /* create an empty select statement passing in some random non-empty string as the table. need this because Zend select statement will 
    generate an empty SQL if the table is empty. */ 
    $select = new Select(' '); 

    /* update the select statement specification so that we don't incorporate the FROM clause */ 
    $select->setSpecification(Select::SELECT, array(
     'SELECT %1$s' => array(
      array(1 => '%1$s', 2 => '%1$s AS %2$s', 'combinedby' => ', '), 
      null 
     ) 
    )); 

    /* specify the column */ 
    $select->columns(array(
     'total' => new Expression("FOUND_ROWS()") 
    )); 

    /* execute the select and extract the total */ 
    $statement = $sql->prepareStatementForSqlObject($select); 
    $result2 = $statement->execute(); 
    $row = $result2->current(); 
    $total = $row['total']'; 

    /* TODO: need to do something with the total? */ 

    return $result; 
} 

}

+0

Zend的\ DB \ SQL \選擇不表。良好的解決方法!謝謝! :) Zend \ Db \ Adapter \ Exception \ InvalidQueryException「語句無法執行」 – wormhit

+0

您能輸出您生成的查詢嗎? – xangxiong

+0

不,不。一切都是正確的。代碼正在創建有效的查詢。我剛剛發佈了一個例外,它將我帶入瞭解決方案。 – wormhit

相關問題