Welcome Guest, Not a member yet? Register   Sign In
Common function
#1

Hello, how do I do a function that works in all controllers and that uses SQL queries? Thank you


 
Reply
#2

Create a core/MY_Controller.php file. Have it extend CI_Controller. Have your controllers extend MY_Controller. Put your method in MY_Controller.php.
Reply
#3

Create a model.
Autoload the model in config/autoload.php or just load it inside the __construct() method of the controller(s) in which you need the functions of the model.
For more information about models: https://www.codeigniter.com/userguide3/g...odels.html
Reply
#4

I almost always extend the CI controller with my own controller. This seems a bit overkill but in to many projects something changed that required some new global function. With all my controller already using my own (at that point empty) controller I never have to remember to change all the controller that now need this function.

Here the docu on how to do it
https://www.codeigniter.com/userguide3/g...asses.html
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#5

I keep saying that a model is the best solution for functions that perform SQL-queries. A MY_Controller is best for common functions in your application's logic, but when it comes to database interaction, a model is your friend.
Models themselves can also be standardized. I use Avenirer's MY_Model, which is great.
Reply
#6

@Wouter60 this might be a bit off topic but I do not understand why I should use MY_Model? I looked and read the articles from Avenirer and Jamie Rumbelow. But to be honest I dont see the point. We already have Active Record and from what I see they do just what Active Record already does. $this->user_model->insert(array( 'email' => 'blah' )) instead of $this->db->insert('mytable', $data).

I am sure I must be missing something here and would appreciate any additional article I should read up on. I have been a programmer for 30 years and maybe I am just stuck to much in my old ways.
On the package it said needs Windows 7 or better. So I installed Linux.
Reply
#7

I do not think you are missing anything, as I too have wondered exactly the same thing.

Sure, you do not have to create a model function when you decide you want to get, say, user details from a user table. You just use the generalised model to get a row from any table you choose. But I have found that very quickly these generalised models and table interactions become more and more bulky as complexity grows, and less and less useful. In the end, with so many special cases, the time saved is negligible. So I stopped using them altogether. (I had done my own one, as I really thought the idea was a good one, but after one project, I abandoned it as unnecessary bloat).

Also, if I change a table (its name, columns, indexes, whatever) I want to update just one or a few models that refer to the table. Not go through every controller that calls the function to alter the name.

Finally, I prefer...

PHP Code:
$user_details $this->user_model->get_user_details(); 

...in my controller. There is no reference here to anything about how the user is stored, what is stored or where (session data, database, files etc).

Now if my user table changes, I update the user model - all done.

Why would I want my user model to access another model to access the database? Just to save a bit of typing? No. And for ease of maintenance, I do not think DRY means force every database call through yet another abstraction layer. It is only about 5% of the time that I have to write an SQL query directly for complex queries. For the vast majority of queries, the query builder is stable, simple, safe, fast, concise and ultimately easy to maintain.

But perhaps like you rtenny, I am missing something important about this. And after 30 years of programming, imagine all the lessons you have learned about coding that even you probably do not realise you apply. You just do it naturally. And to me, naturally, this seems like a nice to play with but actually completely unnecessary layer.

Best wishes,

Paul.

PS the only place I can see this being useful is if you need to record every transaction through a database, like for financial transaction recording. But if that was the case, you would not do it through your application code, but separately on the database itself. (I have never done this but I read some really interesting stuff a while back about how our databases can do such things.)
Reply




Theme © iAndrew 2016 - Forum software by © MyBB