我的模型中有以下代码:
public function getSite()
{
$this->load->library('site');
$data = array();
$str = "SELECT * FROM sitematrix_sites";
$query = $this->getQuery($str);
if($query){
foreach($query->result() as $row){
$site = new Site();
array_push($data, $site->load($row));
}
foreach($data as $site){
$site->data['database'] = $this->getDatabase($site->data['site_id']);
$site->data['baseline'] = $this->getBaseline($site->data['site_id']);
}
return $data;
}
else{
return false;
}
}
我创建了一个名为 site 的类:
class Site{
public $data;
public function __contruct(){
$this->data = ArrayObject(array(), ArrayObject::STD_PROP_LIST);
$this->data['site_id'] = '';
$this->data['site'] = '';
$this->data['site_code'] = '';
$this->data['tc_10'] = array();
$this->data['tc9x'] = array();
$this->data['tc8x'] = array();
$this->data['tc2008'] = array();
$this->data['eng_2005'] = array();
$this->data['database'] = array();
$this->data['baseline'] = array();
但是,我收到以下错误:
<p>Severity: Notice</p>
<p>Message: Trying to get property of non-object</p>
<p>Filename: models/sitematrix_model.php</p>
<p>Line Number: 77</p>
有什么想法吗?我曾尝试以 as$site->data->database
和 as访问这些值,$site->data['database']
但两者都不起作用。
line 77: $site->data['database'] = $this->getDatabase($site->data['site_id']);
函数getDatabase:
public function getDatabase($id){
$database = array();
$str = "SELECT * FROM site_database where site_id='$id'";
$query = $this->getQuery($str);
if($query){
foreach($query->result() as $row){
array_push($database, $row->name);
}
}
return $database;
}
函数获取查询:
public function getQuery($str){
$data = array();
$query = $this->db->query($str);
if(!$query || $query->num_rows() == 0){
return false;
}
return $query;
}
谢谢