Completed
Push — develop ( 4f2388...3f4b0b )
by John
02:50
created

ActiveRecordProviderSQLite::checkBOTableExists()   D

Complexity

Conditions 9
Paths 50

Size

Total Lines 42
Code Lines 23

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 42
rs 4.909
cc 9
eloc 23
nc 50
nop 2
1
<?php
2
3
namespace Alpha\Model;
4
5
use Alpha\Model\Type\Integer;
6
use Alpha\Model\Type\Timestamp;
7
use Alpha\Model\Type\DEnum;
8
use Alpha\Model\Type\Relation;
9
use Alpha\Model\Type\RelationLookup;
10
use Alpha\Model\Type\Double;
11
use Alpha\Model\Type\Text;
12
use Alpha\Model\Type\SmallText;
13
use Alpha\Model\Type\Date;
14
use Alpha\Model\Type\Enum;
15
use Alpha\Model\Type\Boolean;
16
use Alpha\Util\Config\ConfigProvider;
17
use Alpha\Util\Logging\Logger;
18
use Alpha\Util\Http\Session\SessionProviderFactory;
19
use Alpha\Exception\AlphaException;
20
use Alpha\Exception\FailedSaveException;
21
use Alpha\Exception\FailedDeleteException;
22
use Alpha\Exception\FailedIndexCreateException;
23
use Alpha\Exception\LockingException;
24
use Alpha\Exception\ValidationException;
25
use Alpha\Exception\CustomQueryException;
26
use Alpha\Exception\RecordNotFoundException;
27
use Alpha\Exception\BadTableNameException;
28
use Alpha\Exception\NotImplementedException;
29
use Alpha\Exception\PHPException;
30
use Alpha\Exception\ResourceNotAllowedException;
31
use Alpha\Exception\IllegalArguementException;
32
use Exception;
33
use SQLite3Stmt;
34
use SQLite3;
35
use ReflectionClass;
36
37
/**
38
 * SQLite active record provider (uses the SQLite3 native API in PHP).
39
 *
40
 * @since 1.2
41
 *
42
 * @author John Collins <[email protected]>
43
 * @license http://www.opensource.org/licenses/bsd-license.php The BSD License
44
 * @copyright Copyright (c) 2017, John Collins (founder of Alpha Framework).
45
 * All rights reserved.
46
 *
47
 * <pre>
48
 * Redistribution and use in source and binary forms, with or
49
 * without modification, are permitted provided that the
50
 * following conditions are met:
51
 *
52
 * * Redistributions of source code must retain the above
53
 *   copyright notice, this list of conditions and the
54
 *   following disclaimer.
55
 * * Redistributions in binary form must reproduce the above
56
 *   copyright notice, this list of conditions and the
57
 *   following disclaimer in the documentation and/or other
58
 *   materials provided with the distribution.
59
 * * Neither the name of the Alpha Framework nor the names
60
 *   of its contributors may be used to endorse or promote
61
 *   products derived from this software without specific
62
 *   prior written permission.
63
 *
64
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND
65
 * CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES,
66
 * INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
67
 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
68
 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
69
 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
70
 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
71
 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
72
 * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
73
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
74
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE
75
 * OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
76
 * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
77
 * </pre>
78
 */
79
class ActiveRecordProviderSQLite implements ActiveRecordProviderInterface
80
{
81
    /**
82
     * Trace logger.
83
     *
84
     * @var \Alpha\Util\Logging\Logger
85
     *
86
     * @since 1.2
87
     */
88
    private static $logger = null;
89
90
    /**
91
     * Database connection.
92
     *
93
     * @var SQLite3
94
     *
95
     * @since 1.2
96
     */
97
    private static $connection;
98
99
    /**
100
     * The business object that we are mapping back to.
101
     *
102
     * @var \Alpha\Model\ActiveRecord
103
     *
104
     * @since 1.2
105
     */
106
    private $Record;
0 ignored issues
show
Unused Code introduced by
The property $Record is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
107
108
    /**
109
     * An array of new foreign keys that need to be created.
110
     *
111
     * @var array
112
     *
113
     * @since 2.0.1
114
     */
115
    private $foreignKeys = array();
116
117
    /**
118
     * The constructor.
119
     *
120
     * @since 1.2
121
     */
122
    public function __construct()
123
    {
124
        self::$logger = new Logger('ActiveRecordProviderSQLite');
125
        self::$logger->debug('>>__construct()');
126
127
        self::$logger->debug('<<__construct');
128
    }
129
130
    /**
131
     * (non-PHPdoc).
132
     *
133
     * @see Alpha\Model\ActiveRecordProviderInterface::getConnection()
134
     */
135
    public static function getConnection()
136
    {
137
        $config = ConfigProvider::getInstance();
138
139
        if (!isset(self::$connection)) {
140
            try {
141
                self::$connection = new SQLite3($config->get('db.file.path'));
142
            } catch (\Exception $e) {
143
                self::$logger->fatal('Could not open SQLite database: ['.$e->getMessage().']');
144
            }
145
        }
146
147
        return self::$connection;
148
    }
149
150
    /**
151
     * (non-PHPdoc).
152
     *
153
     * @see Alpha\Model\ActiveRecordProviderInterface::disconnect()
154
     */
155
    public static function disconnect()
156
    {
157
        if (isset(self::$connection)) {
158
            self::$connection->close();
159
            self::$connection = null;
160
        }
161
    }
162
163
    /**
164
     * (non-PHPdoc).
165
     *
166
     * @see Alpha\Model\ActiveRecordProviderInterface::getLastDatabaseError()
167
     */
168
    public static function getLastDatabaseError()
169
    {
170
        self::$connection->lastErrorMsg();
171
    }
172
173
    /**
174
     * (non-PHPdoc).
175
     *
176
     * @see Alpha\Model\ActiveRecordProviderInterface::query()
177
     */
178
    public function query($sqlQuery)
179
    {
180
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
181
182
        $resultArray = array();
183
184
        if (!$result = self::getConnection()->query($sqlQuery)) {
185
            throw new CustomQueryException('Failed to run the custom query, SQLite error is ['.self::getLastDatabaseError().'], query ['.$sqlQuery.']');
186
        } else {
187
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
188
                array_push($resultArray, $row);
189
            }
190
191
            return $resultArray;
192
        }
193
    }
194
195
    /**
196
     * (non-PHPdoc).
197
     *
198
     * @see Alpha\Model\ActiveRecordProviderInterface::load()
199
     */
200
    public function load($OID, $version = 0)
201
    {
202
        self::$logger->debug('>>load(OID=['.$OID.'], version=['.$version.'])');
203
204
        $attributes = $this->record->getPersistentAttributes();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
205
        $fields = '';
206
        foreach ($attributes as $att) {
207
            $fields .= $att.',';
208
        }
209
        $fields = mb_substr($fields, 0, -1);
210
211
        if ($version > 0) {
212
            $sqlQuery = 'SELECT '.$fields.' FROM '.$this->record->getTableName().'_history WHERE OID = :OID AND version_num = :version LIMIT 1;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
213
        } else {
214
            $sqlQuery = 'SELECT '.$fields.' FROM '.$this->record->getTableName().' WHERE OID = :OID LIMIT 1;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
215
        }
216
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
217
218
        try {
219
            $stmt = self::getConnection()->prepare($sqlQuery);
220
221
            $row = array();
222
223
            if ($version > 0) {
224
                $stmt->bindValue(':version', $version, SQLITE3_INTEGER);
225
            }
226
227
            $stmt->bindValue(':OID', $OID, SQLITE3_INTEGER);
228
229
            $result = $stmt->execute();
230
231
            // there should only ever be one (or none)
232
            $row = $result->fetchArray(SQLITE3_ASSOC);
233
234
            $stmt->close();
235
        } catch (PHPException $e) {
236
            self::$logger->warn('The following query caused an unexpected result ['.$sqlQuery.']');
237
            if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
238
                $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
239
240
                throw new RecordNotFoundException('Failed to load object of OID ['.$OID.'], table ['.$this->record->getTableName().'] did not exist so had to create!');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
241
            }
242
243
            return;
244
        }
245
246
        if (!isset($row['OID']) || $row['OID'] < 1) {
247
            self::$logger->debug('<<load');
248
            throw new RecordNotFoundException('Failed to load object of OID ['.$OID.'] not found in database.');
249
        }
250
251
        // get the class attributes
252
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
253
        $properties = $reflection->getProperties();
