class stdClass 的对象无法在 Codeigniter 3 中转换为字符串
Object of class stdClass could not be converted to string in Codeigniter3
我正在尝试将数组存储到数据库(SQL 服务器),
在那个数组中,我从 UI 得到了一些值,
以及从模型中获取的一些值。
我的问题仅在于第二个......来自 UI 的值正在插入,
但是来自模型的值是对象表示法....
它不能存储到单个变量中……因此数据甚至不能存储到数据库中……
并引发错误
Object of class stdClass could not be converted to string
这是我的代码...
型号
public function getCustcode($customer)
{
return $query = $MainDB->query("SELECT custcode from CRM_CustomerEvents_View where custname = '".$customer."'")->row();
}
控制器:
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,
);
它引发错误
Object of class stdClass could not be converted to string
然后我就这样改了
$data = array(
"title" => $name,
"start_event" => $start_date,
"end_event" => $end_date,
"custcode" => $custcode[0]['custcode'], //'100287',//
"Leadid" => $leadid[0]['Leadid'] //'10070'//
);
它产生错误:
Cannot use object of type stdClass as array
如果我以静态方式使用 json_encode($custcode)
(进行测试),它给我的数据为:
"{"custcode":"100287"}"
拜托,任何人都可以提供任何解决方案来处理来自模型的对象数据...
在您的情况下,您只需在 stdObject
.
之前添加 (array)
即可将 stdObject
转换为 array
它应该看起来像:
$custcode = (array)$this->Calendarmodel->getCustcode($customer);
$leadid = (array)$this->Calendarmodel->getLeadID($tranno);
并使用第二个 $data
(尝试 $custcode['custcode']
和 $leadid['Leadid']
)。应该有帮助
我正在尝试将数组存储到数据库(SQL 服务器), 在那个数组中,我从 UI 得到了一些值, 以及从模型中获取的一些值。 我的问题仅在于第二个......来自 UI 的值正在插入, 但是来自模型的值是对象表示法.... 它不能存储到单个变量中……因此数据甚至不能存储到数据库中…… 并引发错误
Object of class stdClass could not be converted to string
这是我的代码...
型号
public function getCustcode($customer)
{
return $query = $MainDB->query("SELECT custcode from CRM_CustomerEvents_View where custname = '".$customer."'")->row();
}
控制器:
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,
);
它引发错误
Object of class stdClass could not be converted to string
然后我就这样改了
$data = array(
"title" => $name,
"start_event" => $start_date,
"end_event" => $end_date,
"custcode" => $custcode[0]['custcode'], //'100287',//
"Leadid" => $leadid[0]['Leadid'] //'10070'//
);
它产生错误:
Cannot use object of type stdClass as array
如果我以静态方式使用 json_encode($custcode)
(进行测试),它给我的数据为:
"{"custcode":"100287"}"
拜托,任何人都可以提供任何解决方案来处理来自模型的对象数据...
在您的情况下,您只需在 stdObject
.
(array)
即可将 stdObject
转换为 array
它应该看起来像:
$custcode = (array)$this->Calendarmodel->getCustcode($customer);
$leadid = (array)$this->Calendarmodel->getLeadID($tranno);
并使用第二个 $data
(尝试 $custcode['custcode']
和 $leadid['Leadid']
)。应该有帮助