【问题标题】:Zend SQL - conditional where clauseZend SQL - 条件 where 子句
【发布时间】:2013-12-16 13:34:56
【问题描述】:

我有以下 Zend SQL 查询:

$select = $this->select()
->where($this->_quote('VerDate <= ?', $todate))
->where($this->_quote('VerFieldId = ?', $fieldid));
if (count($records) > 0) {
    $this->select()->where($this->_quote('VerRecordId IN (?)', $records));
}
$this->select()->group('VerRecordId');

如果 $records 数组已传递给函数,我只想在 VerRecordId 上添加 where 条件。

当我运行查询时,它在 VerFieldId 的 where 子句之后停止。

谁能帮助我修改查询需要做什么?

更新:

我已经使用以下代码解决了这个问题。但是,如果有人知道这是否可以更好地构建,请告诉我。

$where = array();
$where['VerDate <= ?'] = $todate;
$where['VerFieldId = ?'] = $fieldid;
if (count($records) > 0) {
    $where['VerRecordId IN (?)'] = $records;
}
$select = $this->select();
$this->_where($select,$where);
$select->group('VerRecordId');

这允许我有条件地添加 VerRecordId 条件。

【问题讨论】:

    标签: php mysql sql zend-framework


    【解决方案1】:

    ZF2

     $sql = new Sql($this->adapter);
              $select = new Select($this->table);
              $where = new  \Zend\Db\Sql\Where();
              $where->equalTo('field1',(string) $value1);
              $where->greaterThan('field2','value2') ;
              $where->equalTo('field3','value3');
              $select->where($where);
              $statement = $sql->prepareStatementForSqlObject($select);
              $result = $statement->execute();
    

    ZF1

    $select = $this->select();      
    $select->setIntegrityCheck(false) // if you have joins
                ->from(array('tablename' => 'tablename',))
                $select->where('field = ?', $value);
    $select->where(new Zend_Db_Expr("field <> 'value'"));   
            $result = $this->fetchAll($select); 
    

    然后你可以循环 $result

    希望对你有帮助

    http://framework.zend.com/manual/2.2/en/modules/zend.db.sql.html

    【讨论】:

    • 您好 Amarjit,感谢您的回复。我忘了说我使用的是 Zend Framework 版本 1.11.11,我不确定你可以使用上面描述的符号吗?
    • 我也为 zf1 添加了
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-05-28
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 1970-01-01
    • 2013-09-08
    • 2014-11-01
    相关资源
    最近更新 更多