-
Notifications
You must be signed in to change notification settings - Fork 1
/
RepositoryBase.php
88 lines (77 loc) · 1.88 KB
/
RepositoryBase.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
<?php
namespace entityfx\utils;
use entityfx\utils\enum\EnumBase;
use entityfx\utils\enum\OrderDirectionEnum;
use Yii;
use yii\base\Component;
use yii\db\Connection;
use yii\db\Query;
/**
* Class RepositoryBase
*
* @author EntityFX <artem.solopiy@gmail.com>
* @package Kontinent\Components\Common
*/
abstract class RepositoryBase extends Component {
const REPOSITORY_CATEGORY = 'app.components.repositories';
/**
*
* @var Connection
*/
protected $db;
public function __construct()
{
parent::__construct();
$this->db = Yii::$app->db;
}
/**
* Returns ORDER BY token for field enum and order enum
*
* @param EnumBase $orderField Field to order
* @param OrderDirectionEnum $direction Order direction
*
* @return string
*/
protected function order(EnumBase $orderField, OrderDirectionEnum $direction)
{
return $orderField->getValue() . ' ' . $direction->getValue();
}
/**
* Returns count items in table
*
* @param string $table SQL table name
* @param mixed $where where condition
* @param array $params parameters to bind in
* @return int Count items
*/
protected function getCount($table, $where, array $params)
{
return (int)((new Query())
->from($table)
->where($where, $params)
->count());
}
protected function getCountByCommand($tableName, Query $command)
{
$command->from($tableName);
return (int)$command->count();
}
/**
* Returns id of inserted record
*
* @return int
*/
protected function lastInsertId()
{
return (int)$this->db->getLastInsertID();
}
/**
* Возвращает GUID
*
* @return Guid
*/
protected function createGuid()
{
return Guid::generate();
}
}