Model Bouncing - Best Practices? |
[eluser]darioism[/eluser]
Didn't want to use the word "looping", but don't know what else to call it. Let's say I have the following model: Code: $car_info = $this->cars->get($car_id); Then I also have the following model: Code: $manufacturer_info = $this->manufacturers->get($manufacturer_id); The problem is, these models overlap and loop back and forth upon themselves, as they try to obtain information about each other's information. What would be a best practice as far as design in this case, and is there a programming term for this predicament? Thanks.
[eluser]WanWizard[/eluser]
You have to stop the recursion in your code. You could do that with a second parameter (for example a boolean) for one of the two method calls, instructing the method not to dig deeper. for example, if you call $this->car-get() from the manufacturers model, you allready know the manufacturer. So use the second parameter to indicate you only want car info, and no manufacturer details.
[eluser]darioism[/eluser]
Thanks for the reply. I was hoping there was a more complex way of detecting this recursion or an advanced programming technique that I don't know about yet for architecting a solution. If not, the boolean approach is of course a good solution, but I like to have as few switches as possible in my code for simplicity. Maybe I'm overthinking it?
[eluser]WanWizard[/eluser]
You could do things dynamically by walking up the object chain ( this -> parent -> grandparent -> ... ) and check if the class object you are about to create is already present further up the chain. Which is a lot of code, and is going to slow your application down considerably. Sometimes KISS is the best approach, if something sounds complex, it usually is...
[eluser]Nick_MyShuitings[/eluser]
[quote author="darioism" date="1283617090"]Thanks for the reply. I was hoping there was a more complex way of detecting this recursion or an advanced programming technique that I don't know about yet for architecting a solution. If not, the boolean approach is of course a good solution, but I like to have as few switches as possible in my code for simplicity. Maybe I'm overthinking it?[/quote] From the way you are thinking, it sounds like an ORM is what you are looking for. There are a few written specifically for CI, as well as wikis like this one regarding Doctrine: http://codeigniter.com/wiki/Using_Doctri...e_Igniter/. If you want automatic mapping between objects from the database, then you are talking ORM. |
Welcome Guest, Not a member yet? Register Sign In |