Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 2085 | Ir a la última revisión | | Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
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 Laminas\Db\Sql\Expression;
12
 
13
 
14
class CompanyUserMapper extends MapperCommon
15
{
16
    const _TABLE = 'tbl_company_users';
17
 
18
 
19
    /**
20
     *
21
     * @var CompanyUserMapper
22
     */
23
    private static $_instance;
24
 
25
    /**
26
     *
27
     * @param AdapterInterface $adapter
28
     */
29
    private function __construct($adapter)
30
    {
31
        parent::__construct($adapter);
32
    }
33
 
34
    /**
35
     *
36
     * @param AdapterInterface $adapter
37
     * @param LoggerInterface $logger
38
     * @param int $user_id
39
     * @return \LeadersLinked\Mapper\CompanyUserMapper
40
     */
41
    public static function getInstance($adapter)
42
    {
43
        if(self::$_instance == null) {
44
            self::$_instance = new CompanyUserMapper($adapter);
45
        }
46
        return self::$_instance;
47
    }
48
 
49
    /**
50
     *
51
     * @param int $company_id
52
     * @return CompanyUser[]
53
     */
54
    public function fetchAllByCompanyId($company_id)
55
    {
56
        $prototype = new CompanyUser();
57
        $select = $this->sql->select(self::_TABLE);
58
        $select->where->equalTo('company_id', $company_id);
59
 
60
        return $this->executeFetchAllObject($select, $prototype);
61
    }
62
 
63
    /**
64
     *
65
     * @param int $company_id
66
     * @return CompanyUser[]
67
     */
68
    public function fetchAllByUserId($user_id)
69
    {
70
        $prototype = new CompanyUser();
71
        $select = $this->sql->select(self::_TABLE);
72
        $select->where->equalTo('user_id', $user_id);
73
 
74
        return $this->executeFetchAllObject($select, $prototype);
75
    }
76
 
77
    /**
78
     *
79
     * @param int $id
80
     * @return CompanyUser
81
     */
82
    public function fetchOne($id)
83
    {
84
        $prototype = new CompanyUser();
85
        $select = $this->sql->select(self::_TABLE);
86
        $select->where->equalTo('id', $id);
87
 
88
        return $this->executeFetchOneObject($select, $prototype);
89
    }
90
 
91
    /**
92
     *
93
     * @param int $company_id
94
     * @return CompanyUser
95
     */
96
    public function fetchOwnerByCompanyId($company_id)
97
    {
98
        $prototype = new CompanyUser();
99
        $select = $this->sql->select(self::_TABLE);
100
        $select->where->equalTo('company_id', $company_id);
101
        $select->where->equalTo('owner', CompanyUser::OWNER_YES);
102
 
103
        return $this->executeFetchOneObject($select, $prototype);
104
    }
105
 
106
    /**
107
     *
108
     * @param int $company_id
109
     * @return CompanyUser
110
     */
111
    public function fetchCreatorByCompanyId($company_id)
112
    {
113
        $prototype = new CompanyUser();
114
        $select = $this->sql->select(self::_TABLE);
115
        $select->where->equalTo('company_id', $company_id);
116
        $select->where->equalTo('creator', CompanyUser::CREATOR_YES);
117
 
118
        return $this->executeFetchOneObject($select, $prototype);
119
    }
120
 
121
    /**
122
     *
123
     * @param int $company_id
124
     * @param int $user_id
125
     * @return CompanyUser
126
     */
127
    public function fetchOneByCompanyIdAndUserId($company_id, $user_id)
128
    {
129
        $prototype = new CompanyUser();
130
        $select = $this->sql->select(self::_TABLE);
131
        $select->where->equalTo('company_id', $company_id);
132
        $select->where->equalTo('user_id', $user_id);
133
 
134
       // echo $select->getSqlString($this->adapter->platform); exit;
135
 
136
        return $this->executeFetchOneObject($select, $prototype);
137
    }
138
 
139
 
140
    /**
141
     *
142
     * @param int $company_id
143
     * @param int $user_id
144
     * @return int
145
     */
146
    public function fetchCountOtherCompaniesByCompanyIdAndUserId($company_id, $user_id)
147
    {
148
        $prototype = new CompanyUser();
149
        $select = $this->sql->select();
150
        $select->columns(['total' => new Expression('COUNT(*)') ]);
151
        $select->from(self::_TABLE);
152
        $select->where->notEqualTo('company_id', $company_id);
153
        $select->where->equalTo('user_id', $user_id);
154
 
155
        $record = $this->executeFetchOneArray($select);
156
 
157
        return $record['total'];
158
    }
159
 
160
    /**
161
     *
162
     * @param CompanyUser $companyUser
163
     * @return boolean
164
     */
165
    public function insert($companyUser)
166
    {
167
        $hydrator = new ObjectPropertyHydrator();
168
        $values = $hydrator->extract($companyUser);
169
        $values = $this->removeEmpty($values);
170
 
171
 
172
        $insert = $this->sql->insert(self::_TABLE);
173
        $insert->values($values);
174
 
175
        $result = $this->executeInsert($insert);
176
        if($result) {
177
            $companyUser->id = $this->lastInsertId;
178
        }
179
 
180
        return $result;
181
    }
182
 
183
    /**
184
     *
185
     * @param CompanyUser $companyUser
186
     * @return boolean
187
     */
188
    public function update($companyUser)
189
    {
190
        $hydrator = new ObjectPropertyHydrator();
191
        $values = $hydrator->extract($companyUser);
192
        $values = $this->removeEmpty($values);
193
 
194
        $update = $this->sql->update(self::_TABLE);
195
        $update->set($values);
196
        $update->where->equalTo('id', $companyUser->id);
197
 
198
        return $this->executeUpdate($update);
199
    }
200
 
201
    /**
202
     *
203
     * @param int $id
204
     * @return boolean
205
     */
206
    public function delete($id)
207
    {
208
        $delete = $this->sql->select(self::_TABLE);
209
        $delete->where->equalTo('id', $id);
210
 
211
        return $this->executeDelete($delete);
212
    }
213
 
214
 
215
}