Answer the question
In order to leave comments, you need to log in
How to properly organize PHP code on CodeIgniter?
Hello!
I am working on a project on CodeIgniter. The essence of the project is a site for drawing up certain plans.
I will try to briefly and clearly describe what is already there, and where, as it seems to me, I am doing something wrong or do not understand something.
There is the following:
// подгружаем класс работы с активностями
$this->load->library('activity');
// подгружаем класс работы с дестинейшенами
$this->load->library('destination');
// подгружаем класс работы с локациями
$this->load->library('location');
public function insert_entry()
{
$this->title = $this->input->post('title');
$this->content = $this->input->post('content');
$this->date = time();
$this->db->insert('entries', $this);
}
Answer the question
In order to leave comments, you need to log in
Everything is wrong.
You do not understand well what a model is in the classic MVC architecture. This is because Codeigniter itself is weird, I explain the model as a storage handler. Usually this is called Store, that is, what puts and takes from the base. And the model in its classical form is an instance of an object from this base. Simply put, a model is a row from a table, and a Store is an array of models (the whole table in the database, a piece of the table in the database) that loads or saves them into the database.
So, ideally, the model should be responsible for everything. It is the model class that must be inflated, it is the one that you must load, and not the library and not the controller. The controller in general, just like a traffic cop on duty, must read the input, understand what to do with it, load the models, do something with them and send them to the output.
You remove everything from the libraries, write normal models, come up with connections to them (it seems like you have already invented them), and work with the models.
And yet, you can cram several classes into one PHP file, for example, so that the Activity model is an Activity, and the ActivityStore class in the same file just works with the table where your Activities are stored.
I came up with an idea to make everything easier by looking at how it is done in Yii - in each model I have a save () method, which I call directly from the model instance and it goes into the base. Similarly, loading Product::get($id) will return an object of the Product model class, on which I can change a couple of properties and then call ->save() directly on this object;
For
// подгружаем класс работы с активностями
$this->load->library('activity');
// подгружаем класс работы с дестинейшенами
$this->load->library('destination');
// подгружаем класс работы с локациями
$this->load->library('location');
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question