1. Go to this page and download the library: Download mfurman/pdomodel library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
mfurman / pdomodel example snippets
$config = [
'DB_CONNECTION' => 'mysql',
'DB_HOST' => 'localhost',
'DB_PORT' => '3306',
'DB_DATABASE' => 'testdb',
'DB_USERNAME' => 'access',
'DB_PASSWORD' => 'password',
'LOGGER_DIR' => 'logger directory started from base directory of project (default "./errors")',
];
\mfurman\pdomodel\PDOAccess::get($config);
use \mfurman\pdomodel\PDOAccess;
use \mfurman\pdomodel\dataDb;
class User extends dataDb
{
private $users_table = 'users';
public function __construct($commit=true)
{
parent::__construct(PDOAccess::get(), $this->users_table, $commit, false);
}
// return associative array, table of users
public function getAll() :array
{
return $this->read('*')->get();
}
// return associative array, users data
public function getById(int $id) :array
{
return $this->read('*','id = '.$id)->get();
}
// return associative array, users data
public function getByName(string $name)
{
return $this->read('*','user_name = "'.$name.'"')->get();
}
// return id of new row,
public function add(array $data) :int
{
$this->set(array('user_name'=>$data['user_name']));
return $this->insert();
}
// return true or false
public function updateOne(array $data, int $id) :bool
{
if (empty($this->read('*','id = '.$id)->get())) return false;
$this->set(array('user_name'=>$data['user_name']));
$this->update($id);
return true;
}
public function deleteById(int $id) :bool
{
if (empty($this->read('*','id = '.$id)->get())) return false;
$this->delete_where('id = '.$id);
return true;
}
}
use Myvendor\Myapp\Models\Task;
use Myvendor\Myapp\Models\User;
class TaskRepository
{
private $task;
private $user;
(...)
public function store(array $data) :array
{
$this->task = new Task(false); // - set autocommit to false (true/false)
$this->user = new User(false); // - set autocommit to false (true/false)
// inform PDO about start transaction
$this->task->begin();
$this->user->getByName($data['user_name']);
if ($this->user->is()) $data['user_id'] = $this->user->get('id');
else {
$this->user->reset();
$data['user_id'] = $this->user->add($data);
}
$id = $this->task->add($data);
// inform PDO to commit ealier started transaction, if something go wrong - commit will rollback
$this->task->commit();
// this toggle change model to autocommit transactions (true/false)
$this->task->set_commit(true);
return $this->task->getById($id);
}
$_SESSION['user_id'] = 12 // set when user log in - for eg ID from users table,
$_SESSION['user_name'] = 'John Smith' // set when user log in - username from users table,
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.