Proyectos de Subversion LeadersLinked - Services

Rev

Rev 302 | | Comparar con el anterior | Ultima modificación | Ver Log |

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