Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP & Codeigniter - how to pass parameters to a model?

I am using the following code to initialize a model from within my controller:

$this->load->model('model_name');

Is it possible to modify the above line somehow so that the model constructor recieves a parameter? I want to use the following code in the model constructor:

function __construct($param_var) {
   parent::Model();

   $this->$param_var = $param_var; //I'm not even sure this works in PHP..but different issue
}

This would be very helpful so that I can reuse my model classes. Thanks.

UPDATE: (from one of the answers, my original question is solved..thanks!) Just to explain why I wanted to do this: the idea is to be able to reuse a model class. So basically to give a simple example I would like to be able to pass an "order_by" variable to the model class so that I can reuse the logic in the model class (and dynamically change the order-by value in the sql) without having to create a separate class or a separate function.

Is this poor design? If so could you please explain why you wouldn't do something like this and how you would do it instead?

like image 459
oym Avatar asked Jul 19 '09 02:07

oym


People also ask

What PHP stand for?

PHP, originally derived from Personal Home Page Tools, now stands for PHP: Hypertext Preprocessor, which the PHP FAQ describes as a "recursive acronym." PHP executes on the server, while a comparable alternative, JavaScript, executes on the client.

Is PHP used anymore?

It is true that PHP has slipped down the rankings of the most popular programming languages, from 5th in 2017 to 8th in 2020 as per the Stack Overflow annual developer survey. And yet, PHP continues to be used by nearly 80% of all websites, powering some major platforms like Wordpress and Facebook.

What is PHP good for?

Although PHP is a general-purpose programming language, it is primarily used for developing dynamic web pages. Considering the availability of PHP-based content management systems such as Moodle and WordPress, PHP is the best solution for blogs, learning management systems, and e-commerce websites.


2 Answers

You can't pass parameters through the load function. You'll have to do something like:

$this->load->model('model_name');
$this->model_name->my_constructor('stuff');

In the model:

function my_constructor($param_var) {
...
}

Response to update:

You could just pass the order_by value when you're calling your model function. I'm assuming in your controller action, you have something like $this->model_name->get($my_id); Just add your order_by parameter to this function. IMO this makes your model logic more flexible/reusable because the way you were doing it, I assume setting order_by in the constructor will set the order_by value for every function.

like image 118
jimyi Avatar answered Sep 26 '22 00:09

jimyi


I see your reasoning for this, but may I suggest looking at Object-Relational Mapping for your database needs. There is a user-made ORM library for CodeIgniter called DataMapper that I've been using lately. You can use tables in your controllers as objects, and it may be a better fit for your problem.

like image 43
Dom M. Avatar answered Sep 24 '22 00:09

Dom M.