Help with Codeigniter and MVC model pattern - model-view-controller

I am creating a site much like a wordpress blog whereby the front page will display a post loop: with the post_summary, author info, and tags.
I have four tables:
posts | users | tags | tag relationships
to display all the results i would need to do multiple JOINs for in the SELECT statement
However, to stay with the MVC pattern, there should be a model for each table ( or object ?). So my question is: If I were doing a SELECT all, how would I do this and still keep with the MVC pattern?
To get all the required info for the post, I need the author_id to get my info from the users table AND I need the post_id to get the tags (and so on). If all of my queries are in different Models, what is the best way to perform the query?
Do I make one Model that does all of the JOINS and just use it? Should I load Models from the view? Or should I do additional query work in the Controller?

I think you have a misunderstanding of the purpose of Models. Models are to deal with data in your database and are not limited to 1 table per model. If you are creating a blog, you will really just need one model. Take a look at the tutorial on the codeigniter website, http://codeigniter.com/tutorials/watch/blog/, and reread the user guide for models, http://codeigniter.com/user_guide/general/models.html .
You may be getting MVC confused with an ORM

Do not make a model for the joins. As answered by #Johnny already, a Model and a table do not need to have a one-to-one relationship. In this case you are displaying blog entries, so you could have a Model named "Blog", with a method "GetList()". It is not relevant whether that query reaches out to multiple tables.
Think about it conceptually. Your are displaying blog entries, and each blog entry has other objects associated to it (such as a user id). Try to think domain-driven, not table-driven.

Make a model for the JOINS. It can include post_summary, recent_comments, etc.
Just use it in the front_page controller, the side_bar controller (for recent_comments, etc).
It would be better not to put query work directly in views or controller and views should not need to access the models IMO.

Related

Models responsability

I have a doub about Laravel. The models are used to define the relationships between the models like hasMany, belongsTo, etc. Also the models are used to define the fillable fields. But he models are only for that? Because I already check some examples that it seems that some queries are executed in the models instead of the controller so Im not understanding if the models should also have the querying of the relationships or not. Can you give a help to understand better what is the correct use of models (what should be placed in the models)?
Its same way to execute queries on model or controller. Written queries in model make your controller more clean. We can write mutator, accessor or query scope in eloquent model. Correct me if I'm wrong.
Visit https://laravel.com/api/5.5/Illuminate/Database/Eloquent/Model.html
You can refer this documentation, it's quite helpful if you want to know deep basics and responsibilities about particular part in Laravel.
Models are mostly used to make an outlook of the data i.e what fields are going to be saved in the database and we also use it to associate the relationships with other related data as you already know but we also use it to alter the values that are either going in/out of the data base which you can check in the documentation in link bellow https://laravel.com/docs/5.6/eloquent-mutators
the main purpose is to divide the code between controller and model (were it best fit to be)

Laravel: What are models?

I'm confused as to what models are and do in Laravel. I've tried to find some explanations but couldn't find any.
Can someone briefly explain what models are, when I would use them, and why I should use them?
More so, what are fillable and guarded attributes? I don't find they're very well explained in the docs.
For example, I have a table in my database, called login_log, that contains all login attempts. Would I create a model for this? Why?
Model is represented by M when you talk about MVC which stands for Model, View and Controller.
In Laravel Model is simply your database table object. This allows you to interact with your database tables as if they are PHP objects or classes.
Fillable property is used to tell laravel to allow mass assignment for the listed fields
while Guard property is the opposite of fillable
Laravel documentation is the best documentation so far.
See this Links to Understand well : Mass Assignment in Eloquent ORM for Laravel 4.2
Suggestions:
If you are newbie in Laravel, as I am Android Application Developer
i've find solution and understood too.
You have to learn documentation before putting question.
As MVC stands for Model View Controller, Model Deals with Database for example controller ask to Model to give me the first names of Students from the student table an d then controller pass it to the view.

Model with multiple tables

We run two websites, A and B. Each website has its own table, _a_ and _b_ which have exactly the same structure. Yes, I know it's silly, we'll be rewriting them over the course of this year and next.
Using Laravel I need to create a model that will hold both tables content. I don't need any kind of UPDATE or INSERT functionality, I just need to SELECT and use with to access other model information.
Is this possible with Laravel 4.1? I can individually model each table, but that would make it difficult in the future.
I was able to fix this by making using the Repository pattern and merging the results of each model into the get and all functions.

CodeIgniter MVC Model Logic

I have programmed in Rails, Django, Zend, and CakePHP. Also, Wordpress and Drupal.
Now, I am "catching up to speed" in as fairly large application in CodeIgniter.
Typically, my experience with MVC frameworks, has led me to believe that Models represent business logic in reference to actual database tables. In the CodeIgniter docs and in the code base I'm dissecting I see models created that represent something as vague as a page. A lot of the business logic is written directly into those models and they incorporate other actual data models. I'm not sure this is ideal and following MVC.
Should models be created beyond data models?
Also, lets say I have a data model representing a user (user table in DB). In that user table there is a column called gender of the type enum('male', 'female'). Now I want to populate a dropdown with the gender options from the enum column.
Where is it most appropriate to put this logic?
The user model is an object used to represent a single user or row in the db... right? So it doesn't seem fitting to include a function in the user model/class called "get_gender_options", because, while the function is related to the user table, it is NOT relevant to a single user object. In Zend this kind of logic could be built into the form object itself.
There is not "right" answer, just one we can consider the most appropriate...
I would probably just put the "get_gender_options" in the model, rather than sticking it in the view for the form. To keep it DRY but not put it in the model, I would create a helper to hold this.

how to use codeigniter database models

I am wondering how the models in code ignitor are suposed to be used.
Lets say I have a couple of tables in menu items database, and I want to query information for each table in different controllers. Do I make different model classes for each of the tables and layout the functions within them?
Thanks!
Models should contain all the functionality for retrieving and inserting data into your database. A controller will load a model:
$this->load->model('model_name');
The controller then fetches any data needed by the view through the abstract functions defined in your model.
It would be best to create a different model for each table although its is not essential.
You should read up about the MVC design pattern, it is used by codeigniter and many other frameworks because it is efficient and allows code reuse. More info about models can be found in the Codeigniter docs:
http://codeigniter.com/user_guide/general/models.html
CodeIgniter is flexible, and leaves this decision up to you. The user's guide does not say one way or the other how you should organize your code.
That said, to keep your code clean and easy to maintain I would recommend an approach where you try to limit each model to dealing with an individual table, or at least a single database entity. You certainly want to avoid having a single model to handle all of your database tables.
For my taste, CodeIgniter is too flexible here - I'd rather call it vague. A CI "model" has no spec, no interface, it can be things as different as:
An entity domain object, where each instance represents basically a record of a table. Sometimes it's an "anemic" domain object, each property maps directly to a DB column, little behaviour and little or no understanding of objects relationships and "graphs" (say, foreign keys in the DB are just integer ids in PHP). Or it can also be a "rich (or true) domain object", with all the business intelligence, and also knows about relations: say instead of $person->getAccountId() (returns int) we have $person->getAccount(); perhaps also knows how to persist itself (and perhaps also the full graph or related object - perhaps some notion of "dirtiness").
A service object, related to objects persistence and/or general DB querying: be a DataMapper, a DAO, etc. In this case we have typically one single instance (singleton) of the object (little or no state), typically one per DB table or per domain class.
When you read, in CI docs or forums, about , say, the Person model you can never know what kind of patter we are dealing with. Worse: frequently it's a ungly mix of those fundamentally different patterns.
This informality/vagueness is not specific to CI, rather to PHP frameworks, in my experience.

Resources