【问题标题】:Clauses OR Where in Zend framework 2Zend 框架 2 中的子句 OR Where
【发布时间】:2013-10-11 09:43:15
【问题描述】:

我是 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

谢谢。

【问题讨论】:

    标签: zend-framework2 zend-db


    【解决方案1】:

    查看文档here。 所以$where 也可以是stringClosure

    在你的情况下这样称呼它:

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

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

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

    编辑:

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

    【讨论】:

    【解决方案2】:
    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,
    ) );
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2023-03-15
      • 2020-11-28
      • 1970-01-01
      • 2015-11-30
      • 1970-01-01
      • 2011-12-31
      • 2018-01-08
      相关资源
      最近更新 更多