ZF2在模型中使用OR

时间:2014-04-25 09:59:36

标签: php mysql zend-framework2

所以我正在尝试实现这个查询:

SELECT `userMessages`.*, `users`.`username` AS `username` FROM `userMessages`
INNER JOIN `users` ON `users`.`id` = `userMessages`.`FROMuID` WHERE
userMessages.FROMuID = 41 AND userMessages.TOuID = 40 
OR userMessages.FROMuID = 40 AND userMessages.TOuID = 41 ORDER BY `userMessages`.`messageTime` DESC

这是我的模特

public function getUserMessages($uID,$fromUID,$paginated = true){
    if($paginated) {
        // create a new Select object for the table ads
        $select = new Select($this->tableGateway->table);
        $select->join('users','users.id = userMessages.FROMuID',array('username'),'inner');
        $select->where("userMessages.FROMuID = $fromUID");
        $select->where("userMessages.TOuID = $uID");

        $select->order('userMessages.messageTime DESC');
        // create a new result set based on the Ads entity
        $resultSetPrototype = new ResultSet();
        // create a new pagination adapter object
        $paginatorAdapter = new DbSelect(
            // our configured select object
            $select,
            // the adapter to run it against
            $this->tableGateway->getAdapter()
            //,
            // the result set to hydrate
            //$resultSetPrototype
            );
        $paginator = new Paginator($paginatorAdapter);
        return $paginator;
    }
    $resultSet = $this->tableGateway->select();
    return $resultSet;
}

你知道我怎么能做到这一点?我想我需要在我的where语句\Zend\Db\Sql\Predicate\中使用,但我不知道如何。

1 个答案:

答案 0 :(得分:0)

通过添加以下行解决:

        $select->where
            ->equalTo("userMessages.FROMuID",$fromUID)
            ->equalTo("userMessages.TOuID",$uID)
        ->or
            ->equalTo('userMessages.FROMuID', $uID)
            ->equalTo('userMessages.TOuID', $fromUID);