🔥 Welcome to the SimpleKit! an open-source PHP framework that is meticulously crafted to harness the power of SimpleORM, offering developers a seamless experience in building robust web applications. 🌐 Dive deep into mastering intricate Object-Relational Mapping nuances🛠.
This readme serves as the official documentation to this project. All information about how to
get started using SimpleKit with SimpleORM is included in this readme.
Why should you use SimpleKit too? because it's blazingly fast. Plus it's open source and easy
to add new features to it as you wish in order to tailor it to your own needs as a developer.
Follow these steps to install SimpleKit on your system:
- Ensure you have Git installed on your machine.
- Make sure you have Node.js and npm installed.
- Ensure Composer is installed on your system.
Firstly, clone the SimpleKit repository to your local machine using Git:
git clone https://github.com/yassirelkhaili/SimpleKit.git
Navigate to the cloned directory:
cd simplekit
Inside the project directory, install the necessary npm packages:
npm install
Next, require the Composer dependencies by executing the following command:
composer install
3. Start up your apache server (Laragon, Xamp or Wamp) and set the host directory to the project's root
4. Navigate to http://localhost/books for a demo crud project
5. Dont forget to setup the database connection (check the establishing connection guide down below for more info)
6. Dont forget to import the database sql or create a new schema in your database using SimpleORM Entities/Migrations, The default sql required for the demo crud project is located in the project's root directory
npm start
SimpleKit includes a simple command line interface called well... simple. It supports multiple
commands that make using SimpleKit seamless.
- help
This command prints a list of all current commands supported by simple.
Example use:
php simple help
- generate
This command allows developers to create Models, Controllers or Entities serving multiple purposes within the application's architecture.
1. **Models**: Models acts as an intermediary between the Database and the controller, harnessing the power of SimpleORM to enable efficient data manipulation and retrieval operations.
Default location: SimpleKit/Models/
2. **Controllers**: Controllers act as intermediaries between the model and view components of the application, facilitating the processing of user requests and directing the flow of data, they call Model methods and render the appropriate Views.
Default location: SimpleKit/Controllers/
3. **Entities**: Essentially, entities function as migrations or schemas that dictate the structure and attributes of database tables.
Default location: SimpleKit/Database/Migrations
Example use:
php generate:model User
php generate:controller UserController
php generate:entity Users
- migrate
This command migrates an entity from your Migrations folder to your database.
Example use:
php migrate:entity Users
- destroy
This command deletes a Model, Controller or Entity from the applications structure.
Example use:
php destroy:model User
- rollback
This command reverses a migration simply dropping which ever table name you give it.
Example use:
php rollback:entity Users
Entities are simply classes that represent tables.
Attributes are first set by default when you generate a class and are optional.
You can edit/delete them or make your own.
Every Entity has its own getPropertyConfig method.
This is SimpleKit gets all the information about how it should create the table in the database.
- Important:
If SimpleKit doesn't find the getPropertyConfig method defined inside the entity class it will
proceed to simply generate the table using the properties instead using default values for each
column. (experimental)
-
supported column types:
-
type
-
length
-
notNull
-
autoIncrement
-
primary
-
unique
Example Use:
public static function getPropertyConfig(): array {
return [
'id' => ['type' => 'int', 'primary' => true, 'autoIncrement' => true, 'notNull' => true],
'name' => ['type' => 'varchar', 'length' => 255, 'notNull' => false],
'email' => ['type' => 'varchar', 'length' => 255, 'notNull' => true, 'unique' => true],
'lastname' => ['type' => 'varchar', 'length' => 23, 'notNull' => false, 'unique' => true],
'userID' => ['type' => 'int', 'notNull' => false, 'unique' => true],
];
}
SimpleKit offers a simple way to establish a connection to your database.
Simply fill up the fields in the .env.db file in the project root with your database
credentials and you are set.
Examle use:
DRIVER = "mysql"
DB_HOST = "localhost"
DB_PORT = "3301"
DB_NAME = "ormtest"
DB_USER = "root"
DB_PWORD = ""
Routers are responsible for the SimpleKit applications's routes.
They take a route URI, the controller responsible for the route and the corresponding method.
Check the BaseRouter for the background code.
Default Location: SimpleKit/Routers/
SimpleKit features a number of helper classes and functions that serve many perposes vital to the flow of a SimpleKit application.
Default Location: SimpleKit/Helpers/
A function typically used in Controllers to redirect to the Views and is capable of creating session data for example to send a comfirmation message to the user.
Example use:
public function store() {
// Redirect back to the index page with a success message (or handle differently based on your needs)
// You can also render a view or return a JSON response
return redirect('/books')->with(['success' => 'book created successfully!']);
}
A class Used to get POST data from a form in controllers before storing it in the database using the corresponding model.
Example use:
public function store(Request $request) {
$data = $request->getPostData();
// Create a new book using the books
$this->books->create($data);
}
SimpleORM, is an open source PHP ORM designed by me to be used in future projects as well as master the behind the scene fundamentals of Object-Relational Mapping, advanced PHP OOP concepts, and design patterns. 📚 This project serves as a practical playground for me to further understand how ORMs work behind the scenes.
SimpleORM uses an Entity Mapper which is a class that handles entity mapping in
order to get all of its properties and types that way SimpleORM knows how to create
the appropriate database table.
Default location: SimpleKit/SimpleORM/MigrationMapper.php
SimpleORM uses an Entity Generator which is a class that handles entity generation
and puts it in the Migrations folder.
Default location: SimpleKit/SimpleORM/EntityGenerator.php
SimpleORM uses a Query Generator which is simply the chef that cooks up all
of the queries and serves them to the Entity Manager.
Default location: SimpleKit/SimpleORM/QueryGenerator.php
SimpleORM uses an Entity Manager which is the class you will be interacting with
almost all the time. It's simply the server that takes your order to the kitchen
then serves you the food. and by order I mean SimpleORM methods/queries and by your
food I mean data/records from the database.
Default location: SimpleORM/SimpleORM/EntityManager.php
require_once "./vendor/autoload.php";
use SimpleKit\SimpleORM\EntityManager;
Required parameters:
- $conn object from SimpleORM/src/Database/connections/conn.php This is autimatically
included when you require the Entity Manager
- entity/database table name as string
$entity = new EntityManager("Users");
- Individual insert
Example use:
$entity->email = "example@gmail.com";
$entity->name = "name";
$entity->lastname = "lastname";
$entity->userID = 21;
$entity->save();
- Batch insert
Required parameters:
- Array of records as associative arrays with column names keys pointing to values
Example use:
$entity->saveMany([
["name"=> "nam eqwd","email"=> "email@gmail.com", "lastname" => "lastname"],
["name"=> "nameqwd2","email"=> "email2@gmail.com", "lastname" => "lastname"],
["name"=> "nameqwd3","email"=> "email3@gmail.com", "lastname" => "lastname"],
]);
- Fetch records
Example use:
$entity->fetchAll()->get(); // fetches all records from the database table
- paginate
Limits the number of records to be fetched
Required parameters:
- value: integer
Example use:
$entity->fetchAll()->get(int number);
- where
Specifies which records should be fetched
Required parameters:
- column name: string
- value: any
Example use:
$entity->fetchAll()->where("name", "exampleName")->get();
$entity->fetchAll()->where("name", "exampleName")-where("email", "exampleEmail")->get(number);
- Delete records
Example use:
$entity->delete()->confirm(); // deletes all records from the database table
Note:
This method can be chained with where to delete specific table records
Example use:
$entity->delete()->where("id", 51)->confirm(); //deletes record which id = 51
- Individual records
Required parameters:
- column name: string
- value: any
Example use:
$entity->update("name", "rand")->where("id", 51)->confirm();
// updates name to rand for record with id = 51
- Multiple records
Required parameters:
- Associative array with table columns as keys
Example use:
$entity->update([
"name" => "random name",
"email" => "random email",
"lastname" => "random lastname",
])->where("id", 52)->confirm();
// updates name, email and lastname for record with id = 51
- Count
Returns table record count
Example use:
$entity->count(); //counts all records
- Order By
Displays table records in order
Required parameters:
- Array of columns to be ordered by
- Order by method: ASC or DESC (optional)
Note:
The second parameter is optional and is set to ASC by default
Example use:
$entity->fetchAll()->where("id", 51)->orderBy(["userID"], "DESC")->get();