Proyectos de Subversion LeadersLinked - Antes de SPA

Rev

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

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