254
255
        try {
256
            foreach ($properties as $propObj) {
257
                $propName = $propObj->name;
258
259
                // filter transient attributes
260
                if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
261
                    $this->record->set($propName, $row[$propName]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
262
                } elseif (!$propObj->isPrivate() && $this->record->getPropObject($propName) instanceof Relation) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
263
                    $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
264
265
                    // handle the setting of ONE-TO-MANY relation values
266
                    if ($prop->getRelationType() == 'ONE-TO-MANY') {
267
                        $this->record->set($propObj->name, $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
268
                    }
269
270
                    // handle the setting of MANY-TO-ONE relation values
271
                    if ($prop->getRelationType() == 'MANY-TO-ONE' && isset($row[$propName])) {
272
                        $this->record->set($propObj->name, $row[$propName]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
273
                    }
274
                }
275
            }
276
        } catch (IllegalArguementException $e) {
277
            self::$logger->warn('Bad data stored in the table ['.$this->record->getTableName().'], field ['.$propObj->name.'] bad value['.$row[$propObj->name].'], exception ['.$e->getMessage().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
278
        } catch (PHPException $e) {
279
            // it is possible that the load failed due to the table not being up-to-date
280
            if ($this->record->checkTableNeedsUpdate()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
281
                $missingFields = $this->record->findMissingFields();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
282
283
                $count = count($missingFields);
284
285
                for ($i = 0; $i < $count; ++$i) {
286
                    $this->record->addProperty($missingFields[$i]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
287
                }
288
289
                self::$logger->debug('<<load');
290
                throw new RecordFoundException('Failed to load object of OID ['.$OID.'], table ['.$this->record->getTableName().'] was out of sync with the database so had to be updated!');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
291
            }
292
        }
293
294
        self::$logger->debug('<<load');
295
    }
296
297
    /**
298
     * (non-PHPdoc).
299
     *
300
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAllOldVersions()
301
     */
302
    public function loadAllOldVersions($OID)
303
    {
304
        self::$logger->debug('>>loadAllOldVersions(OID=['.$OID.'])');
305
306
        if (!$this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
307
            throw new RecordFoundException('loadAllOldVersions method called on an active record where no history is maintained!');
308
        }
309
310
        $sqlQuery = 'SELECT version_num FROM '.$this->record->getTableName().'_history WHERE OID = \''.$OID.'\' ORDER BY version_num;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
311
312
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
313
314
        if (!$result = self::getConnection()->query($sqlQuery)) {
315
            self::$logger->debug('<<loadAllOldVersions');
316
            throw new RecordNotFoundException('Failed to load object versions, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
317
        }
318
319
        // now build an array of objects to be returned
320
        $objects = array();
321
        $count = 0;
322
        $RecordClass = get_class($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
323
324
        while ($row = $result->fetchArray()) {
325
            try {
326
                $obj = new $RecordClass();
327
                $obj->load($OID, $row['version_num']);
328
                $objects[$count] = $obj;
329
                ++$count;
330
            } catch (ResourceNotAllowedException $e) {
331
                // the resource not allowed will be absent from the list
332
            }
333
        }
334
335
        self::$logger->warn('<<loadAllOldVersions ['.count($objects).']');
336
337
        return $objects;
338
    }
339
340
    /**
341
     * (non-PHPdoc).
342
     *
343
     * @see Alpha\Model\ActiveRecordProviderInterface::loadByAttribute()
344
     */
345
    public function loadByAttribute($attribute, $value, $ignoreClassType = false, $loadAttributes = array())
346
    {
347
        self::$logger->debug('>>loadByAttribute(attribute=['.$attribute.'], value=['.$value.'], ignoreClassType=['.$ignoreClassType.'], 
348
            loadAttributes=['.var_export($loadAttributes, true).'])');
349
350
        if (count($loadAttributes) == 0) {
351
            $attributes = $this->record->getPersistentAttributes();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
352
        } else {
353
            $attributes = $loadAttributes;
354
        }
355
356
        $fields = '';
357
        foreach ($attributes as $att) {
358
            $fields .= $att.',';
359
        }
360
        $fields = mb_substr($fields, 0, -1);
361
362
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
363
            $sqlQuery = 'SELECT '.$fields.' FROM '.$this->record->getTableName().' WHERE '.$attribute.' = :attribute AND classname = :classname LIMIT 1;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
364
        } else {
365
            $sqlQuery = 'SELECT '.$fields.' FROM '.$this->record->getTableName().' WHERE '.$attribute.' = :attribute LIMIT 1;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
366
        }
367
368
        self::$logger->debug('Query=['.$sqlQuery.']');
369
370
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
371
        $stmt = self::getConnection()->prepare($sqlQuery);
372
373
        if ($stmt instanceof SQLite3Stmt) {
374
            if ($this->record->getPropObject($attribute) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
375
                if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
376
                    $stmt->bindValue(':attribute', $value, SQLITE3_INTEGER);
377
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
378
                } else {
379
                    $stmt->bindValue(':attribute', $value, SQLITE3_INTEGER);
380
                }
381
            } else {
382
                if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
383
                    $stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
384
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
385
                } else {
386
                    $stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
387
                }
388
            }
389
390
            $result = $stmt->execute();
391
392
            // there should only ever be one (or none)
393
            $row = $result->fetchArray(SQLITE3_ASSOC);
394
395
            $stmt->close();
396
        } else {
397
            self::$logger->warn('The following query caused an unexpected result ['.$sqlQuery.']');
398
            if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
399
                $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
400
401
                throw new RecordNotFoundException('Failed to load object by attribute ['.$attribute.'] and value ['.$value.'], table did not exist so had to create!');
402
            }
403
404
            return;
405
        }
406
407
        if (!isset($row['OID']) || $row['OID'] < 1) {
408
            self::$logger->debug('<<loadByAttribute');
409
            throw new RecordNotFoundException('Failed to load object by attribute ['.$attribute.'] and value ['.$value.'], not found in database.');
410
        }
411
412
        $this->record->setOID($row['OID']);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
413
414
        // get the class attributes
415
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
416
        $properties = $reflection->getProperties();
417
418
        try {
419
            foreach ($properties as $propObj) {
420
                $propName = $propObj->name;
421
422
                if (isset($row[$propName])) {
423
                    // filter transient attributes
424
                    if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
425
                        $this->record->set($propName, $row[$propName]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
426
                    } elseif (!$propObj->isPrivate() && $this->record->get($propName) != '' && $this->record->getPropObject($propName) instanceof Relation) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
427
                        $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
428
429
                        // handle the setting of ONE-TO-MANY relation values
430
                        if ($prop->getRelationType() == 'ONE-TO-MANY') {
431
                            $this->record->set($propObj->name, $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
432
                        }
433
                    }
434
                }
435
            }
436
        } catch (IllegalArguementException $e) {
437
            self::$logger->warn('Bad data stored in the table ['.$this->record->getTableName().'], field ['.$propObj->name.'] bad value['.$row[$propObj->name].'], exception ['.$e->getMessage().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
438
        } catch (PHPException $e) {
439
            // it is possible that the load failed due to the table not being up-to-date
440
            if ($this->record->checkTableNeedsUpdate()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
441
                $missingFields = $this->record->findMissingFields();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
442
443
                $count = count($missingFields);
444
445
                for ($i = 0; $i < $count; ++$i) {
446
                    $this->record->addProperty($missingFields[$i]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
447
                }
448
449
                self::$logger->debug('<<loadByAttribute');
450
                throw new RecordNotFoundException('Failed to load object by attribute ['.$attribute.'] and value ['.$value.'], table ['.$this->record->getTableName().'] was out of sync with the database so had to be updated!');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
451
            }
452
        }
453
454
        self::$logger->debug('<<loadByAttribute');
455
    }
456
457
    /**
458
     * (non-PHPdoc).
459
     *
460
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAll()
461
     */
462
    public function loadAll($start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false)
463
    {
464
        self::$logger->debug('>>loadAll(start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
465
466
        // ensure that the field name provided in the orderBy param is legit
467
        try {
468
            $this->record->get($orderBy);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
469
        } catch (AlphaException $e) {
470
            throw new AlphaException('The field name ['.$orderBy.'] provided in the param orderBy does not exist on the class ['.get_class($this->record).']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
471
        }
472
473
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
474
            if ($limit == 0) {
475
                $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().' WHERE classname = \''.addslashes(get_class($this->record)).'\' ORDER BY '.$orderBy.' '.$order.';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
476
            } else {
477
                $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().' WHERE classname = \''.addslashes(get_class($this->record)).'\' ORDER BY '.$orderBy.' '.$order.' LIMIT '.
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
478
                    $limit.' OFFSET '.$start.';';
479
            }
480
        } else {
481
            if ($limit == 0) {
482
                $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().' ORDER BY '.$orderBy.' '.$order.';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
483
            } else {
484
                $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().' ORDER BY '.$orderBy.' '.$order.' LIMIT '.$limit.' OFFSET '.$start.';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
485
            }
486
        }
487
488
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
489
490
        if (!$result = self::getConnection()->query($sqlQuery)) {
491
            self::$logger->debug('<<loadAll');
492
            throw new RecordNotFoundException('Failed to load object OIDs, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
493
        }
494
495
        // now build an array of objects to be returned
496
        $objects = array();
497
        $count = 0;
498
        $RecordClass = get_class($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
499
500
        while ($row = $result->fetchArray()) {
501
            try {
502
                $obj = new $RecordClass();
503
                $obj->load($row['OID']);
504
                $objects[$count] = $obj;
505
                ++$count;
506
            } catch (ResourceNotAllowedException $e) {
507
                // the resource not allowed will be absent from the list
508
            }
509
        }
510
511
        self::$logger->debug('<<loadAll ['.count($objects).']');
512
513
        return $objects;
514
    }
515
516
    /**
517
     * (non-PHPdoc).
518
     *
519
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAllByAttribute()
520
     */
521
    public function loadAllByAttribute($attribute, $value, $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false, $constructorArgs = array())
522
    {
523
        self::$logger->debug('>>loadAllByAttribute(attribute=['.$attribute.'], value=['.$value.'], start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.'], constructorArgs=['.print_r($constructorArgs, true).']');
524
525
        if ($start != 0 && $limit != 0) {
526
            $limit = ' LIMIT '.$limit.' OFFSET '.$start.';';
527
        } else {
528
            $limit = ';';
529
        }
530
531
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
532
            $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName()." WHERE $attribute = :attribute AND classname = :classname ORDER BY ".$orderBy.' '.$order.$limit;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
533
        } else {
534
            $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName()." WHERE $attribute = :attribute ORDER BY ".$orderBy.' '.$order.$limit;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
535
        }
536
537
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
538
        self::$logger->debug($sqlQuery);
539
540
        $stmt = self::getConnection()->prepare($sqlQuery);
541
542
        $objects = array();
543
544
        if ($stmt instanceof SQLite3Stmt) {
545
            if ($this->record->getPropObject($attribute) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
546
                if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
547
                    $stmt->bindValue(':attribute', $value, SQLITE3_INTEGER);
548
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
549
                } else {
550
                    $stmt->bindValue(':attribute', $value, SQLITE3_INTEGER);
551
                }
552
            } else {
553
                if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
554
                    $stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
555
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
556
                } else {
557
                    $stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
558
                }
559
            }
560
561
            $result = $stmt->execute();
562
563
            // now build an array of objects to be returned
564
            $count = 0;
565
            $RecordClass = get_class($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
566
567
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
568
                try {
569
                    $argsCount = count($constructorArgs);
570
571
                    if ($argsCount < 1) {
572
                        $obj = new $RecordClass();
573
                    } else {
574
                        switch ($argsCount) {
575
                            case 1:
576
                                $obj = new $RecordClass($constructorArgs[0]);
577
                                break;
578
                            case 2:
579
                                $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1]);
580
                                break;
581
                            case 3:
582
                                $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2]);
583
                                break;
584
                            case 4:
585
                                $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2], $constructorArgs[3]);
586
                                break;
587
                            case 5:
588
                                $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2], $constructorArgs[3], $constructorArgs[4]);
589
                                break;
590
                            default:
591
                                throw new IllegalArguementException('Too many elements in the $constructorArgs array passed to the loadAllByAttribute method!');
592
                        }
593
                    }
594
595
                    $obj->load($row['OID']);
596
                    $objects[$count] = $obj;
597
                    ++$count;
598
                } catch (ResourceNotAllowedException $e) {
599
                    // the resource not allowed will be absent from the list
600
                }
601
            }
602
603
            $stmt->close();
604
        } else {
605
            self::$logger->warn('The following query caused an unexpected result ['.$sqlQuery.']');
606
607
            if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
608
                $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
609
610
                throw new RecordFoundException('Failed to load objects by attribute ['.$attribute.'] and value ['.$value.'], table did not exist so had to create!');
611
            }
612
613
            self::$logger->debug('<<loadAllByAttribute []');
614
615
            return array();
616
        }
617
618
        self::$logger->debug('<<loadAllByAttribute ['.count($objects).']');
619
620
        return $objects;
621
    }
622
623
    /**
624
     * (non-PHPdoc).
625
     *
626
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAllByAttributes()
627
     */
628
    public function loadAllByAttributes($attributes = array(), $values = array(), $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false, $constructorArgs = array())
629
    {
630
        self::$logger->debug('>>loadAllByAttributes(attributes=['.var_export($attributes, true).'], values=['.var_export($values, true).'], start=['.
631
            $start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.'], constructorArgs=['.print_r($constructorArgs, true).']');
632
633
        $whereClause = ' WHERE';
634
635
        $count = count($attributes);
636
637
        for ($i = 0; $i < $count; ++$i) {
638
            $whereClause .= ' '.$attributes[$i].' = :'.$attributes[$i].' AND';
639
            self::$logger->debug($whereClause);
640
        }
641
642
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
643
            $whereClause .= ' classname = :classname AND';
644
        }
645
646
        // remove the last " AND"
