Proyectos de Subversion LeadersLinked - Services

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
324 www 1
<?php
2
 
3
declare(strict_types=1);
4
 
5
namespace LeadersLinked\Mapper;
6
 
7
 
8
use Laminas\Db\Adapter\AdapterInterface;
9
use Laminas\Db\ResultSet\HydratingResultSet;
10
use Laminas\Paginator\Adapter\DbSelect;
11
use Laminas\Paginator\Paginator;
12
use Laminas\Log\LoggerInterface;
13
use Laminas\Hydrator\ArraySerializableHydrator;
14
use LeadersLinked\Hydrator\ObjectPropertyHydrator;
15
 
16
use LeadersLinked\Model\HabitCategory;
17
use LeadersLinked\Mapper\Common\MapperCommon;
18
 
19
 
20
class HabitCategoryMapper extends MapperCommon
21
{
22
    const _TABLE = 'tbl_habits_categories';
23
 
24
 
25
    /**
26
     *
27
     * @var HabitCategoryMapper
28
     */
29
    private static $_instance;
30
 
31
    /**
32
     *
33
     * @param AdapterInterface $adapter
34
     */
35
    private function __construct($adapter)
36
    {
37
        parent::__construct($adapter);
38
    }
39
 
40
    /**
41
     *
42
     * @param AdapterInterface $adapter
43
     * @return HabitCategoryMapper
44
     */
45
    public static function getInstance($adapter)
46
    {
47
        if(self::$_instance == null) {
48
            self::$_instance = new HabitCategoryMapper($adapter);
49
        }
50
        return self::$_instance;
51
    }
52
 
53
    /**
54
     *
55
     * @param int $id
56
     * @return HabitCategory
57
     */
58
    public function fetchOne($id)
59
    {
60
        $select = $this->sql->select(self::_TABLE);
61
        $select->where->equalTo('id', $id);
62
        $select->limit(1);
63
 
64
        $prototype = new HabitCategory();
65
        return $this->executeFetchOneObject($select, $prototype);
66
    }
67
 
68
    /**
69
     *
70
     * @param string $uuid
71
     * @return HabitCategory
72
     */
73
    public function fetchOneByUuid($uuid)
74
    {
75
        $select = $this->sql->select(self::_TABLE);
76
        $select->where->equalTo('uuid', $uuid);
77
        $select->limit(1);
78
 
79
        $prototype = new HabitCategory();
80
        return $this->executeFetchOneObject($select, $prototype);
81
    }
82
 
83
    /**
84
     *
85
     * @return HabitCategory[]
86
     */
87
    public function fetchAllByCompanyId($company_id)
88
    {
89
        $prototype = new HabitCategory();
90
        $select = $this->sql->select(self::_TABLE);
91
        $select->where->equalTo('company_id', $company_id);
92
        $select->order('name DESC');
93
        return $this->executeFetchAllObject($select, $prototype);
94
    }
95
 
96
 
97
    /**
98
     *
99
     * @return HabitCategory[]
100
     */
101
    public function fetchAllActiveByCompanyId($company_id)
102
    {
103
        $prototype = new HabitCategory();
104
        $select = $this->sql->select(self::_TABLE);
105
        $select->where->equalTo('status', HabitCategory::STATUS_ACTIVE);
106
        $select->where->equalTo('company_id', $company_id);
107
        $select->order('name DESC');
108
 
109
        return $this->executeFetchAllObject($select, $prototype);
110
    }
111
 
112
 
113
    /**
114
     *
115
     * @return Paginator
116
     */
117
    public function fetchAllActivePaginator()
118
    {
119
        $prototype = new HabitCategory();
120
        $select = $this->sql->select(self::_TABLE);
121
        $select->where->equalTo('status', HabitCategory::STATUS_ACTIVE);
122
 
123
        $hydrator   = new ObjectPropertyHydrator();
124
        $resultset  = new HydratingResultSet($hydrator, $prototype);
125
 
126
        $adapter = new DbSelect($select, $this->sql, $resultset);
127
        return new Paginator($adapter);
128
    }
129
 
130
    /**
131
     *
132
     * @param int $company_id
133
     * @param string $search
134
     * @param int $page
135
     * @param int $records_per_page
136
     * @param string $order_field
137
     * @param string $order_direction
138
     * @return Paginator
139
     */
140
    public function fetchAllDataTableByCompanyId($company_id, $search, $page = 1, $records_per_page = 10, $order_field= 'name', $order_direction = 'ASC')
141
    {
142
        $prototype = new HabitCategory();
143
        $select = $this->sql->select(self::_TABLE);
144
        $select->where->equalTo('company_id',  $company_id);
145
 
146
        if($search) {
147
            $select->where->like('name', '%' . $search . '%');
148
        }
149
        $select->order($order_field . ' ' . $order_direction);
150
 
151
        $hydrator   = new ObjectPropertyHydrator();
152
        $resultset  = new HydratingResultSet($hydrator, $prototype);
153
 
154
        $adapter = new DbSelect($select, $this->sql, $resultset);
155
        $paginator = new Paginator($adapter);
156
        $paginator->setItemCountPerPage($records_per_page);
157
        $paginator->setCurrentPageNumber($page);
158
 
159
 
160
        return $paginator;
161
    }
162
 
163
    /**
164
     *
165
     * @param HabitCategory $record
166
     * @return boolean
167
     */
168
    public function insert($record)
169
    {
170
        $hydrator = new ObjectPropertyHydrator();
171
        $values = $hydrator->extract($record);
172
        $values = $this->removeEmpty($values);
173
 
174
        $insert = $this->sql->insert(self::_TABLE);
175
        $insert->values($values);
176
 
177
 
178
        $result = $this->executeInsert($insert);
179
        if($result) {
180
            $record->id = $this->lastInsertId;
181
        }
182
 
183
        return $result;
184
 
185
    }
186
 
187
    /**
188
     *
189
     * @param HabitCategory $record
190
     * @return boolean
191
     */
192
    public function update($record)
193
    {
194
        $hydrator = new ObjectPropertyHydrator();
195
        $values = $hydrator->extract($record);
196
        $values = $this->removeEmpty($values);
197
 
198
        $update = $this->sql->update(self::_TABLE);
199
        $update->set($values);
200
        $update->where->equalTo('id', $record->id);
201
 
202
        return $this->executeUpdate($update);
203
    }
204
 
205
    /**
206
     *
207
     * @param HabitCategory $record
208
     * @return boolean
209
     */
210
    public function delete($record)
211
    {
212
        $delete = $this->sql->delete(self::_TABLE);
213
        $delete->where->equalTo('id', $record->id);
214
 
215
        return $this->executeDelete($delete);
216
 
217
    }
218
 
219
 
220
}