MY_Model v2.0 For CodeIgniter

by Udi

Update: This post is very old. I’ve created new model class for my CodeIgniter projects, you can find it here: Updated MY_Model for CodeIgniter 2.0

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
<?php

class MY_Model extends CI_Model
{

    // The main table name
    private $_table;

    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Setter/Getters for the table prop
     * @param String $table
     */

    public function setTable($table)
    {
        $this->_table = $table;
    }

    public function getTable()
    {
        return $this->_table;
    }


    // Log as Error each time nonexisting method called.
    public function __call($name, $arguments)
    {
        $args = implode(',', $arguments);
        log_message('error', $name . '(' . $args . ') Not exists.');
        return FALSE;
    }

    /**
     * Checks if certain field in any row has a certain value.
     * Its used to have a unique data like username, email etc.
     * @param String $field Name of the field in the table to check the data in
     * @param String $value The value to check if exists in that field
     * @return Bool TRUE if this data exists, FALSE if unique
     */

    public function is_duplicate($fieldName, $value)
    {
        if (empty($fieldName) OR empty($value))
        {
            log_message('error', 'Got some empty param. field: ' . $fieldName . ' | value: ' . $value);
            exit(0);
        }
        else
        {
            $this->db->select($fieldName);
            $query = $this->db->get_where($this->getTable() , array(
                $fieldName => $value
            ));
            if ($query->num_rows > 0)
            {
                return TRUE;
            }
            else
            {
                return FALSE;
            }
        }

        return FALSE;
    }

    /**
     * This method inserts some array of data into the db
     * @param Array $data
     * @return Int Insert ID
     */

    public function add($data)
    {
        if (is_array($data))
        {

            // $data['Reorder'] = $this->getLastOrder()+1;

            $this->db->insert($this->getTable() , $data);
            return $this->db->insert_id();
        }
        else
        {
            log_message('error', 'Got non-array param.');
            return FALSE;
        }
    }

    /**
     * This method updates fields in my table.
     * @param String $fieldName
     * @param String $value
     * @param Integer $id
     */

    public function update_field($fieldName, $fieldValue, $id)
    {
        if (empty($fieldName))
        {
            log_message('error', 'Got empty fieldName: ' . $fieldName);
            return FALSE;
        }
        else
        if (!is_numeric($id))
        {
            log_message('error', 'Got non-numeric id: ' . $id);
            return FALSE;
        }
        else
        {
            $this->db->where('id', $id);
            $query = $this->db->update($this->getTable() , array(
                $fieldName => $fieldValue
            ));
        }
    }

    /**
     * Updates whole row [unlike update_field()]
     * @param Array $data
     * @param Integer $id
     */

    public function update($data, $id)
    {
        if (!is_array($data))
        {
            log_message('error', 'Supposed to get an array!');
            return FALSE;
        }
        else
        if (!is_numeric($id))
        {
            log_message('error', 'Got non-numeric id: ' . $id);
            return FALSE;
        }
        else
        {
            /* write the old&amp;new data to history
            foreach($data as $fieldName=>$fieldValue) {
            $this->history->write($this->getTable(), $id, $fieldName, $fieldValue);
            }

            */

            $this->db->where('id', $id);
            $this->db->update($this->getTable() , $data);
        }
    }

    /**
     * This method returns all the rows of this model
     * @param Array $where Array of field=>value
     * @param Array $orderby field to order by and side - desc/asc.
     * @return CI_Query_Resource the $query object
     */

    public function get_where($where = array('is_deleted' => 0) , $orderBy = null)
    {
        if ($orderBy != null) $this->db->order_by($orderBy[1], $orderBy[2]);
        $query = $this->db->get_where($this->getTable() , $where);
        return $query;
    }

    /**
     * This method gets 1 row from a table and returns it.
     * @param Integer $id
     * @return DB_Res $query CodeIgniter's db resource
     */

    public function get($id)
    {
        if (!is_numeric($id))
        {
            log_message('error', 'Got not numeric id: ' . $id);
            return FALSE;
        }
        else
        {
            $query = $this->db->get_where($this->getTable() , array(
                'id' => $id
            ));
            return $query;
        }
    }

    /**
     * This method logically deletes a row.
     * @param Integer $id
     * @return Boolean
     */

    public function delete($id)
    {
        if (is_numeric($id))
        {
            $this->update_field('is_deleted', 1, $id);
            return TRUE;
        }
        else
        {
            return FALSE;
        }
    }
}