Download the PHP package gaetanroger/minimal-php-framework without Composer
On this page you can find all versions of the php package gaetanroger/minimal-php-framework. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download gaetanroger/minimal-php-framework
More information about gaetanroger/minimal-php-framework
Files in gaetanroger/minimal-php-framework
Package minimal-php-framework
Short Description Minimal PHP framework
License MIT
Informations about the package minimal-php-framework
Minimal PHP framwork
Concept
General idea
This minimal framework is meant to be used for a Model & Manager PHP based application.
- Model: class that represents one key data object of your application
- Manager: singleton class managing all instances of one model
If your application is about cars, you could have the following classes and methods:
With the framework
This minimal framework defines two classes for you: AbstractModel
and AbstractManager
.
Both predefine methods that every model and manager need, like Manager.find(int)
,
Manager.findAll()
, Model.getId()
, Model.insert()
, etc. (please find all defined methods in Methods list down below).
This framework makes data persistence easy by using properties and class names to match database table names.
For example, if a model is named AwesomeCar
the framework will look for a table named awesome_car
.
How to install
This project is available as a Composer dependency. Please use the following command:
How to use
At the beginning of your application, please set the PDO connection in the model and manager like this:
This will allow the model to update and insert itself into the database, and the manager to find models (from an ID for example). Plus, all your models and manager will be able to easily access database.
Then simply make all your models and managers inherit from Gaetanroger\MinimalPhpFramework\AbstractModel
and
Gaetanroger\MinimalPhpFramework\AbstractManager.
Be careful to follow naming conventions specified down below, as names are by default
dynamically used by the framework.
Conventions to follow
Model
- A model class name must use CamelCase;
- A model class name should not end with
Model
(except if it is part of the actual model name); - The model class name must be the exact conversion from snake_case to CamelCase of the table name.
Valid model names with their table name:
Invalid model names:
Manager
- A manager class name must use CamelCase;
- A manager class name must end with
Manager
; - A manager class name must not contain
Manager
anywhere else; - The manager class name must be identical with the model name plus the word
Manager
.
Valid manager names with their model name:
Invalid manager names:
Properties and methods list
AbstractModel
- $id: ID of the model. The database field must be set to auto increment, as the framework uses this value to check if the model is already inserted into database or not.
- insert(): Create a now row in the table named after the model class by using the model properties as field names.
- update(): Same as
insert()
but updates the fields values of the row matching the model ID. - delete(): Delete the table row matching the model ID.
- getId(): Returns model's ID.
- getTableName(): Returns the model table name by converting the class name to snake_case. This method can be overridden if a custom table name must be used.
AbstractManager
- find(id): Returns the model matching the ID from database. If no model matches, an exception is thrown.
- findWhere(conditions): Returns the model filling the conditions. All the conditions are strict equality
checks (
=
sign). If a different condition check is necessary, feel free to implement a new method. - findAll(): Returns all models managed by this manager.
- getTableName(): Returns the model table name by converting the class name (without the word
Manager
) to snake_case. This method can be overridden if a custom table name must be used. - getModelName(short): Returns the model name by trimming the word
Manager
from the class name. Ifshort
is false, will return full class name (package/model). This method can be overridden if a custom manager name must be used. - count(column, distinct): Counts rows in table using SQL
COUNT
. Default parameters will simply count all rows but you can specify what column you want to count and if you want it to only count distinct values.
Changelog
Please take a look at CHANGELOG.MD
Please criticize
Please feel free to contact me here or by email if something bothers you or does not work as expected.