1. Go to this page and download the library: Download alex-unruh/repository 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/ */
// MyRepo.php
use AlexUnruh\Repository;
class MyRepo extends Repository
{
protected $table_name = 'images';
protected $data_types = [];
/**
* Remember, we are extending the Query Builder class, so we use "$this" here
*/
public function lockRecord(int $status)
{
$id = uniqid(rand(), true);
$now = date('Y-m-d H:i:s');
$max_time = date('Y-m-d H:i:s', strtotime('+5 minutes', strtotime($now)));
$subquery = $this->select('uuid')
->distinct()
->from('another_table')
->where('status = ?')
->setMaxResults(1)
->getSQL();
$this->resetQueryParts();
$this->modify(['time_lock' => "'$max_time'", 'id_lock' => $id])
->where("id_lock = 0 OR time_lock < '{$now}'")
->andwhere('cancel = 0')
->andWhere('status = ?')
->andWhere("uuid IN ($subquery)")
->setParameter(0, $status)
->setParameter(1, $status);
return $this->execute() ? true : false;
}
}
// index.php
$repo = new MyRepo($connection_params);
$repo->lockRecord(1);
// index.php
// example 1
$user = new UserRepo($connection_params);
$result = $user->read(['name', 'username'])->where('id' => 5)->getFirst();
// example 2 (With table alias an join)
$user = new UserRepo($connection_params);
// second parameter "a" is the table alias to users table
$user->read(['a.name as author', 'b.*'], 'a')
->join('a', 'posts', 'b', 'b.author_id' = 'a.id')
->where('a.id = :id')
->setparameter('id', $id);
$result = $user->get();