2012-06-14 66 views
0

我有以下的在我的模型一段代码:访问对象变量在PHP笨

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; 
    } 
} 

我创建了一个名为类网站:

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> 

有什么想法?我试图访问这些值为$site->data->database$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; 
} 

功能getQuery:

public function getQuery($str){ 
    $data = array(); 
    $query = $this->db->query($str); 

    if(!$query || $query->num_rows() == 0){ 
     return false; 
    } 
    return $query; 
} 

感谢

+0

77号线是什么?如何删除“$ this-> data = ArrayObject ....”? – Robert

+0

可能会缺少一些'new'吗? – hakre

+0

我已经更新了我的问题,现在你们能够看到第77行: – cybertextron

回答

0

我敢肯定你的错误信息是在抱怨

$这个- > getDatabase

该行的一部分,而不是$ site->方面。

所有这些功能都在Model上,而不是Class网站上?

当你在模型工作

,$这 - >必须坚持功能&瓦尔从该类

+0

getDatabase是模型的一部分。 – cybertextron

1

做一些调查,我估计$site不是Site,而是一个数组。

Array ( [site_id] => 1
[site] => Ames
[site_code] => ame
[windows_ref_unit_location] => \\plm\amnas\tc_ref
[unix_ref_unit_location] => /tc_ref
[windows_rte_location] => \\plm\amnas\tc_rte
[unix_rte_location] => /tc_rte
[windows_toolbox_location] => &lt;path-to-local-toolbox&gt;
[unix_toolbox_location] => /tc_ref/TOOLBOX
[UGS_LICENSE_SERVER] => [email protected],[email protected],[email protected]
[UGII_LICENSE_FILE] => [email protected],[email protected],[email protected]
[unix_dev_units] => /tc_work
[unix_devop_path] => /usr/site/devop_tools
[netapp_filer] => \\plm\amnas
[perforce_proxy_path] => /p4p/p4p_cache
[primary_contact] => Philomena Siddle
[secondary_contact] => Mathieu Sarrazy
[num_users] => 7
)

因此,没有这种事情像$site->data['site_id'],但$site['site_id'];

+0

我怀疑这个,很好的表演老伙计。 – xyz