lodev09 / php-models
A PHP library that allows you to easily create and define your models using PDO.
Installs: 5 030
Dependents: 1
Suggesters: 0
Security: 0
Stars: 3
Watchers: 2
Forks: 2
Open Issues: 1
Requires
- php: >=5.4
This package is auto-updated.
Last update: 2024-10-17 22:28:37 UTC
README
A PHP library that allows you to easily create and define your models using PDO
Features
- PDO wrapper
FETCH_INTO
made easy- Model helper
Installation
$ composer require lodev09/php-models
Usage
DB.php
The main \Models\DB
class is a PDO wrapper used to make CRUD much easier. It is a forked code from the php-pdo-wrapper-class
// connect to your database. Store the $db instance globally -- you only need to connect to your db ONCE! $db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD);
Available CRUD methods
$db->insert($sql, $binds)
or$db->insert($table, $values)
$db->select($sql, $binds)
$db->row($sql, $binds)
(same withselect
but will return single row)$db->update($sql, $binds)
or$db->update('table', $values)
$db->delete($sql, $binds)
or$db->delete('table', $filters)
The default style is PDO::FETCH_OBJ
Example:
$users = $db->select("SELECT * FROM users WHERE active = 1 AND username = :username", array('username' => 'lodev09')); var_dump($users);
Model.php
The \Models\Model
class is a parent class that can be inherited to a Model class. Inheriting this class allows you to automatically map the result "row" into your model class (table). This class basically uses the PDO::FETC_INTO
style and made it easier for you. Here are the steps to link your table into a class:
- Initiate the
\Models\DB
instance (see above)
$db = new \Models\DB(DB_HOST, DB_NAME, DB_USER, DB_PASSWORD); \Models\Model::setDb($db);
- Create your model class. For example, a
User.php
class
namespace Models; class User extends Model { public function getName() { return $this->name; } }
- Register your table to your custom class
// somewhere in your init.php \Models\User::register('users');
Now, you can directly get the User
instance from a query. Example:
$user = \Models\User::row("SELECT id, name FROM users WHERE id = 1 AND active = 1"); // you can call the getName() method now if ($user) { $name = $user->getName(); echo 'His name is '.$name; }
Feedback
All bugs, feature requests, pull requests, feedback, etc., are welcome. Visit my site at www.lodev09.com or email me at lodev09@gmail.com
Credits
© 2018 - Coded by Jovanni Lo / @lodev09
License
Released under the MIT License. See LICENSE file.