subztep / modell
Model class for PDO
Installs: 18
Dependents: 0
Suggesters: 0
Security: 0
Stars: 2
Watchers: 3
Forks: 1
Open Issues: 0
pkg:composer/subztep/modell
This package is not auto-updated.
Last update: 2020-08-21 20:12:02 UTC
README
A PHP class for easily create/update/load database entries. All you need to do is extend this class from your model and enjoy the benefits. It uses PDO connection but I only tested with MySql. I am going to explain the usage with a simple user class.
Please write your tests well.
Installation
Extend your composer.json file with the following:
{
"require": {
"subztep/modell": "dev-master"
}
}
Create data table
Our user table will contains name and email. Each plural table require an id int primary key field. Optional created_at and updated_at datetime columns for log your updates, recommended.
CREATE TABLE `users` ( `id` int(10) unsigned NOT NULL AUTO_INCREMENT, `name` varchar(30) NOT NULL, `email` varchar(255) DEFAULT NULL, `created_at` datetime DEFAULT NULL, `updated_at` datetime DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
Usage with PHP
Connect to database
Modell::$pdo = new PDO('mysql:host=HOST;dbname=DBNAME;charset=utf8', 'USER', 'PASS');
Connect to memcache is optional. If connected, Modell cache your table's column details and make it faster.
Modell::$memcache = new Memcache; Modell::$memcache->connect('localhost', 11211);
Create table users with primary key id, and add Modell class to your project
class User extends Modell { }
Furthermore, you can run any sql query, connection in singleton.
$query = Modell::$pdo->prepare($sql);
Examples
Create user
$user = new User();
$user->name = 'John Doe';
$user->save();
Load user by id
$user = new User(1);
echo $user->name;
Update user by id
$user = new User(1);
$user->name = 'John Roe';
$user->save();