A
A
Anton Dyshkant2015-03-29 00:48:29
CodeIgniter
Anton Dyshkant, 2015-03-29 00:48:29

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:

  • Activities controller (+ model, + view) that works with Activity entities (plan of a specific user action IRL - with name, date-time, description)
  • Destinations controller (+ model, + view) that works with Destination entities (this is a plan for a visit to some city - with dates and description)

Each Activity corresponds to one Destination, i.e. each planned action takes place in a pre-planned place.
At the moment, everything is implemented as follows: in addition to the specified controllers, the Destination, Activity, and Location classes have been created (this is the base of cities without reference to the user's plans). Accordingly, an object of the Activity class has a property that is initialized in its constructor with an object of the Destination class. At the same time, an object of the Destination class has a property that is initialized with an object of the Location class.
As a result, in the Activities controller, in almost every method, we have something like this:
// подгружаем класс работы с активностями
$this->load->library('activity');

// подгружаем класс работы с дестинейшенами
$this->load->library('destination');

// подгружаем класс работы с локациями
$this->load->library('location');

Those. Since operations with objects of the Activity class take place in the models, it is necessary to include the Activity library, as well as Destination and Location. As a result, these three lines are duplicated in each controller method. And it seems to me that I'm doing something wrong :)
Please evaluate the literacy of my decision on the organization of the code and advise how this organization can be improved.
That is, to be more specific:
  1. Is my approach to creating the Destination, Activity and Location classes correct in terms of object nesting?
  2. Is my approach to creating the Destination, Activity and Location classes correct, in the sense of putting interdependent entities in different libraries?
  3. Is my approach to including libraries correct?
.
UPD :
I found some examples of models on the CodeIgniter documentation site:
public function insert_entry()
{
    $this->title = $this->input->post('title');
    $this->content = $this->input->post('content');
    $this->date = time();

    $this->db->insert('entries', $this);
}

Here I especially like the last line, because, as @evnuh pointed out , the model is actually a row in the database.
In my case, the activities table has a destination_id field (link to the destinations table). And in the code, I wanted to implement it so that when the Activity is initialized, one of its properties would be the Destination object (already containing all the information).
How it is better to implement it? Achieve full correspondence "model = string in the database", or in the constructor of the activities_model model, initialize its property by creating an object of the destinations_model class?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evnuh, 2015-03-29
@vyshkant

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;

V
Vitaly Kamelin, 2015-03-30
@turboGadjet

For

// подгружаем класс работы с активностями
$this->load->library('activity');

// подгружаем класс работы с дестинейшенами
$this->load->library('destination');

// подгружаем класс работы с локациями
$this->load->library('location');

use autoload in configs.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question