mysql - Zend 框架 2 : LEFT JOIN issue

标签 mysql join zend-framework2

public function getInterests($userID) {
    $result = $this->tableGateway->select(function (Select $select) use ($userID) {
                $select->join('interests', 'users_interests.interest_id = interests.interest_id', array('*'), 'left');
                $where = new Where();
                $where->equalTo('user_id', $userID);
                $select->where($where);
            });

    return $result;
}

这是我的方法。它只是从 users_interests 中选择 user_id = $userID 的所有记录并加入 'interests' 表。到目前为止一切顺利,但是当尝试显示获取的结果时,连接表中的字段根本不存在。这是 $result 的转储:

Zend\Db\ResultSet\ResultSet Object
(
[allowedReturnTypes:protected] => Array
    (
        [0] => arrayobject
        [1] => array
    )

[arrayObjectPrototype:protected] => Object\Model\UsersInterests Object
    (
        [settings_id] => 
        [user_id] => 
        [interest_id] => 
    )

[returnType:protected] => arrayobject
[buffer:protected] => 
[count:protected] => 2
[dataSource:protected] => Zend\Db\Adapter\Driver\Pdo\Result Object
    (
        [statementMode:protected] => forward
        [resource:protected] => PDOStatement Object
            (
                [queryString] => SELECT `users_interests`.*, `interests`.* FROM `users_interests` LEFT JOIN `interests` ON `users_interests`.`interest_id` = `interests`.`interest_id` WHERE `user_id` = :where1
            )

        [options:protected] => 
        [currentComplete:protected] => 
        [currentData:protected] => 
        [position:protected] => -1
        [generatedValue:protected] => 0
        [rowCount:protected] => 2
    )

[fieldCount:protected] => 6
[position:protected] => 
)

我非常需要这方面的帮助,因为我应该在周日之前完成我的项目。提前致谢。

最佳答案

您可以使用以下内容来应用左连接。 $select::JOIN_LEFT 而不是 'left'。

public function getInterests($userID) {
    $result = $this->tableGateway->select(function (Select $select) use ($userID) {
        $select->join('interests', 'users_interests.interest_id = interests.interest_id', array('*'), $select::JOIN_LEFT);
        $where = new Where();
        $where->equalTo('user_id', $userID);
        $select->where($where);
    });

    return $result;
}

关于mysql - Zend 框架 2 : LEFT JOIN issue,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15231464/

相关文章:

mysql - 不用 GroupConcat 将多行合并为一

php - 使用实体管理器从表中选择列的最大值的最简单、最简单的方法是什么?

php - 如何在 zf2 的 Controller 中执行查询

php - 数据库php类导致 fatal error

mysql - 如何从mysql中的两个表中获取数据?

php - 如何通过电子邮件过滤从mySQL获取数据?

用于计算权重的 MySQL 查询

MySQL内连接查询多表

SQL - 如果加入期间日期不匹配 - 更改日期然后加入

session - ZF2 session 验证器