Welcome Guest, Not a member yet? Register   Sign In
ERROR getting multiple instances of a Library Class in CodeIgniter
#1

[eluser]Unknown[/eluser]
Hi there guys,

I got a categories.php which is my Controller. I also created a treenode.php and put it in my libraries/ folder.
Inside my controller I got a function test() (see code below)

Code:
function test() {

  $this->load->library('treenode');
  
  $treeNodes = array();

  $categories = $this->category->get_categories();
  if (count($categories) != 0) {
   foreach ($categories as $category) {

    $params = array('parent'=>$category['parent_category'], 'children'=>array());
    $this->load->library('TreeNode', $params);

    [color=red]$currentTreeNode = $this->treenode;[/color]
    
    print_r($currentTreeNode);
    echo "<br /><br />";
    $treeNodes[$category['id']] = $currentTreeNode;
   }

   print_r($treeNodes);

   foreach ($categories as $category) {
    if ($category['id'] != 1) {
     print_r($category['id']);
     $currentTreeNode = $treeNodes[$category['id']];
     $parentTreeNode = $treeNodes[$currentTreeNode->get_parent()];
     $parentTreeNode->add_child($currentTreeNode);

    }
   }
  }

  $this->load->view('supplier_index', $data);
}

The problem is that when I do the print_r($currentTreeNode) I get always the first instance instead of a new one every time. So I suppose it has to be a problem with the creation of Library instances.

I get this output:
Quote:TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )

TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )

TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )

Array (
[1] => TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )
[2] => TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )
[3] => TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) ) )

where I would like to get:
Quote:TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )

TreeNode Object ( [_parent:TreeNode:private] => 1 [_children:TreeNode:private] => Array ( ) )

TreeNode Object ( [_parent:TreeNode:private] => 1 [_children:TreeNode:private] => Array ( ) )

Array (
[1] => TreeNode Object ( [_parent:TreeNode:private] => 0 [_children:TreeNode:private] => Array ( ) )
[2] => TreeNode Object ( [_parent:TreeNode:private] => 1 [_children:TreeNode:private] => Array ( ) )
[3] => TreeNode Object ( [_parent:TreeNode:private] => 1 [_children:TreeNode:private] => Array ( ) ) )

Here is my categories database table in order to have a more complete view of the problem:
[id] name parent_category
1 ROOT 0
2 FOOD 1
3 NONFOOD 1
#2

[eluser]hot_sauce[/eluser]
Code:
Add this in your helper!
function tree($pc = 0){
$CI = & get_instance();
$arr = array();
$arr = tree_recursive($pc,$arr);
return $arr;
}
function tree_recursive($id,$arr)
{
$CI = & get_instance();
$q = $CI->db->query('select * from categories where parent_category = '.$id);
if ($q->num_rows()>0){
  $res = $q->result();
  foreach($res as $r){
   $arr[$r->id] = array(
    'id' => $r->id,
    'childs' => array()
   );
   $arr[$r->id]['childs'] = tree_recursive($r->id,$arr[$r->id]['childs']);
  }
}
else return array();
return $arr;
}

controller
public function test()
{
$arr = tree('your id') // default 0;
}
#3

[eluser]joergy[/eluser]
Because CI instantiates models and libraries for You, it always reuses them if possible.
I caught the same pitfall.
Therefor I don't use CTORs of Libraries for initialization but my own "init()" function.

On the other hand You can use a non documented third parameter, which will become the instance-name.
E.a. $this->load->library("libname",$params,"instance_of_lib");
then using it
$this-> instance_of_lib->...
But then You must somehow track the instance names already used....
#4

[eluser]CroNiX[/eluser]
[quote author="joergy" date="1403810159"]
On the other hand You can use a non documented third parameter, which will become the instance-name.[/quote]
The 3rd parameter for the instance is documented for the loader::library() method.
http://ellislab.com/codeigniter/user-gui...oader.html




Theme © iAndrew 2016 - Forum software by © MyBB