簡體   English   中英

Zend Framework 2 TableGateway返回空結果集

[英]Zend Framework 2 TableGateway returning empty resultset

我剛剛開始使用Zend Framework 2進行開發,但是遇到了障礙。

用最簡單的表達式,fetchAll函數起作用:

public function fetchAll()
{
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}

但是,當我嘗試通過以下方式混合聯接時:

public function fetchAll()
{
    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from('Entreprise')
        ->columns(array('id', 'nom', 'categorie_id'))
        ->join(array('C' => 'Categorie'), 'categorie_id = C.id', array('categorie' => 'nom'), \Zend\Db\Sql\Select::JOIN_INNER);
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;
}

結果查詢為:

SELECT "Entreprise"."id" AS "id", "Entreprise"."nom" AS "nom", "Entreprise"."categorie_id" AS "categorie_id", "C"."nom" AS "categorie" FROM "Entreprise" INNER JOIN "Categorie" AS "C" ON "categorie_id" = "C"."id"

表名,列很好,因為查詢不會引發錯誤,而是只返回一個空結果集。 即使刪除聯接並僅留下以下代碼也無濟於事。

public function fetchAll()
{
    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from('Entreprise');
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;
}

這使我相信,獲取適配器或實例化選擇的方式完全是錯誤的,但是我似乎無法弄清楚它或找到任何解決方案。

誰能幫我弄清楚我在做什么錯?

以下代碼可以完美運行:

public function fetchAll()
{
    $sql = new Sql($this->tableGateway->getAdapter());

    $select = $sql->select();
    $select->from('Entreprise')
        ->columns(array('id', 'nom', 'categorie_id'))
        ->join(array('C' => 'Categorie'), 'categorie_id = C.id', array('categorie' => 'nom'), \Zend\Db\Sql\Select::JOIN_INNER);
    $resultSet = $this->tableGateway->selectWith($select);
    return $resultSet;
}

...這是另一個文件中偶然輸入的錯字...

感謝Sam的帶領,這很簡單,但我只是沒有想到嘗試!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM