CodeIgniter Forums
Call to undefined function CAST() - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: CodeIgniter 4 (https://forum.codeigniter.com/forumdisplay.php?fid=28)
+--- Forum: CodeIgniter 4 Support (https://forum.codeigniter.com/forumdisplay.php?fid=30)
+--- Thread: Call to undefined function CAST() (/showthread.php?tid=73222)



Call to undefined function CAST() - Qiáo yī sī - 04-01-2019

Hi! Im having an error in using the cast function in my model. Does cast() works in codeigniter?

Here is my code.

   public function gen_report($personnel, $ym)
   {
       //print_r($personnel);exit();
       if($personnel != null){
           $query = $this->db->select('*')
               ->join('document_activity b', 'b.document_id = a.id')
               ->join('personnel c', 'c.id = b.receiver_personnel_id')
               ->join('document_type d', 'd.doctype_id = a.document_type_id')
                ->like('a.date_received', $ym)
                    ->CAST('a.tracking_no AS UNSIGNED')
                    ->order_by('date_received', 'ASC')
               ->where('b.receiver_personnel_id',$personnel)
               ->get('document a');
        }else{
            $query = $this->db->select('*')
            ->join('document_activity b', 'b.document_id = a.id')
            ->join('personnel c', 'c.id = b.receiver_personnel_id')
            ->join('document_type d', 'd.doctype_id = a.document_type_id')
            ->like('a.date_received', $ym)
                ->CAST('a.tracking_no AS UNSIGNED')
                ->order_by('date_received', 'ASC')
            ->get('document a');
        }
       
       if($query->num_rows() > 0){
           return $query->result();
       }else{
           return false;
       }
   }



RE: Call to undefined function CAST() - kilishan - 04-01-2019

Please provide code example of what you're trying so that we can help.


RE: Call to undefined function CAST() - Qiáo yī sī - 04-01-2019

    Here is my code 

public function gen_report($personnel, $ym)
   {
       //print_r($personnel);exit();
       if($personnel != null){
           $query = $this->db->select('*')
               ->join('document_activity b', 'b.document_id = a.id')
               ->join('personnel c', 'c.id = b.receiver_personnel_id')
               ->join('document_type d', 'd.doctype_id = a.document_type_id')
                ->like('a.date_received', $ym)
                    ->CAST('a.tracking_no AS UNSIGNED')
                    ->order_by('date_received', 'ASC')
               ->where('b.receiver_personnel_id',$personnel)
               ->get('document a');
        }else{
            $query = $this->db->select('*')
            ->join('document_activity b', 'b.document_id = a.id')
            ->join('personnel c', 'c.id = b.receiver_personnel_id')
            ->join('document_type d', 'd.doctype_id = a.document_type_id')
            ->like('a.date_received', $ym)
                ->CAST('a.tracking_no AS UNSIGNED')
                ->order_by('date_received', 'ASC')
            ->get('document a');
        }
       
       if($query->num_rows() > 0){
           return $query->result();
       }else{
           return false;
       }
   }



RE: Call to undefined function CAST() - ciadmin - 04-01-2019

I don't see a cast() method in CI's query builder.
It appears to be a verb in the supported databases, so perhaps it should be added to Database\BaseBuilder?


RE: Call to undefined function CAST() - kilishan - 04-02-2019

Yeah, CAST is not part of CodeIgniter's QueryBuilder, so of course it's not finding it. Did you see docs somewhere that made you think it would be there? If so, we need to revise.

That's one of MANY SQL commands that don't have a QueryBuilder equivalent, like CASE, etc. If you absolutely need to support that in the query you might have to do a straight $db->query() call (using bindings of course!), unless you can manage to make that part of a select or something which you don't escape, which you would need to be very careful with before using to make sure you didn't pass unescaped user data into.