0

I am trying to pass variables into the model when I call it from the controller...

$this->load->model('Some_model',$var1, $var2);

$var1 and $var2 being collected from the URL in the controller's index function. However these are not being picked up in the code?

Is this at all possible as it is pretty important that they are there from the get go in the model?

thanks.

4
  • You need to add them to the constructor as well. Have you? Commented May 30, 2013 at 16:19
  • it is pretty important that they are there from the get go in the model? Why's that, then? Commented May 30, 2013 at 16:27
  • The second parameter is used to rename the instance of the model. For example, $this->load->model("Some_model", "some_model_renamed"); $this->some_model_renamed->blah_blah(); Commented May 30, 2013 at 16:36
  • I'm extending a library which requires certain things to happen in order to use it. If this model is to do what I want it to, then I need to supply some variables to tell it what to access. Commented May 30, 2013 at 17:06

2 Answers 2

1

You really can't do that with the built-in loader, the model loading code hardcoded with empty new parameters (at least in version 2.1).

You might want to move your code to a library since library loading can take a second array parameter with options, see here.

Since CI's loader will only create one instance of these classes, you can move your configuration into the config files and read them from there in your __construct.

Alternatively you could extend the Loader class and reimplement the model() method to take some parameters (if you don't mind having a 4th parameter or something like that).

Sign up to request clarification or add additional context in comments.

1 Comment

I have figured it out by overriding the function in the library that I'm extending with another in the model I'm writing. That way I can do all the set up it needs far more dynamically. Thanks for your answers.
1

Models don't take data arguments when loaded. Models reflect the underlying data model of your application. This should never change based on user input.

Here is the documentation for loading models which I'm sure you have already read (hopefully you're not just programming by guessing‽); there is an optional second argument that allows you to override the model's name, but no facility for passing data.

Instead, consider using a library, or pass these arguments to whichever model functions need them.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.