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

[eluser]sigma[/eluser]
Hello

I've got following Problem:
I'd like to use a model in an other model. Something like this:
Code:
class Location extends Model{
      public function get($ID){
                //Other Stuff
                $this->load->model('locationType');
                $this->locationType->setID($result->LocationTypeID);
                $this->locationType->setTitle($result->TypeTitle);
                $this->_locationType = $this->locationType;
                //Other Stuff
    }
}

But there is allways following error-message:
Quote:A PHP Error was encountered

Severity: Notice

Message: Undefined property: Location::$locationType

Filename: models/location.php

Line Number: 62

Can anybody help me please?

Greets
#2

[eluser]adamp1[/eluser]
First thing I would say is have you got the class name right? Maybe try LocationType,Locationtype, locationtype.
#3

[eluser]Craig A Rodway[/eluser]
I think it's a requirement that models are called with the _model suffix but this is how I do it:

Code:
class Location extends Model{
      public function get($ID){
                // Get main CI object handle and load model
                $CI =& get_instance();
                $CI->load->model('locationType_model');
                // Call new model functions using handle to main CI object
                $CI->locationType_model->setID($result->LocationTypeID);
                $CI->locationType_model->setTitle($result->TypeTitle);
                $this->_locationType = $CI->locationType;
                //Other Stuff
    }
}
#4

[eluser]sigma[/eluser]
Thank you! Now it works!
The solution was:
Code:
class Location extends Model{
      public function get($ID){
                // Get main CI object handle and load model
                $CI =& get_instance();
                $CI->load->model('LocationType');
                // Call new model functions using handle to main CI object
                $CI->LocationType->setID($result->LocationTypeID);
                $CI->LocationType->setTitle($result->TypeTitle);
                $this->_locationType = $CI->LocationType;
                //Other Stuff
    }
}
#5

[eluser]wiredesignz[/eluser]
Try not to load models within other models, load them both in the same controller. Or in CI 1.6.0 you can autoload common models.
#6

[eluser]sigma[/eluser]
Why? Performance-Problems?
#7

[eluser]wiredesignz[/eluser]
Bad practice. It's called hacking code for a quick fix. I only use &get;_instance() if theres absolutlely no other way to do it. Even in libraries you can instantiate a new CI_something() object to avoid using the CI super-object.
#8

[eluser]lifewithryan[/eluser]
I agree, mixing models are bad practice. Now that doesn't mean that if you need to to get the job done, that you can't do it, but from a more "pure" standpoint...and object should be "dumb" and only know about itself.

In other words, the shouldn't be any business logic in your models, it should know how to create/update/delete itself and access its own properties but really thats about it. (Again, sometimes when the checks need to be written or you don't eat you can stray away, but its just an *ugly* solution).

Just my two Mhz...
#9

[eluser]wiredesignz[/eluser]
Quote:In other words, the shouldn’t be any business logic in your models

Thats not quite correct, Models are specifically for business logic while encapsulating database access.

Sez wikipedia:

Model

The domain-specific representation of the information on which the application operates. Domain logic adds meaning to raw data (e.g., calculating if today is the user's birthday, or the totals, taxes and shipping charges for shopping cart items).

Many applications use a persistent storage mechanism (such as a database) to store data. MVC does not specifically mention the data access layer because it is understood to be underneath or encapsulated by the Model.

An MVC model component is intended for domain logic, not data access. The description says very clearly that the data access layer is understood to be underneath or encapsulated by the model.
#10

[eluser]lifewithryan[/eluser]
I suppose its a matter of taste I guess...most people that I work with prefer the business logic to be in some sort of service layer and the models should be completely dumb, (granted though, I come for the Java world) and 99% of our models are getters and setters and possibly a toString method. I will agree that DB access is usually pawned off on something like hibernate.

Our service layers know more about the models and perform the usiness logic tasks.




Theme © iAndrew 2016 - Forum software by © MyBB