647
        $whereClause = mb_substr($whereClause, 0, -4);
648
649
        if ($limit != 0) {
650
            $limit = ' LIMIT '.$limit.' OFFSET '.$start.';';
651
        } else {
652
            $limit = ';';
653
        }
654
655
        $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().$whereClause.' ORDER BY '.$orderBy.' '.$order.$limit;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
656
657
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
658
659
        $stmt = self::getConnection()->prepare($sqlQuery);
660
661
        if ($stmt instanceof SQLite3Stmt) {
662
            // bind params where required attributes are provided
663
            if (count($attributes) > 0 && count($attributes) == count($values)) {
664
                for ($i = 0; $i < count($attributes); ++$i) {
0 ignored issues
show
Performance Best Practice introduced by
It seems like you are calling the size function count() as part of the test condition. You might want to compute the size beforehand, and not on each iteration.

If the size of the collection does not change during the iteration, it is generally a good practice to compute it beforehand, and not on each iteration:

for ($i=0; $i<count($array); $i++) { // calls count() on each iteration
}

// Better
for ($i=0, $c=count($array); $i<$c; $i++) { // calls count() just once
}
Loading history...
665
                    if (strcspn($values[$i], '0123456789') != strlen($values[$i])) {
666
                        $stmt->bindValue(':'.$attributes[$i], $values[$i], SQLITE3_INTEGER);
667
                    } else {
668
                        $stmt->bindValue(':'.$attributes[$i], $values[$i], SQLITE3_TEXT);
669
                    }
670
                }
671
            } else {
672
                // we'll still need to bind the "classname" for overloaded records...
673
                if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
674
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
675
                }
676
            }
677
678
            $result = $stmt->execute();
679
        } else {
680
            self::$logger->warn('The following query caused an unexpected result ['.$sqlQuery.']');
681
682
            if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
683
                $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
684
685
                throw new RecordFoundException('Failed to load objects by attributes ['.var_export($attributes, true).'] and values ['.
686
                    var_export($values, true).'], table did not exist so had to create!');
687
            }
688
689
            self::$logger->debug('<<loadAllByAttributes []');
690
691
            return array();
692
        }
693
694
        // now build an array of objects to be returned
695
        $objects = array();
696
        $count = 0;
697
        $RecordClass = get_class($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
698
699
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
700
            try {
701
                $argsCount = count($constructorArgs);
702
703
                if ($argsCount < 1) {
704
                    $obj = new $RecordClass();
705
                } else {
706
                    switch ($argsCount) {
707
                        case 1:
708
                            $obj = new $RecordClass($constructorArgs[0]);
709
                            break;
710
                        case 2:
711
                            $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1]);
712
                            break;
713
                        case 3:
714
                            $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2]);
715
                            break;
716
                        case 4:
717
                            $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2], $constructorArgs[3]);
718
                            break;
719
                        case 5:
720
                            $obj = new $RecordClass($constructorArgs[0], $constructorArgs[1], $constructorArgs[2], $constructorArgs[3], $constructorArgs[4]);
721
                            break;
722
                        default:
723
                            throw new IllegalArguementException('Too many elements in the $constructorArgs array passed to the loadAllByAttribute method!');
724
                    }
725
                }
726
727
                $obj->load($row['OID']);
728
                $objects[$count] = $obj;
729
                ++$count;
730
            } catch (ResourceNotAllowedException $e) {
731
                // the resource not allowed will be absent from the list
732
            }
733
        }
734
735
        $stmt->close();
736
737
        self::$logger->debug('<<loadAllByAttributes ['.count($objects).']');
738
739
        return $objects;
740
    }
741
742
    /**
743
     * (non-PHPdoc).
744
     *
745
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAllByDayUpdated()
746
     */
747
    public function loadAllByDayUpdated($date, $start = 0, $limit = 0, $orderBy = 'OID', $order = 'ASC', $ignoreClassType = false)
