https://github.com/wdes/simple-php-model-system
A simple PHP model system
https://github.com/wdes/simple-php-model-system
active-record active-record-design-pattern composer-package model mysql pdo-mysql php simple
Last synced: 2 months ago
JSON representation
A simple PHP model system
- Host: GitHub
- URL: https://github.com/wdes/simple-php-model-system
- Owner: wdes
- License: mpl-2.0
- Created: 2021-05-22T10:28:55.000Z (about 5 years ago)
- Default Branch: main
- Last Pushed: 2024-07-04T15:51:45.000Z (almost 2 years ago)
- Last Synced: 2025-12-14T16:33:51.633Z (7 months ago)
- Topics: active-record, active-record-design-pattern, composer-package, model, mysql, pdo-mysql, php, simple
- Language: PHP
- Homepage: https://packagist.org/packages/wdes/simple-php-model-system
- Size: 104 KB
- Stars: 1
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# simple-php-model-system
[](https://github.com/wdes/simple-php-model-system/actions/workflows/lint-and-analyse.yml)
[](https://github.com/wdes/simple-php-model-system/actions/workflows/tests.yml)
[](https://codecov.io/gh/wdes/simple-php-model-system)
[](https://packagist.org/packages/wdes/simple-php-model-system)
[](https://packagist.org/packages/wdes/simple-php-model-system)
[](https://packagist.org/packages/wdes/simple-php-model-system)

A simple PHP model system
## Why ?
The goal of this project is to provide an easy way to use Models in a non composer setup.
This is why this project is kept very minimal.
The user can copy all classes in `src/` and start using the lib.
But this also works in a composer setup.
## How to use
### Connect to an existing PDO connection
```php
setConnection($em->getConnection()->getNativeConnection());// Wants a PDO connection object class
```
### Connect with config
```php
connect();// Will throw an exception if not successfull
```
### Manage models
```php
query(
'SELECT 1'
);
$this->data = [];
while (($row = $statement->fetch(PDO::FETCH_ASSOC)) !== false) {
$this->data[] = $row;
}
var_dump($this->data);
// With a model
$user1 = User::create(
'5c8169b1-d6ef-4415-8c39-e1664df8b954',
'Raven',
'Reyes',
null
);
$user1->save();// If you forget this line, it will only exist in memory
$user1 = User::findByUuid('5c8169b1-d6ef-4415-8c39-e1664df8b954');// Find the user back
$user1->toArray();
//[
// 'user_uuid' => '5c8169b1-d6ef-4415-8c39-e1664df8b954',
// 'first_name' => 'Raven',
// 'last_name' => 'Reyes',
// 'date_of_birth' => null,
//]
$user1->refresh(); // Get it back from the DB
$user1->setLastName('last_name', 'Ali-2');// Change an attribute value (build your own setters using the example)
$user1->update();// Update it
$user1->delete();// Delete it
User::deleteAll();// Delete all
// And more functions, see AbstractModel class
```