Welcome Guest, Not a member yet? Register   Sign In
Problem loading model
#1

[eluser]Unknown[/eluser]
Hi all!
It's my first time with CI. Always i build my apps writing all the code!

So, I found a problem accidentally when I was writing a class. I'm not sure it's a bug, maybe the admin will have to move this topic to another category.

The problem is simple.

I have this code:

Code:
class A extends Model {

    function A()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->model('B');
    }
}


class B extends Model {

    function B()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->model('A');
    }
}

So, A load B and B load A.

This launch this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in C:\xampp\htdocs\prestamit\system\codeigniter\Base5.php on line 154

An infinite loop.

Maybe it's wrong to load a model from another model.

It's a bug?

Thank you! Cheers.
#2

[eluser]imn.codeartist[/eluser]
[quote author="diego.c" date="1261038590"]Hi all!
It's my first time with CI. Always i build my apps writing all the code!

So, I found a problem accidentally when I was writing a class. I'm not sure it's a bug, maybe the admin will have to move this topic to another category.

The problem is simple.

I have this code:

Code:
class A extends Model {

    function A()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->model('B');
    }
}


class B extends Model {

    function B()
    {
        // Call the Model constructor
        parent::Model();
        $this->load->model('A');
    }
}

So, A load B and B load A.

This launch this error:

Fatal error: Allowed memory size of 134217728 bytes exhausted (tried to allocate 261900 bytes) in C:\xampp\htdocs\prestamit\system\codeigniter\Base5.php on line 154

An infinite loop.

Maybe it's wrong to load a model from another model.

It's a bug?

Thank you! Cheers.[/quote]

The code you have done here is not a bug its illogical and you should never do that.

To solve this problem, you can create Wrapper where you can write your common code in one class and inherit them in all desired classed.

for example


Code:
class Master extends Model
{
  function Master()
    {
        // Call the Model constructor
        parent::Model();
        
    }

// Write your desired common code in this model
}

class A extends Master{

    function A()
    {
        // Call the Model constructor
        parent::Master();
        
    }
}


class B extends Master{

    function B()
    {
        // Call the Model constructor
        parent::Master();
        
    }
}

For more information, please search in the net for Object Oriented Design Patterns
#3

[eluser]wiredesignz[/eluser]
It is a bug in CI and it is not illogical at all.

CodeIgniter uses the singleton pattern when creating objects and registers loaded models so that you should be able to use that code without problem.

Any subsequent calls to load->model() should detect that a model already exists and return without instantiating a new object. Thus avoiding the loop.

You should post this problem in the bug tracker, but don't expect it to get any attention anytime soon.
#4

[eluser]Unknown[/eluser]
Thank you dixcoder for your reply. I know Object Oriented Design Patterns, because I'm an experienced and professional application architect. Thank you for your recommendation.

My question was oriented to the response that did for wiredesignz. If this problem should be a bug to report on the CI's bug tracker.

I guess if CI uses the singleton pattern to create objects, the problem would be resolved easyly.

Cheers!




Theme © iAndrew 2016 - Forum software by © MyBB