748
    {
749
        self::$logger->debug('>>loadAllByDayUpdated(date=['.$date.'], start=['.$start.'], limit=['.$limit.'], orderBy=['.$orderBy.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
750
751
        if ($start != 0 && $limit != 0) {
752
            $limit = ' LIMIT '.$limit.' OFFSET '.$start.';';
753
        } else {
754
            $limit = ';';
755
        }
756
757
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
758
            $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName()." WHERE updated_ts >= '".$date." 00:00:00' AND updated_ts <= '".$date." 23:59:59' AND classname = '".addslashes(get_class($this->record))."' ORDER BY ".$orderBy.' '.$order.$limit;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
759
        } else {
760
            $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName()." WHERE updated_ts >= '".$date." 00:00:00' AND updated_ts <= '".$date." 23:59:59' ORDER BY ".$orderBy.' '.$order.$limit;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
761
        }
762
763
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
764
765
        if (!$result = self::getConnection()->query($sqlQuery)) {
766
            self::$logger->debug('<<loadAllByDayUpdated');
767
            throw new RecordNotFoundException('Failed to load object OIDs, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
768
        }
769
770
        // now build an array of objects to be returned
771
        $objects = array();
772
        $count = 0;
773
        $RecordClass = get_class($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
774
775
        while ($row = $result->fetchArray()) {
776
            $obj = new $RecordClass();
777
            $obj->load($row['OID']);
778
            $objects[$count] = $obj;
779
            ++$count;
780
        }
781
782
        self::$logger->debug('<<loadAllByDayUpdated ['.count($objects).']');
783
784
        return $objects;
785
    }
786
787
    /**
788
     * (non-PHPdoc).
789
     *
790
     * @see Alpha\Model\ActiveRecordProviderInterface::loadAllFieldValuesByAttribute()
791
     */
792
    public function loadAllFieldValuesByAttribute($attribute, $value, $returnAttribute, $order = 'ASC', $ignoreClassType = false)
793
    {
794
        self::$logger->debug('>>loadAllFieldValuesByAttribute(attribute=['.$attribute.'], value=['.$value.'], returnAttribute=['.$returnAttribute.'], order=['.$order.'], ignoreClassType=['.$ignoreClassType.']');
795
796
        if (!$ignoreClassType && $this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
797
            $sqlQuery = 'SELECT '.$returnAttribute.' FROM '.$this->record->getTableName()." WHERE $attribute = '$value' AND classname = '".addslashes(get_class($this->record))."' ORDER BY OID ".$order.';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
798
        } else {
799
            $sqlQuery = 'SELECT '.$returnAttribute.' FROM '.$this->record->getTableName()." WHERE $attribute = '$value' ORDER BY OID ".$order.';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
800
        }
801
802
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
803
804
        self::$logger->debug('lastQuery ['.$sqlQuery.']');
805
806
        if (!$result = self::getConnection()->query($sqlQuery)) {
807
            self::$logger->debug('<<loadAllFieldValuesByAttribute');
808
            throw new RecordNotFoundException('Failed to load field ['.$returnAttribute.'] values, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
809
        }
810
811
        // now build an array of attribute values to be returned
812
        $values = array();
813
        $count = 0;
814
815
        while ($row = $result->fetchArray()) {
816
            $values[$count] = $row[$returnAttribute];
817
            ++$count;
818
        }
819
820
        self::$logger->debug('<<loadAllFieldValuesByAttribute ['.count($values).']');
821
822
        return $values;
823
    }
824
825
    /**
826
     * (non-PHPdoc).
827
     *
828
     * @see Alpha\Model\ActiveRecordProviderInterface::save()
829
     */
830
    public function save()
831
    {
832
        self::$logger->debug('>>save()');
833
834
        $config = ConfigProvider::getInstance();
835
        $sessionProvider = $config->get('session.provider.name');
836
        $session = SessionProviderFactory::getInstance($sessionProvider);
837
838
        // get the class attributes
839
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
840
        $properties = $reflection->getProperties();
841
842
        if ($this->record->getVersion() != $this->record->getVersionNumber()->getValue()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
843
            throw new LockingException('Could not save the object as it has been updated by another user.  Please try saving again.');
844
        }
845
846
        // set the "updated by" fields, we can only set the user id if someone is logged in
847
        if ($session->get('currentUser') != null) {
848
            $this->record->set('updated_by', $session->get('currentUser')->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
849
        }
850
851
        $this->record->set('updated_ts', new Timestamp(date('Y-m-d H:i:s')));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
852
853
        // check to see if it is a transient object that needs to be inserted
854
        if ($this->record->isTransient()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
855
            $savedFields = array();
856
            $sqlQuery = 'INSERT INTO '.$this->record->getTableName().' (';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
857
858
            foreach ($properties as $propObj) {
859
                $propName = $propObj->name;
860
                if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
861
                    // Skip the OID, database auto number takes care of this.
862
                    if ($propName != 'OID' && $propName != 'version_num') {
863
                        $sqlQuery .= "$propName,";
864
                        $savedFields[] = $propName;
865
                    }
866
867
                    if ($propName == 'version_num') {
868
                        $sqlQuery .= 'version_num,';
869
                        $savedFields[] = 'version_num';
870
                    }
871
                }
872
            }
873
            if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
874
                $sqlQuery .= 'classname,';
875
            }
876
877
            $sqlQuery = rtrim($sqlQuery, ',');
878
879
            $sqlQuery .= ') VALUES (';
880
881
            foreach ($savedFields as $savedField) {
882
                $sqlQuery .= ':'.$savedField.',';
883
            }
884
885
            if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
886
                $sqlQuery .= ':classname,';
887
            }
888
889
            $sqlQuery = rtrim($sqlQuery, ',').')';
890
891
            $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
892
            self::$logger->debug('Query ['.$sqlQuery.']');
893
894
            $stmt = self::getConnection()->prepare($sqlQuery);
895
896
            if ($stmt instanceof SQLite3Stmt) {
897
                foreach ($savedFields as $savedField) {
898
                    if ($this->record->get($savedField) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
899
                        $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
900
                    } else {
901
                        $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
902
                    }
903
                }
904
905
                if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
906
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
907
                }
908
909
                $stmt->bindValue(':version_num', 1, SQLITE3_INTEGER); // on an initial save, this will always be 1
910
                $this->record->set('version_num', 1);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
911
912
                try {
913
                    $stmt->execute();
914
                } catch (Exception $e) {
915
                    if (self::getConnection()->lastErrorCode() == 19) {
916
                        throw new ValidationException('Unique key violation while trying to save object, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
917
                    } else {
918
                        throw new FailedSaveException('Failed to save object, exception ['.$e->getMessage().'], DB error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
919
                    }
920
                }
921
            } else {
922
                throw new FailedSaveException('Failed to save object, exception ['.$e->getMessage().'], DB error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
923
            }
924
        } else {
925
            // assume that it is a persistent object that needs to be updated
926
            $savedFields = array();
927
            $sqlQuery = 'UPDATE '.$this->record->getTableName().' SET ';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
928
929
            foreach ($properties as $propObj) {
930
                $propName = $propObj->name;
931
                if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
932
                    // Skip the OID, database auto number takes care of this.
933
                    if ($propName != 'OID' && $propName != 'version_num') {
934
                        $sqlQuery .= "$propName = :$propName,";
935
                        $savedFields[] = $propName;
936
                    }
937
938
                    if ($propName == 'version_num') {
939
                        $sqlQuery .= 'version_num = :version_num,';
940
                        $savedFields[] = 'version_num';
941
                    }
942
                }
943
            }
944
945
            if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
946
                $sqlQuery .= 'classname = :classname,';
947
            }
948
949
            $sqlQuery = rtrim($sqlQuery, ',');
950
951
            $sqlQuery .= ' WHERE OID=:OID;';
952
953
            $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
954
            $stmt = self::getConnection()->prepare($sqlQuery);
955
956
            if ($stmt instanceof SQLite3Stmt) {
957
                foreach ($savedFields as $savedField) {
958
                    if ($this->record->get($savedField) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
959
                        $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
960
                    } else {
961
                        $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
962
                    }
963
                }
964
965
                if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
966
                    $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
967
                }
968
969
                $stmt->bindValue(':OID', $this->record->getOID(), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
970
971
                $temp = $this->record->getVersionNumber()->getValue();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
972
                $this->record->set('version_num', $temp + 1);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
973
                $stmt->bindValue(':version_num', $temp + 1, SQLITE3_INTEGER);
974
975
                $stmt->execute();
976
            } else {
977
                throw new FailedSaveException('Failed to save object, error is ['.$stmt->error.'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
978
            }
979
        }
980
981
        if ($stmt != null && $stmt != false) {
982
            // populate the updated OID in case we just done an insert
983
            if ($this->record->isTransient()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
984
                $this->record->setOID(self::getConnection()->lastInsertRowID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
985
            }
986
987
            try {
988
                foreach ($properties as $propObj) {
989
                    $propName = $propObj->name;
990
991
                    if ($this->record->getPropObject($propName) instanceof Relation) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
992
                        $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
993
994
                        // handle the saving of MANY-TO-MANY relation values
995
                        if ($prop->getRelationType() == 'MANY-TO-MANY' && count($prop->getRelatedOIDs()) > 0) {
996
                            try {
997
                                try {
998
                                    // check to see if the rel is on this class
999
                                    $side = $prop->getSide(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1000
                                } catch (IllegalArguementException $iae) {
1001
                                    $side = $prop->getSide(get_parent_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1002
                                }
1003
1004
                                $lookUp = $prop->getLookup();
1005
1006
                                // first delete all of the old RelationLookup objects for this rel
1007
                                try {
1008
                                    if ($side == 'left') {
1009
                                        $lookUp->deleteAllByAttribute('leftID', $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1010
                                    } else {
1011
                                        $lookUp->deleteAllByAttribute('rightID', $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1012
                                    }
1013
                                } catch (Exception $e) {
1014
                                    throw new FailedSaveException('Failed to delete old RelationLookup objects on the table ['.$prop->getLookup()->getTableName().'], error is ['.$e->getMessage().']');
1015
                                }
1016
1017
                                $OIDs = $prop->getRelatedOIDs();
1018
1019
                                if (isset($OIDs) && !empty($OIDs[0])) {
1020
                                    // now for each posted OID, create a new RelationLookup record and save
1021
                                    foreach ($OIDs as $oid) {
1022
                                        $newLookUp = new RelationLookup($lookUp->get('leftClassName'), $lookUp->get('rightClassName'));
1023
                                        if ($side == 'left') {
1024
                                            $newLookUp->set('leftID', $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1025
                                            $newLookUp->set('rightID', $oid);
1026
                                        } else {
1027
                                            $newLookUp->set('rightID', $this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1028
                                            $newLookUp->set('leftID', $oid);
1029
                                        }
1030
                                        $newLookUp->save();
1031
                                    }
1032
                                }
1033
                            } catch (Exception $e) {
1034
                                throw new FailedSaveException('Failed to update a MANY-TO-MANY relation on the object, error is ['.$e->getMessage().']');
1035
                            }
1036
                        }
1037
1038
                        // handle the saving of ONE-TO-MANY relation values
1039
                        if ($prop->getRelationType() == 'ONE-TO-MANY') {
1040
                            $prop->setValue($this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1041
                        }
1042
                    }
1043
                }
1044
            } catch (Exception $e) {
1045
                throw new FailedSaveException('Failed to save object, error is ['.$e->getMessage().']');
1046
            }
1047
1048
            $stmt->close();
1049
        } else {
1050
            // there has been an error, so decrement the version number back
1051
            $temp = $this->record->getVersionNumber()->getValue();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1052
            $this->record->set('version_num', $temp - 1);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1053
1054
            throw new FailedSaveException('Failed to save object, SQLite error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1055
        }
1056
1057
        if ($this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1058
            $this->record->saveHistory();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1059
        }
1060
    }
1061
1062
    /**
1063
     * (non-PHPdoc).
1064
     *
1065
     * @see Alpha\Model\ActiveRecordProviderInterface::saveAttribute()
1066
     */
1067
    public function saveAttribute($attribute, $value)
1068
    {
1069
        self::$logger->debug('>>saveAttribute(attribute=['.$attribute.'], value=['.$value.'])');
1070
1071
        $config = ConfigProvider::getInstance();
1072
        $sessionProvider = $config->get('session.provider.name');
1073
        $session = SessionProviderFactory::getInstance($sessionProvider);
1074
1075
        // get the class attributes
1076
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1077
        $properties = $reflection->getProperties();
0 ignored issues
show
Unused Code introduced by
$properties is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1078
1079
        if ($this->record->getVersion() != $this->record->getVersionNumber()->getValue()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1080
            throw new LockingException('Could not save the object as it has been updated by another user.  Please try saving again.');
1081
        }
1082
1083
        // set the "updated by" fields, we can only set the user id if someone is logged in
1084
        if ($session->get('currentUser') != null) {
1085
            $this->record->set('updated_by', $session->get('currentUser')->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1086
        }
1087
1088
        $this->record->set('updated_ts', new Timestamp(date('Y-m-d H:i:s')));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1089
1090
        // assume that it is a persistent object that needs to be updated
1091
        $sqlQuery = 'UPDATE '.$this->record->getTableName().' SET '.$attribute.'=:attribute, version_num=:version, updated_by=:updated_by, updated_ts=:updated_ts WHERE OID=:OID;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1092
1093
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1094
        $stmt = self::getConnection()->prepare($sqlQuery);
1095
1096
        $newVersionNumber = $this->record->getVersionNumber()->getValue() + 1;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1097
1098
        if ($stmt instanceof SQLite3Stmt) {
1099
            if ($this->record->getPropObject($attribute) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1100
                $stmt->bindValue(':attribute', $value, SQLITE3_INTEGER);
1101
            } else {
1102
                $stmt->bindValue(':attribute', $value, SQLITE3_TEXT);
1103
            }
1104
1105
            $updatedBy = $this->record->get('updated_by');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1106
            $updatedTS = $this->record->get('updated_ts');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1107
1108
            $stmt->bindValue(':version', $newVersionNumber, SQLITE3_INTEGER);
1109
            $stmt->bindValue(':updated_by', $updatedBy, SQLITE3_INTEGER);
1110
            $stmt->bindValue(':updated_ts', $updatedTS, SQLITE3_TEXT);
1111
            $stmt->bindValue(':OID', $this->record->getOID(), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1112
1113
            $stmt->execute();
1114
        } else {
1115
            throw new FailedSaveException('Failed to save attribute, error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1116
        }
1117
1118
        $stmt->close();
1119
1120
        $this->record->set($attribute, $value);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1121
        $this->record->set('version_num', $newVersionNumber);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1122
1123
        if ($this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1124
            $this->record->saveHistory();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1125
        }
1126
1127
        self::$logger->debug('<<saveAttribute');
1128
    }
1129
1130
    /**
1131
     * (non-PHPdoc).
1132
     *
1133
     * @see Alpha\Model\ActiveRecordProviderInterface::saveHistory()
1134
     */
1135
    public function saveHistory()
1136
    {
1137
        self::$logger->debug('>>saveHistory()');
1138
1139
        // get the class attributes
1140
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1141
        $properties = $reflection->getProperties();
1142
1143
        $savedFields = array();
1144
        $attributeNames = array();
1145
        $attributeValues = array();
1146
1147
        $sqlQuery = 'INSERT INTO '.$this->record->getTableName().'_history (';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1148
1149
        foreach ($properties as $propObj) {
1150
            $propName = $propObj->name;
1151
            if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1152
                $sqlQuery .= "$propName,";
1153
                $attributeNames[] = $propName;
1154
                $attributeValues[] = $this->record->get($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1155
                $savedFields[] = $propName;
1156
            }
1157
        }
1158
1159
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1160
            $sqlQuery .= 'classname,';
1161
        }
1162
1163
        $sqlQuery = rtrim($sqlQuery, ',');
1164
1165
        $sqlQuery .= ') VALUES (';
1166
1167
        foreach ($savedFields as $savedField) {
1168
            $sqlQuery .= ':'.$savedField.',';
1169
        }
1170
1171
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1172
            $sqlQuery .= ':classname,';
1173
        }
1174
1175
        $sqlQuery = rtrim($sqlQuery, ',').')';
1176
1177
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1178
        self::$logger->debug('Query ['.$sqlQuery.']');
1179
1180
        $stmt = self::getConnection()->prepare($sqlQuery);
1181
1182
        if ($stmt instanceof SQLite3Stmt) {
1183
            foreach ($savedFields as $savedField) {
1184
                if ($this->record->get($savedField) instanceof Integer) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1185
                    $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1186
                } else {
1187
                    $stmt->bindValue(':'.$savedField, $this->record->get($savedField), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1188
                }
1189
            }
1190
1191
            if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1192
                $stmt->bindValue(':classname', get_class($this->record), SQLITE3_TEXT);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1193
            }
1194
1195
            $stmt->execute();
1196
        } else {
1197
            throw new FailedSaveException('Failed to save object history, error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1198
        }
1199
    }
1200
1201
    /**
1202
     * (non-PHPdoc).
1203
     *
1204
     * @see Alpha\Model\ActiveRecordProviderInterface::delete()
1205
     */
1206
    public function delete()
1207
    {
1208
        self::$logger->debug('>>delete()');
1209
1210
        $sqlQuery = 'DELETE FROM '.$this->record->getTableName().' WHERE OID = :OID;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1211
1212
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1213
1214
        $stmt = self::getConnection()->prepare($sqlQuery);
1215
1216
        if ($stmt instanceof SQLite3Stmt) {
1217
            $stmt->bindValue(':OID', $this->record->getOID(), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1218
            $stmt->execute();
1219
            self::$logger->debug('Deleted the object ['.$this->record->getOID().'] of class ['.get_class($this->record).']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1220
        } else {
1221
            throw new FailedDeleteException('Failed to delete object ['.$this->record->getOID().'], error is ['.self::getLastDatabaseError().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1222
        }
1223
1224
        $stmt->close();
1225
1226
        self::$logger->debug('<<delete');
1227
    }
1228
1229
    /**
1230
     * (non-PHPdoc).
1231
     *
1232
     * @see Alpha\Model\ActiveRecordProviderInterface::getVersion()
1233
     */
1234
    public function getVersion()
1235
    {
1236
        self::$logger->debug('>>getVersion()');
1237
1238
        $sqlQuery = 'SELECT version_num FROM '.$this->record->getTableName().' WHERE OID = :OID;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1239
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1240
1241
        $stmt = self::getConnection()->prepare($sqlQuery);
1242
1243
        if ($stmt instanceof SQLite3Stmt) {
1244
            $stmt->bindValue(':OID', $this->record->getOID(), SQLITE3_INTEGER);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1245
1246
            $result = $stmt->execute();
1247
1248
            // there should only ever be one (or none)
1249
            $row = $result->fetchArray(SQLITE3_ASSOC);
1250
1251
            $stmt->close();
1252
        } else {
1253
            self::$logger->warn('The following query caused an unexpected result ['.$sqlQuery.']');
1254
            if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1255
                $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1256
1257
                throw new RecordNotFoundException('Failed to get the version number, table did not exist so had to create!');
1258
            }
1259
1260
            return;
1261
        }
1262
1263
        if (!isset($row['version_num']) || $row['version_num'] < 1) {
1264
            self::$logger->debug('<<getVersion [0]');
1265
1266
            return 0;
1267
        } else {
1268
            $version_num = $row['version_num'];
1269
1270
            self::$logger->debug('<<getVersion ['.$version_num.']');
1271
1272
            return $version_num;
1273
        }
1274
    }
1275
1276
    /**
1277
     * (non-PHPdoc).
1278
     *
1279
     * @see Alpha\Model\ActiveRecordProviderInterface::makeTable()
1280
     */
1281
    public function makeTable()
1282
    {
1283
        self::$logger->debug('>>makeTable()');
1284
1285
        $sqlQuery = 'CREATE TABLE '.$this->record->getTableName().' (OID INTEGER PRIMARY KEY,';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1286
1287
        // get the class attributes
1288
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1289
        $properties = $reflection->getProperties();
1290
1291
        $foreignKeys = array();
1292
1293
        foreach ($properties as $propObj) {
1294
            $propName = $propObj->name;
1295
1296
            if (!in_array($propName, $this->record->getTransientAttributes()) && $propName != 'OID') {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1297
                $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1298
1299
                if ($prop instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) {
1300
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().') NOT NULL,';
0 ignored issues
show
Bug introduced by
The method getSize() does not seem to exist on object<Alpha\Model\Type\RelationLookup>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1301
                } elseif ($prop instanceof Integer) {
1302
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().'),';
1303
                } elseif ($prop instanceof Double) {
1304
                    $sqlQuery .= "$propName REAL(".$prop->getSize(true).'),';
1305
                } elseif ($prop instanceof SmallText) {
1306
                    $sqlQuery .= "$propName TEXT(".$prop->getSize().'),';
1307
                } elseif ($prop instanceof Text) {
1308
                    $sqlQuery .= "$propName TEXT,";
1309
                } elseif ($prop instanceof Boolean) {
1310
                    $sqlQuery .= "$propName INTEGER(1) DEFAULT '0',";
1311
                } elseif ($prop instanceof Date) {
1312
                    $sqlQuery .= "$propName TEXT,";
1313
                } elseif ($prop instanceof Timestamp) {
1314
                    $sqlQuery .= "$propName TEXT,";
1315
                } elseif ($prop instanceof Enum) {
1316
                    $sqlQuery .= "$propName TEXT,";
1317
                } elseif ($prop instanceof DEnum) {
1318
                    $tmp = new DEnum(get_class($this->record).'::'.$propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1319
                    $sqlQuery .= "$propName INTEGER(11),";
1320
                } elseif ($prop instanceof Relation) {
1321
                    $sqlQuery .= "$propName INTEGER(11),";
1322
1323
                    $rel = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1324
1325
                    $relatedField = $rel->getRelatedClassField();
1326
                    $relatedClass = $rel->getRelatedClass();
1327
                    $relatedRecord = new $relatedClass();
1328
                    $tableName = $relatedRecord->getTableName();
1329
                    $foreignKeys[$propName] = array($tableName, $relatedField);
1330
                } else {
1331
                    $sqlQuery .= '';
1332
                }
1333
            }
1334
        }
1335
1336
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1337
            $sqlQuery .= 'classname TEXT(100)';
1338
        } else {
1339
            $sqlQuery = mb_substr($sqlQuery, 0, -1);
1340
        }
1341
1342
        if (count($foreignKeys) > 0) {
1343
            foreach ($foreignKeys as $field => $related) {
1344
                $sqlQuery .= ', FOREIGN KEY ('.$field.') REFERENCES '.$related[0].'('.$related[1].')';
1345
            }
1346
        }
1347
1348
        if (count($this->foreignKeys) > 0) {
1349
            foreach ($this->foreignKeys as $field => $related) {
1350
                $sqlQuery .= ', FOREIGN KEY ('.$field.') REFERENCES '.$related[0].'('.$related[1].')';
1351
            }
1352
        }
1353
1354
        $sqlQuery .= ');';
1355
1356
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1357
1358
        if (!self::getConnection()->exec($sqlQuery)) {
1359
            self::$logger->debug('<<makeTable');
1360
            throw new AlphaException('Failed to create the table ['.$this->record->getTableName().'] for the class ['.get_class($this->record).'], database error is ['.self::getLastDatabaseError().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1361
        }
1362
1363
        // check the table indexes if any additional ones required
1364
        $this->checkIndexes();
1365
1366
        if ($this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1367
            $this->record->makeHistoryTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1368
        }
1369
1370
        self::$logger->debug('<<makeTable');
1371
    }
1372
1373
    /**
1374
     * (non-PHPdoc).
1375
     *
1376
     * @see Alpha\Model\ActiveRecordProviderInterface::makeHistoryTable()
1377
     */
1378
    public function makeHistoryTable()
1379
    {
1380
        self::$logger->debug('>>makeHistoryTable()');
1381
1382
        $sqlQuery = 'CREATE TABLE '.$this->record->getTableName().'_history (OID INTEGER NOT NULL,';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1383
1384
        // get the class attributes
1385
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1386
        $properties = $reflection->getProperties();
1387
1388
        foreach ($properties as $propObj) {
1389
            $propName = $propObj->name;
1390
1391
            if (!in_array($propName, $this->record->getTransientAttributes()) && $propName != 'OID') {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1392
                $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1393
1394
                if ($prop instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) {
1395
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().') NOT NULL,';
0 ignored issues
show
Bug introduced by
The method getSize() does not seem to exist on object<Alpha\Model\Type\RelationLookup>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1396
                } elseif ($prop instanceof Integer) {
1397
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().'),';
1398
                } elseif ($prop instanceof Double) {
1399
                    $sqlQuery .= "$propName REAL(".$prop->getSize(true).'),';
1400
                } elseif ($prop instanceof SmallText) {
1401
                    $sqlQuery .= "$propName TEXT(".$prop->getSize().'),';
1402
                } elseif ($prop instanceof Text) {
1403
                    $sqlQuery .= "$propName TEXT,";
1404
                } elseif ($prop instanceof Boolean) {
1405
                    $sqlQuery .= "$propName INTEGER(1) DEFAULT '0',";
1406
                } elseif ($prop instanceof Date) {
1407
                    $sqlQuery .= "$propName TEXT,";
1408
                } elseif ($prop instanceof Timestamp) {
1409
                    $sqlQuery .= "$propName TEXT,";
1410
                } elseif ($prop instanceof Enum) {
1411
                    $sqlQuery .= "$propName TEXT,";
1412
                } elseif ($prop instanceof DEnum) {
1413
                    $tmp = new DEnum(get_class($this->record).'::'.$propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1414
                    $sqlQuery .= "$propName INTEGER(11),";
1415
                } elseif ($prop instanceof Relation) {
1416
                    $sqlQuery .= "$propName INTEGER(11),";
1417
1418
                    $rel = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1419
1420
                    $relatedField = $rel->getRelatedClassField();
0 ignored issues
show
Unused Code introduced by
$relatedField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1421
                    $relatedClass = $rel->getRelatedClass();
1422
                    $relatedRecord = new $relatedClass();
1423
                    $tableName = $relatedRecord->getTableName();
0 ignored issues
show
Unused Code introduced by
$tableName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1424
                } else {
1425
                    $sqlQuery .= '';
1426
                }
1427
            }
1428
        }
1429
1430
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1431
            $sqlQuery .= 'classname TEXT(100),';
1432
        }
1433
1434
        $sqlQuery .= 'PRIMARY KEY (OID, version_num));';
1435
1436
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1437
1438
        if (!$result = self::getConnection()->query($sqlQuery)) {
1439
            self::$logger->debug('<<makeHistoryTable');
1440
            throw new AlphaException('Failed to create the table ['.$this->record->getTableName().'_history] for the class ['.get_class($this->record).'], database error is ['.self::getLastDatabaseError().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1441
        }
1442
1443
        self::$logger->debug('<<makeHistoryTable');
1444
    }
1445
1446
    /**
1447
     * (non-PHPdoc).
1448
     *
1449
     * @see Alpha\Model\ActiveRecordProviderInterface::rebuildTable()
1450
     */
1451
    public function rebuildTable()
1452
    {
1453
        self::$logger->debug('>>rebuildTable()');
1454
1455
        // the use of "IF EXISTS" here requires SQLite 3.3.0 or above.
1456
        $sqlQuery = 'DROP TABLE IF EXISTS '.$this->record->getTableName().';';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1457
1458
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1459
1460
        if (!$result = self::getConnection()->query($sqlQuery)) {
1461
            self::$logger->debug('<<rebuildTable');
1462
            throw new AlphaException('Failed to drop the table ['.$this->record->getTableName().'] for the class ['.get_class($this->record).'], database error is ['.self::getLastDatabaseError().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1463
        }
1464
1465
        $this->record->makeTable();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1466
1467
        self::$logger->debug('<<rebuildTable');
1468
    }
1469
1470
    /**
1471
     * (non-PHPdoc).
1472
     *
1473
     * @see Alpha\Model\ActiveRecordProviderInterface::dropTable()
1474
     */
1475
    public function dropTable($tableName = null)
1476
    {
1477
        self::$logger->debug('>>dropTable()');
1478
1479
        if ($tableName === null) {
1480
            $tableName = $this->record->getTableName();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1481
        }
1482
1483
        // the use of "IF EXISTS" here requires SQLite 3.3.0 or above.
1484
        $sqlQuery = 'DROP TABLE IF EXISTS '.$tableName.';';
1485
1486
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1487
1488
        if (!$result = self::getConnection()->query($sqlQuery)) {
1489
            self::$logger->debug('<<dropTable');
1490
            throw new AlphaException('Failed to drop the table ['.$tableName.'] for the class ['.get_class($this->record).'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1491
        }
1492
1493
        if ($this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1494
            $sqlQuery = 'DROP TABLE IF EXISTS '.$tableName.'_history;';
1495
1496
            $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1497
1498
            if (!$result = self::getConnection()->query($sqlQuery)) {
1499
                self::$logger->debug('<<dropTable');
1500
                throw new AlphaException('Failed to drop the table ['.$tableName.'_history] for the class ['.get_class($this->record).'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1501
            }
1502
        }
1503
1504
        self::$logger->debug('<<dropTable');
1505
    }
1506
1507
    /**
1508
     * (non-PHPdoc).
1509
     *
1510
     * @see Alpha\Model\ActiveRecordProviderInterface::addProperty()
1511
     */
1512
    public function addProperty($propName)
1513
    {
1514
        self::$logger->debug('>>addProperty(propName=['.$propName.'])');
1515
1516
        $sqlQuery = 'ALTER TABLE '.$this->record->getTableName().' ADD ';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1517
1518
        if ($this->isTableOverloaded() && $propName == 'classname') {
1519
            $sqlQuery .= 'classname TEXT(100)';
1520
        } else {
1521
            if (!in_array($propName, $this->record->getDefaultAttributes()) && !in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1522
                $prop = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1523
1524
                if ($prop instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) {
1525
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().') NOT NULL';
0 ignored issues
show
Bug introduced by
The method getSize() does not seem to exist on object<Alpha\Model\Type\RelationLookup>.

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
1526
                } elseif ($prop instanceof Integer) {
1527
                    $sqlQuery .= "$propName INTEGER(".$prop->getSize().')';
1528
                } elseif ($prop instanceof Double) {
1529
                    $sqlQuery .= "$propName REAL(".$prop->getSize(true).')';
1530
                } elseif ($prop instanceof SmallText) {
1531
                    $sqlQuery .= "$propName TEXT(".$prop->getSize().')';
1532
                } elseif ($prop instanceof Text) {
1533
                    $sqlQuery .= "$propName TEXT";
1534
                } elseif ($prop instanceof Boolean) {
1535
                    $sqlQuery .= "$propName INTEGER(1) DEFAULT '0'";
1536
                } elseif ($prop instanceof Date) {
1537
                    $sqlQuery .= "$propName TEXT";
1538
                } elseif ($prop instanceof Timestamp) {
1539
                    $sqlQuery .= "$propName TEXT";
1540
                } elseif ($prop instanceof Enum) {
1541
                    $sqlQuery .= "$propName TEXT";
1542
                } elseif ($prop instanceof DEnum) {
1543
                    $tmp = new DEnum(get_class($this->record).'::'.$propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
Unused Code introduced by
$tmp is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1544
                    $sqlQuery .= "$propName INTEGER(11)";
1545
                } elseif ($prop instanceof Relation) {
1546
                    $sqlQuery .= "$propName INTEGER(11)";
1547
1548
                    $rel = $this->record->getPropObject($propName);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1549
1550
                    $relatedField = $rel->getRelatedClassField();
0 ignored issues
show
Unused Code introduced by
$relatedField is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1551
                    $relatedClass = $rel->getRelatedClass();
1552
                    $relatedRecord = new $relatedClass();
1553
                    $tableName = $relatedRecord->getTableName();
0 ignored issues
show
Unused Code introduced by
$tableName is not used, you could remove the assignment.

This check looks for variable assignements that are either overwritten by other assignments or where the variable is not used subsequently.

$myVar = 'Value';
$higher = false;

if (rand(1, 6) > 3) {
    $higher = true;
} else {
    $higher = false;
}

Both the $myVar assignment in line 1 and the $higher assignment in line 2 are dead. The first because $myVar is never used and the second because $higher is always overwritten for every possible time line.

Loading history...
1554
                } else {
1555
                    $sqlQuery .= '';
1556
                }
1557
            }
1558
        }
1559
1560
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1561
1562
        if (!$result = self::getConnection()->query($sqlQuery)) {
1563
            self::$logger->debug('<<addProperty');
1564
            throw new AlphaException('Failed to add the new attribute ['.$propName.'] to the table ['.$this->record->getTableName().'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1565
        } else {
1566
            self::$logger->info('Successfully added the ['.$propName.'] column onto the ['.$this->record->getTableName().'] table for the class ['.get_class($this->record).']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1567
        }
1568
1569
        if ($this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1570
            $sqlQuery = str_replace($this->record->getTableName(), $this->record->getTableName().'_history', $sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1571
1572
            if (!$result = self::getConnection()->query($sqlQuery)) {
1573
                self::$logger->debug('<<addProperty');
1574
                throw new AlphaException('Failed to add the new attribute ['.$propName.'] to the table ['.$this->record->getTableName().'_history], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1575
            } else {
1576
                self::$logger->info('Successfully added the ['.$propName.'] column onto the ['.$this->record->getTableName().'_history] table for the class ['.get_class($this->record).']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1577
            }
1578
        }
1579
1580
        self::$logger->debug('<<addProperty');
1581
    }
1582
1583
    /**
1584
     * (non-PHPdoc).
1585
     *
1586
     * @see Alpha\Model\ActiveRecordProviderInterface::getMAX()
1587
     */
1588
    public function getMAX()
1589
    {
1590
        self::$logger->debug('>>getMAX()');
1591
1592
        $sqlQuery = 'SELECT MAX(OID) AS max_OID FROM '.$this->record->getTableName();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1593
1594
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1595
1596
        try {
1597
            $result = $this->record->query($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1598
1599
            $row = $result[0];
1600
1601
            if (isset($row['max_OID'])) {
1602
                self::$logger->debug('<<getMAX ['.$row['max_OID'].']');
1603
1604
                return $row['max_OID'];
1605
            } else {
1606
                throw new AlphaException('Failed to get the MAX ID for the class ['.get_class($this->record).'] from the table ['.$this->record->getTableName().'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1607
            }
1608
        } catch (Exception $e) {
1609
            self::$logger->debug('<<getMAX');
1610
            throw new AlphaException($e->getMessage());
1611
        }
1612
    }
1613
1614
    /**
1615
     * (non-PHPdoc).
1616
     *
1617
     * @see Alpha\Model\ActiveRecordProviderInterface::getCount()
1618
     */
1619
    public function getCount($attributes = array(), $values = array())
1620
    {
1621
        self::$logger->debug('>>getCount(attributes=['.var_export($attributes, true).'], values=['.var_export($values, true).'])');
1622
1623
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1624
            $whereClause = ' WHERE classname = \''.addslashes(get_class($this->record)).'\' AND';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1625
        } else {
1626
            $whereClause = ' WHERE';
1627
        }
1628
1629
        $count = count($attributes);
1630
1631
        for ($i = 0; $i < $count; ++$i) {
1632
            $whereClause .= ' '.$attributes[$i].' = \''.$values[$i].'\' AND';
1633
            self::$logger->debug($whereClause);
1634
        }
1635
        // remove the last " AND"
1636
        $whereClause = mb_substr($whereClause, 0, -4);
1637
1638
        if ($whereClause != ' WHERE') {
1639
            $sqlQuery = 'SELECT COUNT(OID) AS class_count FROM '.$this->record->getTableName().$whereClause;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1640
        } else {
1641
            $sqlQuery = 'SELECT COUNT(OID) AS class_count FROM '.$this->record->getTableName();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1642
        }
1643
1644
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1645
1646
        if (!$result = self::getConnection()->query($sqlQuery)) {
1647
            self::$logger->debug('<<getCount');
1648
            throw new AlphaException('Failed to get the count for the class ['.get_class($this->record).'] from the table ['.$this->record->getTableName().'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1649
        } else {
1650
            $row = $result->fetchArray(SQLITE3_ASSOC);
1651
1652
            self::$logger->debug('<<getCount ['.$row['class_count'].']');
1653
1654
            return $row['class_count'];
1655
        }
1656
    }
1657
1658
    /**
1659
     * (non-PHPdoc).
1660
     *
1661
     * @see Alpha\Model\ActiveRecordProviderInterface::getHistoryCount()
1662
     */
1663
    public function getHistoryCount()
1664
    {
1665
        self::$logger->debug('>>getHistoryCount()');
1666
1667
        if (!$this->record->getMaintainHistory()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1668
            throw new AlphaException('getHistoryCount method called on a DAO where no history is maintained!');
1669
        }
1670
1671
        $sqlQuery = 'SELECT COUNT(OID) AS object_count FROM '.$this->record->getTableName().'_history WHERE OID='.$this->record->getOID();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1672
1673
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1674
        self::$logger->debug('query ['.$sqlQuery.']');
1675
1676
        if (!$result = self::getConnection()->query($sqlQuery)) {
1677
            self::$logger->debug('<<getHistoryCount');
1678
            throw new AlphaException('Failed to get the history count for the business object ['.$this->record->getOID().'] from the table ['.$this->record->getTableName().'_history], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1679
        } else {
1680
            $row = $result->fetchArray(SQLITE3_ASSOC);
1681
1682
            self::$logger->debug('<<getHistoryCount ['.$row['object_count'].']');
1683
1684
            return $row['object_count'];
1685
        }
1686
    }
1687
1688
    /**
1689
     * Given that Enum values are not saved in the database for SQLite, an implementation is not required here.
1690
     *
1691
     * (non-PHPdoc)
1692
     *
1693
     * @see Alpha\Model\ActiveRecordProviderInterface::setEnumOptions()
1694
     *
1695
     * @throws \Alpha\Exception\NotImplementedException
1696
     */
1697
    public function setEnumOptions()
1698
    {
1699
        throw new NotImplementedException('ActiveRecordProviderInterface::setEnumOptions() not implemented by the SQLite3 provider');
1700
    }
1701
1702
    /**
1703
     * (non-PHPdoc).
1704
     *
1705
     * @see Alpha\Model\ActiveRecordProviderInterface::checkTableExists()
1706
     */
1707
    public function checkTableExists($checkHistoryTable = false)
1708
    {
1709
        self::$logger->debug('>>checkTableExists(checkHistoryTable=['.$checkHistoryTable.'])');
1710
1711
        $tableExists = false;
1712
1713
        $sqlQuery = 'SELECT name FROM sqlite_master WHERE type = "table";';
1714
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1715
1716
        $result = self::getConnection()->query($sqlQuery);
1717
1718
        $tableName = ($checkHistoryTable ? $this->record->getTableName().'_history' : $this->record->getTableName());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1719
1720
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1721
            if (strtolower($row['name']) == mb_strtolower($tableName)) {
1722
                $tableExists = true;
1723
            }
1724
        }
1725
1726
        if ($result) {
1727
            self::$logger->debug('<<checkTableExists ['.$tableExists.']');
1728
1729
            return $tableExists;
1730
        } else {
1731
            self::$logger->debug('<<checkTableExists');
1732
            throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']');
1733
        }
1734
    }
1735
1736
    /**
1737
     * (non-PHPdoc).
1738
     *
1739
     * @see Alpha\Model\ActiveRecordProviderInterface::checkRecordTableExists()
1740
     */
1741
    public static function checkRecordTableExists($RecordClassName, $checkHistoryTable = false)
1742
    {
1743
        if (self::$logger == null) {
1744
            self::$logger = new Logger('ActiveRecordProviderSQLite');
1745
        }
1746
        self::$logger->debug('>>checkRecordTableExists(RecordClassName=['.$RecordClassName.'], checkHistoryTable=['.$checkHistoryTable.'])');
1747
1748
        if (!class_exists($RecordClassName)) {
1749
            throw new IllegalArguementException('The classname provided ['.$checkHistoryTable.'] is not defined!');
1750
        }
1751
1752
        $tableName = $RecordClassName::TABLE_NAME;
1753
1754
        if (empty($tableName)) {
1755
            $tableName = mb_substr($RecordClassName, 0, mb_strpos($RecordClassName, '_'));
1756
        }
1757
1758
        if ($checkHistoryTable) {
1759
            $tableName .= '_history';
1760
        }
1761
1762
        $tableExists = false;
1763
1764
        $sqlQuery = 'SELECT name FROM sqlite_master WHERE type = "table";';
1765
1766
        $result = self::getConnection()->query($sqlQuery);
1767
1768
        while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1769
            if ($row['name'] == $tableName) {
1770
                $tableExists = true;
1771
            }
1772
        }
1773
1774
        if ($result) {
1775
            self::$logger->debug('<<checkRecordTableExists ['.($tableExists ? 'true' : 'false').']');
1776
1777
            return $tableExists;
1778
        } else {
1779
            self::$logger->debug('<<checkRecordTableExists');
1780
            throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']');
1781
        }
1782
    }
1783
1784
    /**
1785
     * (non-PHPdoc).
1786
     *
1787
     * @see Alpha\Model\ActiveRecordProviderInterface::checkTableNeedsUpdate()
1788
     */
1789
    public function checkTableNeedsUpdate()
1790
    {
1791
        self::$logger->debug('>>checkTableNeedsUpdate()');
1792
1793
        if (!$this->record->checkTableExists()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1794
            return false;
1795
        }
1796
1797
        $updateRequired = false;
1798
1799
        $matchCount = 0;
1800
1801
        $query = 'PRAGMA table_info('.$this->record->getTableName().')';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1802
        $result = self::getConnection()->query($query);
1803
        $this->record->setLastQuery($query);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1804
1805
        // get the class attributes
1806
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1807
        $properties = $reflection->getProperties();
1808
1809
        foreach ($properties as $propObj) {
1810
            $propName = $propObj->name;
1811
            if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1812
                $foundMatch = false;
1813
1814
                while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1815
                    if ($propName == $row['name']) {
1816
                        $foundMatch = true;
1817
                        break;
1818
                    }
1819
                }
1820
1821
                if (!$foundMatch) {
1822
                    --$matchCount;
1823
                }
1824
1825
                $result->reset();
1826
            }
1827
        }
1828
1829
        // check for the "classname" field in overloaded tables
1830
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1831
            $foundMatch = false;
1832
1833
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1834
                if ('classname' == $row['name']) {
1835
                    $foundMatch = true;
1836
                    break;
1837
                }
1838
            }
1839
            if (!$foundMatch) {
1840
                --$matchCount;
1841
            }
1842
        }
1843
1844
        if ($matchCount != 0) {
1845
            $updateRequired = true;
1846
        }
1847
1848
        if (!$result) {
1849
            self::$logger->debug('<<checkTableNeedsUpdate');
1850
            throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']');
1851
        } else {
1852
            // check the table indexes
1853
            try {
1854
                $this->checkIndexes();
1855
            } catch (AlphaException $ae) {
1856
                self::$logger->warn("Error while checking database indexes:\n\n".$ae->getMessage());
1857
            }
1858
1859
            self::$logger->debug('<<checkTableNeedsUpdate ['.$updateRequired.']');
1860
1861
            return $updateRequired;
1862
        }
1863
    }
1864
1865
    /**
1866
     * (non-PHPdoc).
1867
     *
1868
     * @see Alpha\Model\ActiveRecordProviderInterface::findMissingFields()
1869
     */
1870
    public function findMissingFields()
1871
    {
1872
        self::$logger->debug('>>findMissingFields()');
1873
1874
        $missingFields = array();
1875
        $matchCount = 0;
1876
1877
        $sqlQuery = 'PRAGMA table_info('.$this->record->getTableName().')';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1878
        $result = self::getConnection()->query($sqlQuery);
1879
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1880
1881
        // get the class attributes
1882
        $reflection = new ReflectionClass(get_class($this->record));
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1883
        $properties = $reflection->getProperties();
1884
1885
        foreach ($properties as $propObj) {
1886
            $propName = $propObj->name;
1887
            if (!in_array($propName, $this->record->getTransientAttributes())) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1888
                while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1889
                    if ($propName == $row['name']) {
1890
                        ++$matchCount;
1891
                        break;
1892
                    }
1893
                }
1894
                $result->reset();
1895
            } else {
1896
                ++$matchCount;
1897
            }
1898
1899
            if ($matchCount == 0) {
1900
                array_push($missingFields, $propName);
1901
            } else {
1902
                $matchCount = 0;
1903
            }
1904
        }
1905
1906
        // check for the "classname" field in overloaded tables
1907
        if ($this->record->isTableOverloaded()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1908
            $foundMatch = false;
1909
1910
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1911
                if ('classname' == $row['name']) {
1912
                    $foundMatch = true;
1913
                    break;
1914
                }
1915
            }
1916
            if (!$foundMatch) {
1917
                array_push($missingFields, 'classname');
1918
            }
1919
        }
1920
1921
        if (!$result) {
1922
            throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']');
1923
        }
1924
1925
        self::$logger->debug('<<findMissingFields ['.var_export($missingFields, true).']');
1926
1927
        return $missingFields;
1928
    }
1929
1930
    /**
1931
     * (non-PHPdoc).
1932
     *
1933
     * @see Alpha\Model\ActiveRecordProviderInterface::getIndexes()
1934
     */
1935
    public function getIndexes()
1936
    {
1937
        self::$logger->debug('>>getIndexes()');
1938
1939
        $sqlQuery = "SELECT name FROM sqlite_master WHERE type='index' AND tbl_name='".$this->record->getTableName()."'";
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1940
1941
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1942
1943
        $indexNames = array();
1944
1945
        if (!$result = self::getConnection()->query($sqlQuery)) {
1946
            throw new AlphaException('Failed to access the system database correctly, error is ['.self::getLastDatabaseError().']');
1947
        } else {
1948
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1949
                array_push($indexNames, $row['name']);
1950
            }
1951
        }
1952
1953
        // in SQLite foreign keys are not stored in sqlite_master, so we have to run a different query and append the results
1954
        $sqlQuery = 'PRAGMA foreign_key_list('.$this->record->getTableName().')';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1955
        
1956
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1957
1958
        if (!$result = self::getConnection()->query($sqlQuery)) {
1959
            self::$logger->warn('Error during pragma table foreign key lookup ['.self::getLastDatabaseError().']');
1960
        } else {
1961
            while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
1962
                // SQLite does not name FK indexes, so we will return a fake name based the same convention used in MySQL
1963
                $fakeIndexName = $this->record->getTableName().'_'.$row['from'].'_fk_idx';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1964
                array_push($indexNames, $fakeIndexName);
1965
            }
1966
        }
1967
1968
        self::$logger->debug('<<getIndexes');
1969
1970
        return $indexNames;
1971
    }
1972
1973
    /**
1974
     * Checks to see if all of the indexes are in place for the record's table, creates those that are missing.
1975
     *
1976
     * @since 1.2
1977
     */
1978
    private function checkIndexes()
1979
    {
1980
        self::$logger->debug('>>checkIndexes()');
1981
1982
        $indexNames = $this->record->getIndexes();
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1983
1984
        // process unique keys
1985
        foreach ($this->record->getUniqueAttributes() as $prop) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
1986
            // check for composite indexes
1987
            if (mb_strpos($prop, '+')) {
1988
                $attributes = explode('+', $prop);
1989
1990
                $index_exists = false;
1991
                foreach ($indexNames as $index) {
1992
                    if ($attributes[0].'_'.$attributes[1].'_unq_idx' == $index) {
1993
                        $index_exists = true;
1994
                    }
1995
                    if (count($attributes) == 3) {
1996
                        if ($attributes[0].'_'.$attributes[1].'_'.$attributes[2].'_unq_idx' == $index) {
1997
                            $index_exists = true;
1998
                        }
1999
                    }
2000
                }
2001
2002
                if (!$index_exists) {
2003
                    if (count($attributes) == 3) {
2004
                        $this->record->createUniqueIndex($attributes[0], $attributes[1], $attributes[2]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2005
                    } else {
2006
                        $this->record->createUniqueIndex($attributes[0], $attributes[1]);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2007
                    }
2008
                }
2009
            } else {
2010
                $index_exists = false;
2011
                foreach ($indexNames as $index) {
2012
                    if ($prop.'_unq_idx' == $index) {
2013
                        $index_exists = true;
2014
                    }
2015
                }
2016
2017
                if (!$index_exists) {
2018
                    $this->createUniqueIndex($prop);
2019
                }
2020
            }
2021
        }
2022
2023
        self::$logger->debug('<<checkIndexes');
2024
    }
2025
2026
    /**
2027
     * Note that SQLite 3.6.19 is requrired for foreign key support.
2028
     *
2029
     * (non-PHPdoc)
2030
     *
2031
     * @see Alpha\Model\ActiveRecordProviderInterface::createForeignIndex()
2032
     */
2033
    public function createForeignIndex($attributeName, $relatedClass, $relatedClassAttribute, $indexName = null)
2034
    {
2035
        self::$logger->info('>>createForeignIndex(attributeName=['.$attributeName.'], relatedClass=['.$relatedClass.'], relatedClassAttribute=['.$relatedClassAttribute.'], indexName=['.$indexName.']');
2036
2037
        /*
2038
         * High-level approach
2039
         *
2040
         * 1. Rename the source table to [tablename]_temp
2041
         * 2. Create a new [tablename] table, with the new FK in place.
2042
         * 3. Copy all of the data from [tablename]_temp to [tablename].
2043
         * 4. Drop [tablename]_temp.
2044
         */
2045
        try {
2046
            ActiveRecord::begin($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2047
2048
            // rename the table to [tablename]_temp
2049
            $query = 'ALTER TABLE '.$this->record->getTableName().' RENAME TO '.$this->record->getTableName().'_temp;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2050
            $this->record->setLastQuery($query);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2051
            self::getConnection()->query($query);
2052
2053
            self::$logger->info('Renamed the table ['.$this->record->getTableName().'] to ['.$this->record->getTableName().'_temp]');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2054
2055
            // now create the new table with the FK in place
2056
            $record = new $relatedClass();
2057
            $tableName = $record->getTableName();
2058
            $this->foreignKeys[$attributeName] = array($tableName, $relatedClassAttribute);
2059
2060
            $this->makeTable();
2061
2062
            self::$logger->info('Made a new copy of the table ['.$this->record->getTableName().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2063
2064
            // copy all of the old data to the new table
2065
            $query = 'INSERT INTO '.$this->record->getTableName().' SELECT * FROM '.$this->record->getTableName().'_temp;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2066
            $this->record->setLastQuery($query);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2067
            self::getConnection()->query($query);
2068
2069
            self::$logger->info('Copied all of the data from ['.$this->record->getTableName().'] to ['.$this->record->getTableName().'_temp]');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2070
2071
            // finally, drop the _temp table and commit the changes
2072
            $this->record->dropTable($this->record->getTableName().'_temp');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2073
2074
            self::$logger->info('Dropped the table ['.$this->record->getTableName().'_temp]');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2075
2076
            ActiveRecord::commit($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2077
        } catch (Exception $e) {
2078
            ActiveRecord::rollback($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2079
2080
            throw new FailedIndexCreateException('Failed to create the index ['.$attributeName.'] on ['.$this->record->getTableName().'], error is ['.$e->getMessage().'], query ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2081
        }
2082
2083
        self::$logger->info('<<createForeignIndex');
2084
    }
2085
2086
    /**
2087
     * (non-PHPdoc).
2088
     *
2089
     * @see Alpha\Model\ActiveRecordProviderInterface::createUniqueIndex()
2090
     */
2091
    public function createUniqueIndex($attribute1Name, $attribute2Name = '', $attribute3Name = '')
2092
    {
2093
        self::$logger->debug('>>createUniqueIndex(attribute1Name=['.$attribute1Name.'], attribute2Name=['.$attribute2Name.'], attribute3Name=['.$attribute3Name.'])');
2094
2095
        $sqlQuery = '';
2096
2097
        if ($attribute2Name != '' && $attribute3Name != '') {
2098
            $sqlQuery = 'CREATE UNIQUE INDEX IF NOT EXISTS '.$attribute1Name.'_'.$attribute2Name.'_'.$attribute3Name.'_unq_idx ON '.$this->record->getTableName().' ('.$attribute1Name.','.$attribute2Name.','.$attribute3Name.');';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2099
        }
2100
2101
        if ($attribute2Name != '' && $attribute3Name == '') {
2102
            $sqlQuery = 'CREATE UNIQUE INDEX IF NOT EXISTS '.$attribute1Name.'_'.$attribute2Name.'_unq_idx ON '.$this->record->getTableName().' ('.$attribute1Name.','.$attribute2Name.');';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2103
        }
2104
2105
        if ($attribute2Name == '' && $attribute3Name == '') {
2106
            $sqlQuery = 'CREATE UNIQUE INDEX IF NOT EXISTS '.$attribute1Name.'_unq_idx ON '.$this->record->getTableName().' ('.$attribute1Name.');';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2107
        }
2108
2109
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2110
2111
        $result = self::getConnection()->query($sqlQuery);
2112
2113
        if ($result) {
2114
            self::$logger->debug('Successfully created the unique index on ['.$this->record->getTableName().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2115
        } else {
2116
            throw new FailedIndexCreateException('Failed to create the unique index on ['.$this->record->getTableName().'], error is ['.self::getConnection()->lastErrorMsg().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2117
        }
2118
2119
        self::$logger->debug('<<createUniqueIndex');
2120
    }
2121
2122
    /**
2123
     * (non-PHPdoc).
2124
     *
2125
     * @see Alpha\Model\ActiveRecordProviderInterface::reload()
2126
     */
2127
    public function reload()
2128
    {
2129
        self::$logger->debug('>>reload()');
2130
2131
        if (!$this->record->isTransient()) {
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2132
            $this->record->load($this->record->getOID());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2133
        } else {
2134
            throw new AlphaException('Cannot reload transient object from database!');
2135
        }
2136
2137
        self::$logger->debug('<<reload');
2138
    }
2139
2140
    /**
2141
     * (non-PHPdoc).
2142
     *
2143
     * @see Alpha\Model\ActiveRecordProviderInterface::checkRecordExists()
2144
     */
2145
    public function checkRecordExists($OID)
2146
    {
2147
        self::$logger->debug('>>checkRecordExists(OID=['.$OID.'])');
2148
2149
        $sqlQuery = 'SELECT OID FROM '.$this->record->getTableName().' WHERE OID = :OID;';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2150
        $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2151
        $stmt = self::getConnection()->prepare($sqlQuery);
2152
2153
        if ($stmt instanceof SQLite3Stmt) {
2154
            $stmt->bindValue(':OID', $OID, SQLITE3_INTEGER);
2155
2156
            $result = $stmt->execute();
2157
2158
            // there should only ever be one (or none)
2159
            $row = $result->fetchArray(SQLITE3_ASSOC);
2160
2161
            $stmt->close();
2162
        } else {
2163
            self::$logger->debug('<<checkRecordExists');
2164
            throw new AlphaException('Failed to check for the record ['.$OID.'] on the class ['.get_class($this->record).'] from the table ['.$this->record->getTableName().'], query is ['.$this->record->getLastQuery().']');
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2165
        }
2166
2167
        if (!isset($row['OID'])) {
2168
            self::$logger->debug('<<checkRecordExists [false]');
2169
2170
            return false;
2171
        } else {
2172
            self::$logger->debug('<<checkRecordExists [true]');
2173
2174
            return true;
2175
        }
2176
    }
2177
2178
    /**
2179
     * (non-PHPdoc).
2180
     *
2181
     * @see Alpha\Model\ActiveRecordProviderInterface::isTableOverloaded()
2182
     */
2183
    public function isTableOverloaded()
2184
    {
2185
        self::$logger->debug('>>isTableOverloaded()');
2186
2187
        $reflection = new ReflectionClass($this->record);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2188
        $classname = $reflection->getShortName();
2189
        $tablename = ucfirst($this->record->getTableName());
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2190
2191
        // use reflection to check to see if we are dealing with a persistent type (e.g. DEnum) which are never overloaded
2192
        $implementedInterfaces = $reflection->getInterfaces();
2193
2194
        foreach ($implementedInterfaces as $interface) {
2195
            if ($interface->name == 'Alpha\Model\Type\TypeInterface') {
2196
                self::$logger->debug('<<isTableOverloaded [false]');
2197
2198
                return false;
2199
            }
2200
        }
2201
2202
        if ($classname != $tablename) {
2203
            // loop over all records to see if there is one using the same table as this record
2204
2205
            $Recordclasses = ActiveRecord::getRecordClassNames();
2206
2207
            foreach ($Recordclasses as $RecordclassName) {
2208
                $reflection = new ReflectionClass($RecordclassName);
2209
                $classname = $reflection->getShortName();
2210
                if ($tablename == $classname) {
2211
                    self::$logger->debug('<<isTableOverloaded [true]');
2212
2213
                    return true;
2214
                }
2215
            }
2216
            self::$logger->debug('<<isTableOverloaded');
2217
            throw new BadTableNameException('The table name ['.$tablename.'] for the class ['.$classname.'] is invalid as it does not match a Record definition in the system!');
2218
        } else {
2219
            // check to see if there is already a "classname" column in the database for this record
2220
            $sqlQuery = 'PRAGMA table_info('.$this->record->getTableName().')';
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2221
            $result = self::getConnection()->query($sqlQuery);
2222
            $this->record->setLastQuery($sqlQuery);
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2223
2224
            if (!$result) {
2225
                self::$logger->warn('Error during pragma table info lookup ['.self::getLastDatabaseError().']');
2226
            } else {
2227
                while ($row = $result->fetchArray(SQLITE3_ASSOC)) {
2228
                    if ('classname' == $row['name']) {
2229
                        self::$logger->debug('<<isTableOverloaded [true]');
2230
2231
                        return true;
2232
                    }
2233
                }
2234
            }
2235
2236
            self::$logger->debug('<<isTableOverloaded [false]');
2237
2238
            return false;
2239
        }
2240
    }
2241
2242
    /**
2243
     * (non-PHPdoc).
2244
     *
2245
     * @see Alpha\Model\ActiveRecordProviderInterface::begin()
2246
     */
2247
    public static function begin()
2248
    {
2249
        if (self::$logger == null) {
2250
            self::$logger = new Logger('ActiveRecordProviderSQLite');
2251
        }
2252
        self::$logger->debug('>>begin()');
2253
2254
        if (!self::getConnection()->exec('BEGIN')) {
2255
            throw new AlphaException('Error beginning a new transaction, error is ['.self::getLastDatabaseError().']');
2256
        }
2257
2258
        self::$logger->debug('<<begin');
2259
    }
2260
2261
    /**
2262
     * (non-PHPdoc).
2263
     *
2264
     * @see Alpha\Model\ActiveRecordProviderInterface::commit()
2265
     */
2266
    public static function commit()
2267
    {
2268
        if (self::$logger == null) {
2269
            self::$logger = new Logger('ActiveRecordProviderSQLite');
2270
        }
2271
        self::$logger->debug('>>commit()');
2272
2273
        if (!self::getConnection()->exec('COMMIT')) {
2274
            throw new AlphaException('Error commiting a transaction, error is ['.self::getLastDatabaseError().']');
2275
        }
2276
2277
        self::$logger->debug('<<commit');
2278
    }
2279
2280
    /**
2281
     * (non-PHPdoc).
2282
     *
2283
     * @see Alpha\Model\ActiveRecordProviderInterface::rollback()
2284
     */
2285
    public static function rollback()
2286
    {
2287
        if (self::$logger == null) {
2288
            self::$logger = new Logger('ActiveRecordProviderSQLite');
2289
        }
2290
2291
        self::$logger->debug('>>rollback()');
2292
2293
        try {
2294
            self::getConnection()->exec('ROLLBACK');
2295
            self::disconnect();
2296
        } catch (Exception $e) {
2297
            if (mb_strpos($e->getMessage(), 'cannot rollback - no transaction is active') === false) { // just filtering out errors where the rollback failed due to no current transaction
2298
                throw new AlphaException('Error rolling back a transaction, error is ['.self::getLastDatabaseError().']');
2299
            }
2300
        }
2301
2302
        self::$logger->debug('<<rollback');
2303
    }
2304
2305
    /**
2306
     * (non-PHPdoc).
2307
     *
2308
     * @see Alpha\Model\ActiveRecordProviderInterface::setRecord()
2309
     */
2310
    public function setRecord($Record)
2311
    {
2312
        $this->record = $Record;
0 ignored issues
show
Bug introduced by
The property record does not seem to exist. Did you mean Record?

An attempt at access to an undefined property has been detected. This may either be a typographical error or the property has been renamed but there are still references to its old name.

If you really want to allow access to undefined properties, you can define magic methods to allow access. See the php core documentation on Overloading.

Loading history...
2313
    }
2314
2315
    /**
2316
     * (non-PHPdoc).
2317
     *
2318
     * @see Alpha\Model\ActiveRecordProviderInterface::checkDatabaseExists()
2319
     */
2320
    public static function checkDatabaseExists()
2321
    {
2322
        $config = ConfigProvider::getInstance();
2323
2324
        return file_exists($config->get('db.file.path'));
2325
    }
2326
2327
    /**
2328
     * (non-PHPdoc).
2329
     *
2330
     * @see Alpha\Model\ActiveRecordProviderInterface::createDatabase()
2331
     */
2332
    public static function createDatabase()
2333
    {
2334
        $config = ConfigProvider::getInstance();
2335
2336
        if (!self::checkDatabaseExists()) {
2337
            fopen($config->get('db.file.path'), 'x+');
2338
        }
2339
    }
2340
2341
    /**
2342
     * (non-PHPdoc).
2343
     *
2344
     * @see Alpha\Model\ActiveRecordProviderInterface::dropDatabase()
2345
     */
2346
    public static function dropDatabase()
2347
    {
2348
        $config = ConfigProvider::getInstance();
2349
2350
        if (self::checkDatabaseExists()) {
2351
            unlink($config->get('db.file.path'));
2352
        }
2353
    }
2354
}
2355