【问题标题】:Object of class stdClass could not be converted to string in Codeigniter3类 stdClass 的对象无法在 Codeigniter 3 中转换为字符串
【发布时间】:2019-10-24 06:49:24
【问题描述】:

我正在尝试将数组存储到数据库(SQL Server)中, 在那个数组中,我从 UI 中获取了一些值, 以及从模型中获取的一些值。 在这里,我的问题仅在于第二个...来自 UI 的值正在被插入, 但是来自模型的值是对象表示法.... 它不能存储到单个变量中......因此数据甚至没有存储到数据库中...... 它引发了错误

stdClass 类的对象无法转换为字符串

这是我的代码...

型号

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,   
             );

它会引发错误

stdClass 类的对象无法转换为字符串

然后我就这样改了

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

它会产生错误:

不能使用 stdClass 类型的对象作为数组

如果我以静态方式(测试)使用json_encode($custcode),它给我的数据如下:

"{"custcode":"100287"}" 

请,任何人都可以提供任何解决方案来处理来自模型的对象数据...

【问题讨论】:

  • 打印var_dump($custcode);的结果
  • 它给我的输出如下 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) 错误是遇到无法加载请求的文件:Calendar_view.php
  • var_dump() 不适用于我先生...你能给我任何替代解决方案吗?
  • 我想在数据库中存储一个字段的 id...但是我必须在 UI 中显示客户名称
  • 你的问题解决了吗?

标签: sql-server codeigniter codeigniter-3


【解决方案1】:

在您的情况下,您需要将stdObject 转换为array,只需在stdObject 之前添加(array)

它应该看起来像:

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

然后使用第二个$data(试试$custcode['custcode']$leadid['Leadid'])。应该有帮助。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2021-02-21
    • 2013-12-13
    • 2011-04-06
    • 2018-10-19
    相关资源
    最近更新 更多