I'm trying to figure out how to properly work this out. I don't know if I'm applying things correctly. I think it's a good thing to separate the User object, from the database operations, meaning that User class shouldn't know how to operate with the DB. Thats why I created another class UserModel (I'm not sure of how to name this class) which will be the one handling the db queries and returning their results. Before, I was selecting, inserting, updating and deleting from the User class, but I felt like that responsability didn't belong to that class. Why a User should return a set of Users? That didn't make sense to me. I also got doubts when using the database, as I don't know how to properly use PDO.
(In this example I've only implemented the Insert query, the other methods are implemented the same way.)
Let's get started.
User.php
class User {
private $id;
private $name;
private $email;
private $username;
private $password;
//Getters & Setters
}
UserModel.php (Should this be called UserModel?)
class UserModel {
private $db;
private $pdo;
public function __construct()
{
$db = new Database;
$pdo = $db->getPDO();
}
public function createUser($user)
{
$sql = 'INSERT INTO users (`name`,`email`,`username`,`password`) VALUES(:name, :email, :username, :password)';
$pdo->prepare($sql);
$pdo->bindValue(':name', $user->getName(), PDO::PARAM_STR);
$pdo->bindValue(':email', $user->getEmail(), PDO::PARAM_STR);
$pdo->bindValue(':username', $user->getUsername(), PDO::PARAM_STR);
$pdo->bindValue(':password', $user->getPassword(), PDO::PARAM_STR);
return $pdo->execute();
//return true if inserted, return false if not
}
public function editUser($user)
{
}
public function deleteUser($uid)
{
}
public function getUser($uid)
{
}
public function getAllUsers()
{
}
}
Database.php
class Database {
private $pdo;
public function __construct()
{
$pdo = new PDO('mysql:host=localhost;dbname=testdb;charset=utf8', 'username', 'password');
}
public function getPdo() {
return $this->pdo;
}
}
users.php
$usermodel = new UserModel;
$user = new User;
$user->setName('John');
$user->setEmail('john@john.com');
$user->setUsername('john');
$user->setPassword('1234');
$usermodel->createUser($user);
Looks good to me as a custom based user model solution. These work awesome for small projects since they're so lightweight and you're able to customize each to your taste.
As you scale up you'll notice this has to be done/customized for each database table and if you have joins... things may get hairy to say the least.
I would recommend you take a look at the ORMs used within major php frameworks: zend, laravel, cake among others.
The ORM essentially does auto-mapping based on table name/primary key. Assuming you're ok with SOME 'magic' and losing full control over data in and data out you will have the same ability you've created for the user object in any other table you add by setting the two defaults I mentioned.
On the downside is the performance hit you'll experience. Here's some stats on the line count/file count of the frameworks I mentioned: