CodeIgniter db查询无法按预期工作

时间:2014-02-28 09:24:32

标签: php codeigniter yii

我正在尝试将参数从Yii MVC传递到CodeIgniter模型方法,并在时间间隔之间搜索结果;

created属性的值为26/2/2014;

query是否会使用where条款执行并连接关于字段created的两个get_where子句?

或者它只会执行get_where子句?

我看不到结果,但正在传递created值;

created字段在timestamp中存储为db值,我使用date picker选择day/month/year,然后传递值通过api,然后我创建24 hour时间间隔

这就是我被卡住的地方;

ps:使用EActiveResource代替ActiveRecord并通过使用api

构建的codeIgniter传递数据时,这是一个很好的示例
public function findAll($params) {
        if (!empty($params)) {
            if (isset($params['created'])) {
                $e = explode('/', $params['created']);
                $month = $e[1];
                $day = $e[0];
                $year = $e[2];
                $start = mktime(0, 0, 0, $month, $day, $year);
                $end = mktime(23, 59, 59, $month, $day, $year);
                $this->db->where('created >', $start);
                $this->db->where('created <', $end);
            }
            $query = $this->db->get_where($this->_table, $params);
        } else {
            $query = $this->db->get($this->_table);
        }
        $ret = $query->result_array();
        foreach ($ret as $key => $value) {
            $ret[$key]['order_id'] = $value['id'];
        }
        return (isset($ret) && !empty($ret)) ? $ret : false;
    }

我尝试在$params['created']中设置yii的值并将其传递给codeIgniter,以确保我通过if;

codeIgniter模型中我有:

    if (!empty($params)) {
        if (isset($params['created'])) {
            $this->db->where('created >=', 1393246473);
            $this->db->where('created <=', 1393404935);
        }
        $query = $this->db->get_where($this->_table, $params);
    } else {
        $query = $this->db->get($this->_table);
    }
    $ret = $query->result_array();

结果仍然没有数据;

任何想法?

1 个答案:

答案 0 :(得分:0)

试试这个

public function findAll($params) {
            if (!empty($params)) {
                if (isset($params['created'])) {
                    $e = explode('/', $params['created']);
                    $month = $e[1];
                    $day = $e[0];
                    $year = $e[2];
                    $start = mktime(0, 0, 0, $month, $day, $year);
                    $end = mktime(23, 59, 59, $month, $day, $year);
                    $this->db->where(array('created >'=> $start, 'created <'=> $end));

                }
                $query = $this->db->get($this->_table);
            } else {
                $query = $this->db->get($this->_table);
            }
            $ret = $query->result_array();
            foreach ($ret as $key => $value) {
                $ret[$key]['order_id'] = $value['id'];
            }
            return (isset($ret) && !empty($ret)) ? $ret : false;
        }

如果你想使用get_where然后传递数组中的所有条件

$this->db->get_where('mytable', array('id' => $id, 'created' => $cre), $limit, $offset);

你也可以编写一个sql查询,如: -

$sql = "SELECT * FROM example WHERE payment_status = '0' AND intial_pay_status = '1'";

        $query = $this->db->query($sql);
        if ($query->num_rows() > 0) {
          foreach ($query->result() as $row) {
            //echo '<pre>'; print_r($row); 
                  }
                }