如何将数组对象从模型传递到另一个控制器?

问题描述 投票:0回答:2

这是一个菜鸟问题,但我搜索了很多但没有运气。

我正在将对象数组从模型返回到控制器,并且必须将其传递给另一个模型。我必须转换它,但是如何转换?

这是我的控制器:

        $data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
        $data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
        $data['bookings']=$this->Myaccount_customer->getBookings($cid);

型号:

public function getBookings($cid)

    {
      $this->db->where('cid', $cid);    
      $this->db->from('bookings');
      $query = $this->db->get();
      return $query->result();
    }   

我想从一个模型获取数据,并用它从另一个模型获取一些其他数据。

我尝试传递 $data['cid'] 但收到此错误

    A PHP Error was encountered

Severity: Notice

Message: Array to string conversion

Filename: database/DB_query_builder.php

Line Number: 662

Backtrace:

File: C:\xampp\htdocs\style\application\models\Myaccount_customer.php
Line: 24
Function: where

File: C:\xampp\htdocs\style\application\controllers\Myaccount.php
Line: 95
Function: getBookings

File: C:\xampp\htdocs\style\index.php
Line: 292
Function: require_once

A Database Error Occurred

Error Number: 1054

Unknown column 'Array' in 'where clause'

SELECT * FROM `bookings` WHERE `cid` = `Array`

Filename: C:/xampp/htdocs/style/application/models/Myaccount_customer.php

Line Number: 26

我尝试通过

$data['cid']->cid

但是它不起作用 当我打印 $data['cid'] 时,我得到了

Array ( [0] => stdClass Object ( [cid] => 8 ) )

那么我如何将其转换为字符串?

php codeigniter
2个回答
1
投票

然后你可以使用 as 来实现它

控制器

public function your_function(){
    $this->load->model('folder_name/my_calling_model');//initialize that model


//your rest code
    $data['my_data'] = $this->my_calling_model->my_calling_function();//your function to be called
}

您可以像上面的代码一样在代码中调用另一个模型,但在传递 $cid 时,您的代码中似乎可能有

typo
,它似乎是
$data['cid']
。所以你的代码看起来像

$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);//<----- changed from $cid to $data['cid']

0
投票

如果我错了,请纠正我,但我认为你想实现这一目标:

$data['customer_phoneno'] = $this->session->userdata('customer_phoneno');
$data['cid']=$this->Stylish_wizard->getCid($this->session->userdata('customer_phoneno'));
$data['bookings']=$this->Myaccount_customer->getBookings($data['cid']);

确保传递正确的数组/对象元素。

编辑: 就像我说的:确保传递正确的数组或对象元素。

$data['cid']
包含一个数组。但你必须传递一个字符串(id)。所以你必须查看这个数组并传递带有你的 id 的正确元素。

© www.soinside.com 2019 - 2024. All rights reserved.