0
votes

I am trying to store a array into database(SQL Server), in that array I am getting some values from UI, and some values fetched from Model. Here my problem is with second one only... The values coming from UI is getting inserted, but the values coming from model are in object notation.... It can't store into a single variable...so that the data is not even store into database... and it raises the error

Object of class stdClass could not be converted to string

Here is my code...

Model

public function getCustcode($customer)
{
 return $query = $MainDB->query("SELECT custcode from CRM_CustomerEvents_View  where custname = '".$customer."'")->row();
}

Controller:

public function addEvent()
    {        
        $name               = $this->input->post("name",TRUE);
        $start_date         = $this->input->post("start_date", TRUE);
        $end_date           = $this->input->post("end_date", TRUE);
        $customer           = $this->input->post("customer",TRUE);
        $custcode           = $this->Calendarmodel->getCustcode($customer);

        $tranno             = $this->input->post("transno",TRUE);
        $leadid             = $this->Calendarmodel->getLeadID($tranno);
}



$data = array(
            "title"       => $name,                          
            "start_event" => $start_date,                 
            "end_event"   => $end_date,                   
            "custcode"    => $custcode,   
            "Leadid"      => $leadid,   
             );

It raises the error

Object of class stdClass could not be converted to string

then I changed like this

$data = array(
            "title"       => $name,                          
            "start_event" => $start_date,                  
            "end_event"   => $end_date,
           "custcode"    => $custcode[0]['custcode'],     //'100287',//
            "Leadid"      => $leadid[0]['Leadid']         //'10070'//
             );

It generates the error:

Cannot use object of type stdClass as array

If I use json_encode($custcode) in static way(to test) and it gives me the data as:

"{"custcode":"100287"}" 

Please, can anyone give any solution to handle this object data coming from Model...

1
Print the result of var_dump($custcode); - Giacomo M
It gives me the output as follows C:\wamp\www\CRM_Dev5\application\controllers\Calendar.php:163: object(stdClass)[20] public 'custcode' => string '100287' (length=6) C:\wamp\www\CRM_Dev5\application\controllers\Calendar.php:164: object(stdClass)[21] public 'Leadid' => string '10070' (length=5) An Error Was Encountered Unable to load the requested file: Calendar_view.php - Sowmya Gayam
var_dump() is not worked for me sir... Can you give me any alternative solution? - Sowmya Gayam
I want to store id of a field in database...But I have to show customer name in UI - Sowmya Gayam
Did you solved your problem? - Aksen P

1 Answers

0
votes

In your case you need to convert stdObject into array by simply adding (array) before stdObject.

It should looks like:

$custcode = (array)$this->Calendarmodel->getCustcode($customer); 
$leadid   = (array)$this->Calendarmodel->getLeadID($tranno);

And use your second $data(try $custcode['custcode'] and $leadid['Leadid']). Should help.