CakePHP find condition for a query between two dates

前端 未结 6 1173
谎友^
谎友^ 2020-12-30 08:59

I have a start and an end date in my database and a $date variable from a form field. I am now trying to query all the rows where $date is either = start/end date in the db,

6条回答
  •  感动是毒
    2020-12-30 09:50

    General Example For CakePHP 2.x Query

            $_condition = array("TABLENAME.id" => $id, "TABLENAME.user_id" => array_unique($_array), 'date(TABLENAME.created_at) BETWEEN ? AND ?' => array($start_date, $end_date));
            $result_array = $this->TABLENAME->find("all", array(
                'fields' => array("TABLENAME.id", "TABLENAME.user_id", 'TABLENAME.created_at'),
                "conditions" => $_condition,
                "group" => array("TABLENAME.id"), //fields to GROUP BY
                'joins' => array(
                    array(
                        'alias' => 'T2',
                        'table' => 'TABLENAME2',
                        'type' => 'LEFT',
                        'conditions' => array('TABLENAME.t_id = TABLENAME2.t_id')
                    ),
                    array(
                        'alias' => 'T3',
                        'table' => 'TABLENAME3',
                        'type' => 'LEFT',
                        'conditions' => array(
                            'IF(
                               TABLENAME.t3_id > 0,
                                    T2.f_id = T3.f_id,
                                    TABLENAME.ff_id = T2.ff_id
                                )'
                        )
                    ),
                ),
                'recursive' => 0
                    )
            );
    

提交回复
热议问题