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

时间:2018-03-23 06:46:15

标签: codeigniter

我想根据他们的名字获取user_ID,并将其插入到不同的表中。这是我的控制者:

    $customer = strtoupper($this->input->post('cust_name'));
    $address = $this->input->post('address');
    $category = $this->input->post('category');

    $sales = ($this->input->post('sic'));              
    $salesID = $this->user_model->getUserName($sales);       

    $result = $this->db->insert('customer', [        
        'cust_name' => $customer,
        'cust_address' => $address,
        'category' => $category,
        'user_id' => $salesID
    ]);

我的模特:

public function getUserName($sales) {
    $this->db->select("user_id");
    $query = $this->db->get_where('user', ['name' => $sales]);
    return $query->row();
}

当我执行查询时,它总是说"类stdClass的对象无法转换为字符串"

这里是var_dump:

  

object(stdClass)#22(1){[" user_id"] =>字符串(2)" 15" }

的print_r:

  

stdClass对象([user_id] => 15)

如何解决这个问题?谢谢

1 个答案:

答案 0 :(得分:1)

希望这会对您有所帮助:

使用row这样的对象:

  $salesID = $this->user_model->getUserName($sales);       

   $result = $this->db->insert('customer', [        
    'cust_name' => $customer,
    'cust_address' => $address,
    'category' => $category,
    'user_id' => $salesID->user_id  /* here is the change*/
  ]);

替代方式:像这样返回user_id

    public function getUserName($sales) {
       $this->db->select("user_id");
       $query = $this->db->get_where('user', ['name' => $sales]);
       return $query->row()->user_id;
    }
   /*then use it as you are using currently*/