php – Zend Framework 2 TableGateway返回空结果集

我刚刚开始使用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的领导,可笑的简单,但我只是没想到尝试!

点赞