Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

Rev 3454 | Rev 3671 | Ir a la última revisión | | Comparar con el anterior | Ultima modificación | Ver Log |

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