Welcome Guest, Not a member yet? Register   Sign In
Call, another model in a model
#1

[eluser]chakhar86[/eluser]
Hi there,

I curious to know if it's possible to load a model inside model???

I've tried and it failed.

I think CI should have this feature. For example when we create a model representing a table which has a relation to another table (which I defined in another model).

I need suggestion...

sorry for bad english Smile

I've tried this,though:
Code:
class Has_something extends Model {
  
  var $something;
  
  function Has_something() {
    require_once('./system/application/models/something.php');
    $this->something[] = new Something();
  }
}

oh, yeah. Another thing, is it possible to pass a parameter to Model (or Library) constructor?
#2

[eluser]Colin Williams[/eluser]
Use the get_instance() function to get an instance of the CI superobject, then use the normal APIs to load and call model functions.

Code:
$ci =& get_instance();
$ci->load->model('other_model');
$ci->other_model->get_related(3);

Quote:Another thing, is it possible to pass a parameter to Model (or Library) constructor?

It wouldn't make much sense to, since models in CI are not really designed to represent single objects (they are purely organizational).
#3

[eluser]chakhar86[/eluser]
[quote author="Colin Williams" date="1227725484"]
It wouldn't make much sense to, since models in CI are not really designed to represent single objects (they are purely organizational).[/quote]

But, still.
What if I want to implement ORM like Hibernate? Wouldn't it be more MVC-ish?
#4

[eluser]dcunited08[/eluser]
I load a model into another model by calling the
Code:
$this->load->model('my_model');
. In order to do that, I added a method to MY_Loader like the following:
Code:
// $name is the name that the model was stored in.
function getModel($name)
    {
        return $this->_models[$name];
    }

It is possible to send data to the Library constructor but not to the model constructor (wish it was)
#5

[eluser]Colin Williams[/eluser]
MVC is mainly about organization and responsibility. ORM is a different concept altogether. Also, ORM doesn't require a class for each object. You can achieve good enough ORM with stdClass.
#6

[eluser]sophistry[/eluser]
i am pretty sure that if you autoload the model you can access it in the regular way. no need to use the CI instance unless you want to for some reason.
#7

[eluser]Colin Williams[/eluser]
I just recall people having trouble with doing it that way, sophistry. Getting a reference is guaranteed to work, so that's why I recommended it. I don't think I've ever called on model from another, so I have no direct experience with it, and am not bothered enough to try it out myself.
#8

[eluser]sophistry[/eluser]
@colin, yeah, haven't tried autoloading models, i prefer to use an instance to make it clear that i am going "outside" the current model.

anyway, just noticed that you went from 1337 posts to 1338 posts on that reply! shouldn't crossing the 1337 threshold give you some extra star or whatever?
#9

[eluser]Colin Williams[/eluser]
Nah.. just makes my keys that much more worn-looking.
#10

[eluser]chakhar86[/eluser]
So, there is no way we can pass parameter to model constructor except I hack (maybe extends it (thanks dcunited08)) the Loader class?




Theme © iAndrew 2016 - Forum software by © MyBB