Welcome Guest, Not a member yet? Register   Sign In
Calling a model from another model?
#21

[eluser]mattalexx[/eluser]
[quote author="mattalexx" date="1214261377"][quote author="Randy Casburn" date="1214256399"]@mattelex

Models: Look for Anatomy of a Model

Libraries: Look for (uh...) Naming Conventions


Come on Dude...this is one of the finest User Guides on the planet. Try a little harder.[/quote]

Why are you being so condescending?

1. Go here
2. Ctrl+F for "Blogmodel"
3. Ctrl+F for "User_model".

Am I missing something here or is that a discrepancy in the docs?[/quote]

Now this problem is explained in the docs:

http://ellislab.com/codeigniter/user-gui...hod_naming
#22

[eluser]Randy Casburn[/eluser]
Yes, the inclusion of the style guide certainly clears up the confusion for new folks. It's a good thing.

Randy
#23

[eluser]Pascal Kriete[/eluser]
Quote:Now this problem is explained in the docs:

http://ellislab.com/codeigniter/user-gui...hod_naming
Why is that link an email?
#24

[eluser]mattalexx[/eluser]
Whoops, fixed now.
#25

[eluser]Unknown[/eluser]
[quote author="Randy Casburn" date="1214780514"]

It appears from looking at the CI internals that CI was designed with the idea that your Model functionality would be consolidated _functionally_ within a single file. That doesn't mean that you cannot use multiple Model files. Some times one might call multiple model files from the Controller and consolidate the data in the Controller prior to sending it to the View for example. But now I'll get flamed for breaking MVC because of the cloudy delineation between Model and Controller (academically). Either way, it seems CI prefers you to consolidate the data gathering routines into a single file. Then CI expects you to call that single Model to gather your data so you can deliver that data to your View.

That means all the dependency you've created here (and segregated into separate files) is expected to be in a single file from CI's perspective.

So here is the question. Do you ever use these classes or the functionality found in them independently of one another? Are they 'generally' used together and rarely used separately. If this is the case, then you've substantiated the CI design principles. Right? You should probably combine the functionality into a single class and be done with the problems this is causing you. Then consider this in future designs.

If they are always used independently, then consider that in future designs too. Knowing that you'll have to take special care to make the CI object available inside you're additional class structures.

We all do it both ways depending on the nature of what we are doing. I will tell you, though, given what I know, and nothing more, of this situation, I would consolidate this Model into a single class unless it is completely unwieldy. I would also consider the Outer Joins as a separate specialized DB class of sorts. But that's because I tend to consolidate my queries as much as I can.

Hope this is helpful.

Randy[/quote]

I jump into the conversation as I have the exact same problem the author has.
Before going deep into my questions, be aware that I'm an old school programmer who has learned Java like 8 years ago and I've never used MVC before.
Putting everything in a single controller is perhaps how we should use MVC, but it does not look very OOP to me. Shouldn't we separate as much as possible the different layers, ie having as many models as we have "object" in our database model?
If so, we can not put everything in one single file, and thus we need to have a lot of models, and call those models in other model to have the good hierarchy.
#26

[eluser]Colin Williams[/eluser]
Quote:If so, we can not put everything in one single file, and thus we need to have a lot of models, and call those models in other model to have the good hierarchy.

And, as Randy demonstrated, that is certainly possible. You just need a reference to the CI object.
#27

[eluser]XedinUnknown[/eluser]
Hi all!

I hate to revive dead threads, but I felt that creating another one would be too much, since this one's problem is apparently still in question, and it only has two pages, so....

I have come across the same problem as the author. And, from what I've read here, CI's creators think it is useless to call another model's methods from within a model, and thus have designed it in a way to make it difficult to do so. However, it says in the manual that the architectural goals of CodeIgniter are "flexibility", "loose coupling" and "singularity". Well, how can you achieve those, if the components of the system cannot use one another..? What flexibility and reuseability are we talking about then..? I think it is perfectly rational to invoke objects from other objects. I think that "loose coupling" does not mean "insane incapsulation". One object is not supposed to perform all work in my opinion. And I totally agree that it is definately not OOP to do as CI intends. Moreover, there is nothing deviating from the MVC approach. Magento, one of the best (but, unfortunately, one of the most poorly documented) packages, does this all the time; it is essential to do this in Magento. This is not an attempt to advertise the eCommerce solution...

So I'd really like to know how to invoke a model's method from within another model. I have done as it says in the Manual (used the get_instance method), and it kinda works, but not as expected. You see, the problem is that CI stores the invoked models somehow inside, but from within one model it is impossible to know whether another model, or another model with the same name, has been instanciated before, and this leads to a disaster when addressing these models. Especially this goes crazy when specifying a second parameter while loading, like so:
Code:
$this->load->model('Model_name', 'fubar');
This is very frustrating. Please, if someone knows how to do this properly, tell me, as I really need this kind of functionality.

Thanks.
#28

[eluser]jedd[/eluser]
Hi XedinUnknown,

[quote author="XedinUnknown" date="1260983935"]
I hate to revive dead threads ...
[/quote]

Your intuition is correct - it's bad form to revive old threads, as it pings a bunch of people that have long since demonstrated their lack of interest in this thread, possibly this forum. Please start a new thread in preference to reviving any thread that's been inactive more than a month.

Quote: ... CI's creators think it is useless to call another model's methods from within a model, and thus have designed it in a way to make it difficult to do so.

I haven't seen anything that explicit, but I expect the logic is inverted - it's difficult to facilitate this, and inspires laziness in the application designer, consequently they didn't bother trying to wrangle the framework to provide this functionality.

Quote:So I'd really like to know how to invoke a model's method from within another model.

I would humbly suggest that you are approaching this problem with a solution already in mind - and consequently trying to make that solution work.

Instead, again offered as a humble suggestion, I think you may want to reconsider your application design.

Perhaps describing, in broad terms, what you are trying to do would be helpful.

This is as opposed to describing how you want to do something and then demanding the framework allows it.

Please start a new thread, describing the nature of your problem from the beginning.
#29

[eluser]wiredesignz[/eluser]
Quote:... consequently they didn’t bother trying to wrangle the framework to provide this functionality.
Absolute rubbish.

There is a bug in CodeIgniter that prevents loading a model and accessing it from another model. This issue is discussed quite often and there is a solution described in the bug tracker.

This link should lead you there.
http://ellislab.com/forums/viewreply/651558/




Theme © iAndrew 2016 - Forum software by © MyBB