Codeigniter do not select the data whose date is less than 2019 which have some types from joined table

308 Views Asked by At

I'm working on Codeigniter rest API project and came across a problem. I would like my model not to select the data whose registration date is less than 01/01/2019 and whose types in the joined table equal type_1 type_2, but if the registration date is less than 2019 and the type in the joined table equals type_3, type_4 the request must return the record .

so i tried this method how can i get this result?

code: // MyModel

function get_data() {
    
    
    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';

    if ($id != null) {
        $this->db->where('ID',$id);
        $result =  $this->db->select('POST.ID,POST.DATE,'
        . ' TYPE.Post_Type P_TYPE,)
        ->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left')
        ->order_by('POST.ID', 'DESC')
        ->where_in('POST.Post_Type  = type_1, type_2')
        ->where('POST.DATE<', "to_date('01/01/19', 'DD/MM/RR')", FALSE)
        ->get('POST');
    } 
    return $result;
}

So i want that its not returning data with type_1 type_2 which are <than 2019, but can get others type

1

There are 1 best solutions below

8
On BEST ANSWER

First, why did you filter in where post.date less than if you want greater than that date?. Second, where_in does accept values as an array on the second parameter. well here is my code, I reorganize your code to a much readable one. If you are trying to query date in timestamp or date field it should be in format of Y-m-d

function get_data() {
    
    
    $id = isset($_REQUEST['id']) ? $_REQUEST['id'] : '';
    
    $types = array('type_1', 'type_2');
    if ($id != null) {
        $this->db->where('ID',$id);
        $this->db->select('POST.ID, POST.DATE, TYPE.Post_Type as P_TYPE,', FALSE);
        $this->db->join('TYPE', 'POST.TYPE_ID = TYPE.ID', 'left');
        $this->db->order_by('POST.ID', 'DESC');
        $this->db->where_in('POST.Post_Type', $types);
        $this->db->where('POST.DATE >', date('Y-m-d', strtotime('01-01-2019')) );
        $this->db->from('POST');

        $result = $this->db->get();
    } 
    return $result;
}