| 1 |
www |
1 |
<?php
|
|
|
2 |
declare(strict_types=1);
|
|
|
3 |
|
|
|
4 |
namespace LeadersLinked\Mapper;
|
|
|
5 |
|
|
|
6 |
use LeadersLinked\Model\CompanyUser;
|
|
|
7 |
use LeadersLinked\Mapper\Common\MapperCommon;
|
|
|
8 |
use Laminas\Db\Adapter\AdapterInterface;
|
|
|
9 |
use Laminas\Log\LoggerInterface;
|
|
|
10 |
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
|
|
|
11 |
use LeadersLinked\Model\CompanyUserRole;
|
|
|
12 |
use Laminas\Db\Sql\Expression;
|
|
|
13 |
|
|
|
14 |
|
|
|
15 |
class CompanyUserRoleMapper extends MapperCommon
|
|
|
16 |
{
|
|
|
17 |
const _TABLE = 'tbl_company_user_roles';
|
|
|
18 |
|
|
|
19 |
|
|
|
20 |
/**
|
|
|
21 |
*
|
|
|
22 |
* @var CompanyUserRoleMapper
|
|
|
23 |
*/
|
|
|
24 |
private static $_instance;
|
|
|
25 |
|
|
|
26 |
/**
|
|
|
27 |
*
|
|
|
28 |
* @param AdapterInterface $adapter
|
|
|
29 |
*/
|
|
|
30 |
private function __construct($adapter)
|
|
|
31 |
{
|
|
|
32 |
|
|
|
33 |
parent::__construct($adapter);
|
|
|
34 |
}
|
|
|
35 |
|
|
|
36 |
/**
|
|
|
37 |
*
|
|
|
38 |
* @param AdapterInterface $adapter
|
|
|
39 |
* @param LoggerInterface $logger
|
|
|
40 |
* @param int $user_id
|
|
|
41 |
* @return \LeadersLinked\Mapper\CompanyUserRoleMapper
|
|
|
42 |
*/
|
|
|
43 |
public static function getInstance($adapter)
|
|
|
44 |
{
|
|
|
45 |
if(self::$_instance == null) {
|
|
|
46 |
self::$_instance = new CompanyUserRoleMapper($adapter);
|
|
|
47 |
}
|
|
|
48 |
return self::$_instance;
|
|
|
49 |
}
|
|
|
50 |
|
|
|
51 |
/**
|
|
|
52 |
*
|
|
|
53 |
* @param int $company_id
|
|
|
54 |
* @param int $user_id
|
|
|
55 |
* @return CompanyUserRole[]
|
|
|
56 |
*/
|
|
|
57 |
public function fetchAllByCompanyIdAndUserId($company_id, $user_id)
|
|
|
58 |
{
|
|
|
59 |
$prototype = new CompanyUserRole();
|
|
|
60 |
$prototype = new CompanyUserRole();
|
|
|
61 |
$select = $this->sql->select(self::_TABLE);
|
|
|
62 |
$select->where->equalTo('company_id', $company_id);
|
|
|
63 |
$select->where->equalTo('user_id', $user_id);
|
|
|
64 |
|
|
|
65 |
|
|
|
66 |
|
|
|
67 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
68 |
|
|
|
69 |
return $this->executeFetchAllObject($select, $prototype);
|
|
|
70 |
}
|
|
|
71 |
|
|
|
72 |
/**
|
|
|
73 |
*
|
|
|
74 |
* @param int $company_id
|
|
|
75 |
* @param int $user_id
|
|
|
76 |
* @param int $rolw_id
|
|
|
77 |
* @return CompanyUserRole[]
|
|
|
78 |
*/
|
|
|
79 |
public function fetchOneByCompanyIdAndUserIdAndRoleId($company_id, $user_id, $role_id)
|
|
|
80 |
{
|
|
|
81 |
$prototype = new CompanyUserRole();
|
|
|
82 |
$prototype = new CompanyUserRole();
|
|
|
83 |
$select = $this->sql->select(self::_TABLE);
|
|
|
84 |
$select->where->equalTo('company_id', $company_id);
|
|
|
85 |
$select->where->equalTo('user_id', $user_id);
|
|
|
86 |
$select->where->equalTo('role_id', $role_id);
|
|
|
87 |
|
|
|
88 |
|
|
|
89 |
|
|
|
90 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
91 |
|
|
|
92 |
return $this->executeFetchOneObject($select, $prototype);
|
|
|
93 |
}
|
|
|
94 |
|
|
|
95 |
/**
|
|
|
96 |
*
|
|
|
97 |
* @param int $company_id
|
|
|
98 |
* @param int $user_id
|
|
|
99 |
* @return int
|
|
|
100 |
*/
|
|
|
101 |
public function getCountByCompanyIdAndUserId($company_id, $user_id)
|
|
|
102 |
{
|
|
|
103 |
$select = $this->sql->select(self::_TABLE);
|
|
|
104 |
$select->columns(['total' => new Expression('COUNT(*)')]);
|
|
|
105 |
$select->where->equalTo('company_id', $company_id);
|
|
|
106 |
$select->where->equalTo('user_id', $user_id);
|
|
|
107 |
|
|
|
108 |
|
|
|
109 |
|
|
|
110 |
// echo $select->getSqlString($this->adapter->platform); exit;
|
|
|
111 |
|
|
|
112 |
$record = $this->executeFetchOneArray($select);
|
|
|
113 |
return $record['total'];
|
|
|
114 |
}
|
|
|
115 |
|
|
|
116 |
|
|
|
117 |
|
|
|
118 |
|
|
|
119 |
/**
|
|
|
120 |
*
|
|
|
121 |
* @param CompanyUser $companyUserRole
|
|
|
122 |
* @return boolean
|
|
|
123 |
*/
|
|
|
124 |
public function insert($companyUserRole)
|
|
|
125 |
{
|
|
|
126 |
$hydrator = new ObjectPropertyHydrator();
|
|
|
127 |
$values = $hydrator->extract($companyUserRole);
|
|
|
128 |
|
|
|
129 |
$values = array_filter($values, function($value) {
|
|
|
130 |
return !empty($value);
|
|
|
131 |
});
|
|
|
132 |
|
|
|
133 |
$insert = $this->sql->insert(self::_TABLE);
|
|
|
134 |
$insert->values($values);
|
|
|
135 |
|
|
|
136 |
return $this->executeInsert($insert);
|
|
|
137 |
}
|
|
|
138 |
|
|
|
139 |
/**
|
|
|
140 |
*
|
|
|
141 |
* @param int $id
|
|
|
142 |
* @return boolean
|
|
|
143 |
*/
|
|
|
144 |
public function delete($id)
|
|
|
145 |
{
|
|
|
146 |
$delete = $this->sql->delete(self::_TABLE);
|
|
|
147 |
$delete->where->equalTo('id', $id);
|
|
|
148 |
|
|
|
149 |
return $this->executeDelete($delete);
|
|
|
150 |
}
|
|
|
151 |
|
|
|
152 |
/**
|
|
|
153 |
*
|
|
|
154 |
* @param int $company_id
|
|
|
155 |
* @param int $user_id
|
|
|
156 |
* @param int $role_id
|
|
|
157 |
* @return boolean
|
|
|
158 |
*/
|
|
|
159 |
public function deleteByCompanyIdAndUserIdAndRoleId($company_id, $user_id, $role_id)
|
|
|
160 |
{
|
|
|
161 |
$delete = $this->sql->delete(self::_TABLE);
|
|
|
162 |
$delete->where->equalTo('company_id', $company_id);
|
|
|
163 |
$delete->where->equalTo('user_id', $user_id);
|
|
|
164 |
$delete->where->equalTo('role_id', $role_id);
|
|
|
165 |
|
|
|
166 |
//echo $delete->getSqlString($this->adapter->platform);
|
|
|
167 |
|
|
|
168 |
return $this->executeDelete($delete);
|
|
|
169 |
}
|
|
|
170 |
|
|
|
171 |
/**
|
|
|
172 |
*
|
|
|
173 |
* @param int $company_id
|
|
|
174 |
* @param int $role_id
|
|
|
175 |
* @return boolean
|
|
|
176 |
*/
|
|
|
177 |
public function deleteByCompanyIdAndRoleId($company_id, $role_id)
|
|
|
178 |
{
|
|
|
179 |
$delete = $this->sql->delete(self::_TABLE);
|
|
|
180 |
$delete->where->equalTo('company_id', $company_id);
|
|
|
181 |
$delete->where->equalTo('role_id', $role_id);
|
|
|
182 |
|
|
|
183 |
return $this->executeDelete($delete);
|
|
|
184 |
}
|
|
|
185 |
|
|
|
186 |
}
|