cybertextron
cybertextron

Reputation: 10981

accessing a object variable in PHP codeigniter

I have the following piece of code in my Model:

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

And I created a class called 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();

However, I'm getting the following error:

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

Any thoughts? I have tried to access those values either as $site->data->database and as $site->data['database'] but both don't work.

line 77:  $site->data['database'] = $this->getDatabase($site->data['site_id']);

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

Function getQuery:

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

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

Thanks

Upvotes: 0

Views: 486

Answers (2)

jmadsen
jmadsen

Reputation: 3675

I'm pretty sure your error message is complaining about the

$this->getDatabase

part of that line, not the $site-> aspect.

Are all of these functions on the Model, not the Class site?

when you are working in the model, $this-> has to stick to functions & vars from that class

Upvotes: 0

cybertextron
cybertextron

Reputation: 10981

Doing some investigation, I figured that $site is not a Site, but an Array.

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] => 28000@cinxflex1,28000@cinxflex2,28000@cinxflex3
[UGII_LICENSE_FILE] => 27000@cinxflex1,27000@cinxflex2,27000@cinxflex3
[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
)

Thus, there is not such thing like $site->data['site_id'], but $site['site_id'];

Upvotes: 1

Related Questions