Jump to content

Attempt to assign property of non-object in...


MarioRossi

Recommended Posts

I'm having issues with the following function in PHP 5...

 

        function getTreeWithChildren()
        {
            $category_id = $this->fields['id'];
            $parent_id = $this->fields['parent'];
                        
                        $this->DB->build( array(        'select' => join(',', $this->getFields()), 
                                                                                'from' => $this->table,
                                                                                'order' => $this->fields['sort']
                                                         )              );
                        $this->DB->execute();


            // create a pseudo root level object
            $root = new stdClass;
            $root->$category_id = 0;
            $root->children = array();

            $arr = array($root);

            // populate array and create empty child array
            while ($row = $this->DB->fetch()) {
                $arr[$row->$category_id] = $row;
                $arr[$row->$category_id]->children = array();          
                                }

            // build child data
            foreach ($arr as $id => $row) {
                if (isset($row->$parent_id))
                     $arr[$row->$parent_id]->children[$id] = $id;
            }

            return $arr;
        }

 

I'm getting the error

 

Attempt to assign property of non-object in ... on line 391

 

which is...

 

$arr[$row->$category_id]->children = array(); 

 

 

I've tried typecasting "$this->DB->fetch()" to an object with...

 

while ($row = (object) $this->DB->fetch()) {

 

but get a maximum execution time exceeded error instead.

 

The code was originally written for PHP 4 so I suspect it might be a way the objects are handled and the use of stdClass()?

 

I'd be extremely grateful if somebody could give me pointers to make this PHP5 compatible (5.2.9)? Thanks in advance.

If you really need it as an object, you might do

 

$rowOb = new stdClass();
foreach($row as $key => $value) {
  $rowOb->$key = $value
}

 

It's not pretty though. Ideally you should create a class that represents whatever this object is meant to represent.

I suppose it doesn't necessarily have to be an object I'm just following an article here... http://www.phpriot.com/articles/nested-trees-2 ... The whole class is displayed here...

 

http://www.phpriot.com/articles/nested-trees-2/7

 

I'm trying to learn as well not just plug something that works.

 

It's the rebuild() method and the resulting recursion aspect of the class I'm having difficulty getting my head around. I understand the rest of the theory and methods OK.

 

Ideally you should create a class that represents whatever this object is meant to represent.

 

Like you say I'm not even sure an object is necessary or indeed preferred could the same thing be achieved using an array? To put it in context I'm calling rebuild() from my applications controller which is then grabbing all of the category data from the DB. Now I believe what the troublesome part of the code is doing is reformatting/nesting the data so each node also contains references to it's children. The resulting data is then used recursively to update the database. 

 

 

 

 

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.