2

我是ZF2的新手。我怎样才能写这样的查询?

SELECT * FROM users WHERE id = 1 AND status != 2

我的代码模型:

public function getUser($where = array())
    {
        $select = $this->sql->select();
        $select->from(self::TABLE);
        $select->where($where);
        $select->order('name ASC');
        return $statement->execute();
    }

我正在使用: Zend\Db\Sql

谢谢。

4

2 回答 2

1

看看这里的文档。所以$where也可以是stringor Closure

在您的情况下调用它,例如:

$user = $object->getUser('status != 2');

哦,我错过了第一个条件:

$user = $object->getUser(array('id = 1', 'status != 2'));

编辑:

您肯定可以保留= array()默认值。我不知道为什么,但我将它与类型提示混淆了。(array $where)

于 2013-10-11T09:49:49.480 回答
1
public function getUser( where = array() ) {
    $select = $this->sql->select();
    $select->from(self::TABLE);

    $select
       ->where->nest()
           ->equalTo( 'id' => where['id'] )
           ->or
           ->notEqualTo( 'status' => where['status'] )
       ->unnest();

    $select->order('name ASC');

    return $statement->execute();
}

像这样使用:

getUser( array(
    'id'     => 1,
    'where'  => 2,
) );
于 2013-10-12T16:52:14.430 回答