Welcome Guest, Not a member yet? Register   Sign In
Is it possible to have more than a single instance of a model?
#1

[eluser]MyDarkPassenger[/eluser]
I'm a little confused by how models work in CI. Is it possible to have more than one instance of a model open at a time. I'd like to use them as data objects but it's not very useful if I can only have 1 instance open. Is there another way to create classes with CI.
#2

[eluser]vitoco[/eluser]
i think this may work
Quote://If you would like your model assigned to a different object name you can specify it via
// the second parameter of the loading function:

$this->load->model('Model_name', 'fubar');

$this->fubar->function();

So, you can specify 2 different object names , so you'll have 2 instances of the model

http://ellislab.com/codeigniter/user-gui...ml#loading
#3

[eluser]skunkbad[/eluser]
[quote author="MyDarkPassenger" date="1250404885"]I'm a little confused by how models work in CI. Is it possible to have more than one instance of a model open at a time. I'd like to use them as data objects but it's not very useful if I can only have 1 instance open. Is there another way to create classes with CI.[/quote]

It sounds like you might not fully understand what a model is for, or how classes and objects work. When you load a model in a controller, the model is available within the scope of either the entire class if you loaded it from a constructor method, or an entire method if loaded from within a method. Once loaded, you can use the model as often as you wish.
#4

[eluser]jedd[/eluser]
[quote author="MyDarkPassenger" date="1250404885"]I'm a little confused by how models work in CI. Is it possible to have more than one instance of a model open at a time. I'd like to use them as data objects but it's not very useful if I can only have 1 instance open. Is there another way to create classes with CI.[/quote]

Hey MyDarkPassenger, and welcome to the CI forums.

I posted a similar question a while back in [url="http://ellislab.com/forums/viewthread/108763/"]this thread[/url] - you may find it interesting.

Summary - pretty much everyone uses CI models as a library (in the set of similar functions sense) to talk to one table (or a small, related set of tables) within the DB. But having said that, you can do the new $model_object thing to your heart's content if you want to go down that path.

My gut feel after having looked at the latter, then stuck with the former, is that CI is geared up to the library approach, PHP isn't really geared up for OOP in the same sense that most language-agnostic OOP books expect OOP languages to work, and I speculate that if you go down the treat-a-model-as-an-object path, you'll probably find some models get used this way while others do not.

This might, and again I'm really speculating here as I've not thought about it too much, let alone designed anything this way, mean you're better off using a handful of libraries to act as the kinds of objects you're talking about (and they front the various models) and retaining all model interactions in the conventional CI approach. I expect that that'd get real messy real quick, however - and that you'd want to implement some particularly strict guidelines for your code.
#5

[eluser]jedd[/eluser]
[quote author="skunkbad" date="1250425384"]

It sounds like you might not fully understand what a model is for, or how classes and objects work. When you load a model in a controller, the model is available within the scope of either the entire class if you loaded it from a constructor method, or an entire method if loaded from within a method. Once loaded, you can use the model as often as you wish.[/quote]

I suspect our new member has a better idea of objects and classes than you grant them. The CI way (or at least the common way most people use CI) of using a model isn't particularly OOP. Note that I'm not from an OOP background - I was trained in particularly procedural languages - so I'm going by stuff I've read rather than done.
#6

[eluser]wabu[/eluser]
>>how models work in CI

Yeah it's a little different in CI because models here aren't based on the active record pattern as in other frameworks. But you might consider whether or not you really need that, and CI still promotes modularity or a "separation of concerns."

If you need a simple CRUD example, check out this guy's article:

http://henrihnr.wordpress.com/2009/04/26...plication/
#7

[eluser]Nick Husher[/eluser]
There are a few reasons why you'd want multiple instances of a model object, or some object that looks like a model. Have you considered defining an instance-able class within your model file? So, something like this:

Code:
// example_model.php
class Example_Model extends Model {
// methods and properties
}

class Example {
// instance-specific methods and properties
}

The Example_Model could return instances of Example when interacting with the database, and you can declare a new Example() as well. At that point you're looking at a really lightweight ORM concept, which can get abstract and complicated really fast, but it's something to consider.




Theme © iAndrew 2016 - Forum software by © MyBB