Models in MVC in PHP
Let's now understand models. Models,
like controllers, are
OOP classes. They should be placed in the folder
/project/models/. Let's in this folder
in the file Page.php create a model Page
responsible for the data of the pages of our site:
<?php
namespace Project\Models;
use \Core\Model;
class Page extends Model
{
}
?>
As you can see, our model inherits from the class
\Core\Model. From this class, our
model inherits the protected methods findOne
and findMany. The method findOne as a parameter
takes an SQL query and returns the first
record from the database that fell into the query result.
The method findMany also takes an SQL
query, but returns an array of records.
At the same time, you don't need to write code that establishes
a connection to the database, or process the results
of the query via mysqli_fetch_assoc.
The framework does it for you. You just need
to choose whether you expect one record from the query
from the database or several and use the appropriate
function, getting as a result a sane array
that you can simply take and use
further.