Passed
Push — 4.3 ( 28cdbc...a1e8cd )
by David
14:50 queued 13:41
created

TDBMService::setTableToBeanMap()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 4
rs 10
c 0
b 0
f 0
cc 1
eloc 2
nc 1
nop 1
1
<?php
2
3
/*
4
 Copyright (C) 2006-2016 David Négrier - THE CODING MACHINE
5
6
This program is free software; you can redistribute it and/or modify
7
it under the terms of the GNU General Public License as published by
8
the Free Software Foundation; either version 2 of the License, or
9
(at your option) any later version.
10
11
This program is distributed in the hope that it will be useful,
12
but WITHOUT ANY WARRANTY; without even the implied warranty of
13
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
GNU General Public License for more details.
15
16
You should have received a copy of the GNU General Public License
17
along with this program; if not, write to the Free Software
18
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
19
*/
20
21
namespace Mouf\Database\TDBM;
22
23
use Doctrine\Common\Cache\Cache;
24
use Doctrine\Common\Cache\VoidCache;
25
use Doctrine\DBAL\Connection;
26
use Doctrine\DBAL\Schema\Column;
27
use Doctrine\DBAL\Schema\ForeignKeyConstraint;
28
use Doctrine\DBAL\Schema\Schema;
29
use Doctrine\DBAL\Schema\Table;
30
use Doctrine\DBAL\Types\Type;
31
use Mouf\Database\MagicQuery;
32
use Mouf\Database\SchemaAnalyzer\SchemaAnalyzer;
33
use Mouf\Database\TDBM\QueryFactory\FindObjectsFromSqlQueryFactory;
34
use Mouf\Database\TDBM\QueryFactory\FindObjectsQueryFactory;
35
use Mouf\Database\TDBM\Utils\NamingStrategyInterface;
36
use Mouf\Database\TDBM\Utils\TDBMDaoGenerator;
37
use Phlib\Logger\LevelFilter;
38
use Psr\Log\LoggerInterface;
39
use Psr\Log\LogLevel;
40
use Psr\Log\NullLogger;
41
42
/**
43
 * The TDBMService class is the main TDBM class. It provides methods to retrieve TDBMObject instances
44
 * from the database.
45
 *
46
 * @author David Negrier
47
 * @ExtendedAction {"name":"Generate DAOs", "url":"tdbmadmin/", "default":false}
48
 */
49
class TDBMService
50
{
51
    const MODE_CURSOR = 1;
52
    const MODE_ARRAY = 2;
53
54
    /**
55
     * The database connection.
56
     *
57
     * @var Connection
58
     */
59
    private $connection;
60
61
    /**
62
     * @var SchemaAnalyzer
63
     */
64
    private $schemaAnalyzer;
65
66
    /**
67
     * @var MagicQuery
68
     */
69
    private $magicQuery;
70
71
    /**
72
     * @var TDBMSchemaAnalyzer
73
     */
74
    private $tdbmSchemaAnalyzer;
75
76
    /**
77
     * @var string
78
     */
79
    private $cachePrefix;
80
81
    /**
82
     * Cache of table of primary keys.
83
     * Primary keys are stored by tables, as an array of column.
84
     * For instance $primary_key['my_table'][0] will return the first column of the primary key of table 'my_table'.
85
     *
86
     * @var string[]
87
     */
88
    private $primaryKeysColumns;
89
90
    /**
91
     * Service storing objects in memory.
92
     * Access is done by table name and then by primary key.
93
     * If the primary key is split on several columns, access is done by an array of columns, serialized.
94
     *
95
     * @var StandardObjectStorage|WeakrefObjectStorage
96
     */
97
    private $objectStorage;
98
99
    /**
100
     * The fetch mode of the result sets returned by `getObjects`.
101
     * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY or TDBMObjectArray::MODE_COMPATIBLE_ARRAY.
102
     *
103
     * In 'MODE_ARRAY' mode (default), the result is an array. Use this mode by default (unless the list returned is very big).
104
     * In 'MODE_CURSOR' mode, the result is a Generator which is an iterable collection that can be scanned only once (only one "foreach") on it,
105
     * and it cannot be accessed via key. Use this mode for large datasets processed by batch.
106
     * In 'MODE_COMPATIBLE_ARRAY' mode, the result is an old TDBMObjectArray (used up to TDBM 3.2).
107
     * You can access the array by key, or using foreach, several times.
108
     *
109
     * @var int
110
     */
111
    private $mode = self::MODE_ARRAY;
112
113
    /**
114
     * Table of new objects not yet inserted in database or objects modified that must be saved.
115
     *
116
     * @var \SplObjectStorage of DbRow objects
117
     */
118
    private $toSaveObjects;
119
120
    /**
121
     * A cache service to be used.
122
     *
123
     * @var Cache|null
124
     */
125
    private $cache;
126
127
    /**
128
     * Map associating a table name to a fully qualified Bean class name.
129
     *
130
     * @var array
131
     */
132
    private $tableToBeanMap = [];
133
134
    /**
135
     * @var \ReflectionClass[]
136
     */
137
    private $reflectionClassCache = array();
138
139
    /**
140
     * @var LoggerInterface
141
     */
142
    private $rootLogger;
143
144
    /**
145
     * @var LevelFilter|NullLogger
146
     */
147
    private $logger;
148
149
    /**
150
     * @var OrderByAnalyzer
151
     */
152
    private $orderByAnalyzer;
153
154
    /**
155
     * @var string
156
     */
157
    private $beanNamespace;
158
159
    /**
160
     * @var NamingStrategyInterface
161
     */
162
    private $namingStrategy;
163
    /**
164
     * @var ConfigurationInterface
165
     */
166
    private $configuration;
167
168
    /**
169
     * @param ConfigurationInterface $configuration The configuration object
170
     */
171
    public function __construct(ConfigurationInterface $configuration)
172
    {
173
        if (extension_loaded('weakref')) {
174
            $this->objectStorage = new WeakrefObjectStorage();
175
        } else {
176
            $this->objectStorage = new StandardObjectStorage();
177
        }
178
        $this->connection = $configuration->getConnection();
179
        $this->cache = $configuration->getCache();
180
        $this->schemaAnalyzer = $configuration->getSchemaAnalyzer();
181
182
        $this->magicQuery = new MagicQuery($this->connection, $this->cache, $this->schemaAnalyzer);
183
184
        $this->tdbmSchemaAnalyzer = new TDBMSchemaAnalyzer($this->connection, $this->cache, $this->schemaAnalyzer);
185
        $this->cachePrefix = $this->tdbmSchemaAnalyzer->getCachePrefix();
186
187
        $this->toSaveObjects = new \SplObjectStorage();
188
        $logger = $configuration->getLogger();
189
        if ($logger === null) {
190
            $this->logger = new NullLogger();
191
            $this->rootLogger = new NullLogger();
192
        } else {
193
            $this->rootLogger = $logger;
194
            $this->setLogLevel(LogLevel::WARNING);
195
        }
196
        $this->orderByAnalyzer = new OrderByAnalyzer($this->cache, $this->cachePrefix);
197
        $this->beanNamespace = $configuration->getBeanNamespace();
198
        $this->namingStrategy = $configuration->getNamingStrategy();
199
        $this->configuration = $configuration;
200
    }
201
202
    /**
203
     * Returns the object used to connect to the database.
204
     *
205
     * @return Connection
206
     */
207
    public function getConnection(): Connection
208
    {
209
        return $this->connection;
210
    }
211
212
    /**
213
     * Sets the default fetch mode of the result sets returned by `findObjects`.
214
     * Can be one of: TDBMObjectArray::MODE_CURSOR or TDBMObjectArray::MODE_ARRAY.
215
     *
216
     * In 'MODE_ARRAY' mode (default), the result is a ResultIterator object that behaves like an array. Use this mode by default (unless the list returned is very big).
217
     * In 'MODE_CURSOR' mode, the result is a ResultIterator object. If you scan it many times (by calling several time a foreach loop), the query will be run
218
     * several times. In cursor mode, you cannot access the result set by key. Use this mode for large datasets processed by batch.
219
     *
220
     * @param int $mode
221
     *
222
     * @return $this
223
     *
224
     * @throws TDBMException
225
     */
226
    public function setFetchMode($mode)
227
    {
228
        if ($mode !== self::MODE_CURSOR && $mode !== self::MODE_ARRAY) {
229
            throw new TDBMException("Unknown fetch mode: '".$this->mode."'");
230
        }
231
        $this->mode = $mode;
232
233
        return $this;
234
    }
235
236
    /**
237
     * Removes the given object from database.
238
     * This cannot be called on an object that is not attached to this TDBMService
239
     * (will throw a TDBMInvalidOperationException).
240
     *
241
     * @param AbstractTDBMObject $object the object to delete
242
     *
243
     * @throws TDBMException
244
     * @throws TDBMInvalidOperationException
245
     */
246
    public function delete(AbstractTDBMObject $object)
247
    {
248
        switch ($object->_getStatus()) {
249
            case TDBMObjectStateEnum::STATE_DELETED:
250
                // Nothing to do, object already deleted.
251
                return;
252
            case TDBMObjectStateEnum::STATE_DETACHED:
253
                throw new TDBMInvalidOperationException('Cannot delete a detached object');
254
            case TDBMObjectStateEnum::STATE_NEW:
255
                $this->deleteManyToManyRelationships($object);
256
                foreach ($object->_getDbRows() as $dbRow) {
257
                    $this->removeFromToSaveObjectList($dbRow);
258
                }
259
                break;
260
            case TDBMObjectStateEnum::STATE_DIRTY:
261
                foreach ($object->_getDbRows() as $dbRow) {
262
                    $this->removeFromToSaveObjectList($dbRow);
263
                }
264
                // And continue deleting...
0 ignored issues
show
Unused Code Comprehensibility introduced by
43% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
265
            case TDBMObjectStateEnum::STATE_NOT_LOADED:
266
            case TDBMObjectStateEnum::STATE_LOADED:
267
                $this->deleteManyToManyRelationships($object);
268
                // Let's delete db rows, in reverse order.
269
                foreach (array_reverse($object->_getDbRows()) as $dbRow) {
270
                    $tableName = $dbRow->_getDbTableName();
271
                    $primaryKeys = $dbRow->_getPrimaryKeys();
272
                    $this->connection->delete($tableName, $primaryKeys);
273
                    $this->objectStorage->remove($dbRow->_getDbTableName(), $this->getObjectHash($primaryKeys));
274
                }
275
                break;
276
            // @codeCoverageIgnoreStart
277
            default:
278
                throw new TDBMInvalidOperationException('Unexpected status for bean');
279
            // @codeCoverageIgnoreEnd
280
        }
281
282
        $object->_setStatus(TDBMObjectStateEnum::STATE_DELETED);
283
    }
284
285
    /**
286
     * Removes all many to many relationships for this object.
287
     *
288
     * @param AbstractTDBMObject $object
289
     */
290
    private function deleteManyToManyRelationships(AbstractTDBMObject $object)
291
    {
292
        foreach ($object->_getDbRows() as $tableName => $dbRow) {
293
            $pivotTables = $this->tdbmSchemaAnalyzer->getPivotTableLinkedToTable($tableName);
294
            foreach ($pivotTables as $pivotTable) {
295
                $remoteBeans = $object->_getRelationships($pivotTable);
296
                foreach ($remoteBeans as $remoteBean) {
297
                    $object->_removeRelationship($pivotTable, $remoteBean);
298
                }
299
            }
300
        }
301
        $this->persistManyToManyRelationships($object);
302
    }
303
304
    /**
305
     * This function removes the given object from the database. It will also remove all objects relied to the one given
306
     * by parameter before all.
307
     *
308
     * Notice: if the object has a multiple primary key, the function will not work.
309
     *
310
     * @param AbstractTDBMObject $objToDelete
311
     */
312
    public function deleteCascade(AbstractTDBMObject $objToDelete)
313
    {
314
        $this->deleteAllConstraintWithThisObject($objToDelete);
315
        $this->delete($objToDelete);
316
    }
317
318
    /**
319
     * This function is used only in TDBMService (private function)
320
     * It will call deleteCascade function foreach object relied with a foreign key to the object given by parameter.
321
     *
322
     * @param AbstractTDBMObject $obj
323
     */
324
    private function deleteAllConstraintWithThisObject(AbstractTDBMObject $obj)
325
    {
326
        $dbRows = $obj->_getDbRows();
327
        foreach ($dbRows as $dbRow) {
328
            $tableName = $dbRow->_getDbTableName();
329
            $pks = array_values($dbRow->_getPrimaryKeys());
330
            if (!empty($pks)) {
331
                $incomingFks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($tableName);
332
333
                foreach ($incomingFks as $incomingFk) {
334
                    $filter = array_combine($incomingFk->getLocalColumns(), $pks);
335
336
                    $results = $this->findObjects($incomingFk->getLocalTableName(), $filter);
337
338
                    foreach ($results as $bean) {
339
                        $this->deleteCascade($bean);
340
                    }
341
                }
342
            }
343
        }
344
    }
345
346
    /**
347
     * This function performs a save() of all the objects that have been modified.
348
     */
349
    public function completeSave()
350
    {
351
        foreach ($this->toSaveObjects as $dbRow) {
352
            $this->save($dbRow->getTDBMObject());
353
        }
354
    }
355
356
    /**
357
     * Takes in input a filter_bag (which can be about anything from a string to an array of TDBMObjects... see above from documentation),
358
     * and gives back a proper Filter object.
359
     *
360
     * @param mixed $filter_bag
361
     * @param int   $counter
362
     *
363
     * @return array First item: filter string, second item: parameters
364
     *
365
     * @throws TDBMException
366
     */
367
    public function buildFilterFromFilterBag($filter_bag, $counter = 1)
368
    {
369
        if ($filter_bag === null) {
370
            return ['', []];
371
        } elseif (is_string($filter_bag)) {
372
            return [$filter_bag, []];
373
        } elseif (is_array($filter_bag)) {
374
            $sqlParts = [];
375
            $parameters = [];
376
            foreach ($filter_bag as $column => $value) {
377
                if (is_int($column)) {
378
                    list($subSqlPart, $subParameters) = $this->buildFilterFromFilterBag($value, $counter);
379
                    $sqlParts[] = $subSqlPart;
380
                    $parameters += $subParameters;
381
                } else {
382
                    $paramName = 'tdbmparam'.$counter;
383
                    if (is_array($value)) {
384
                        $sqlParts[] = $this->connection->quoteIdentifier($column).' IN :'.$paramName;
385
                    } else {
386
                        $sqlParts[] = $this->connection->quoteIdentifier($column).' = :'.$paramName;
387
                    }
388
                    $parameters[$paramName] = $value;
389
                    ++$counter;
390
                }
391
            }
392
393
            return [implode(' AND ', $sqlParts), $parameters];
394
        } elseif ($filter_bag instanceof AbstractTDBMObject) {
395
            $sqlParts = [];
396
            $parameters = [];
397
            $dbRows = $filter_bag->_getDbRows();
398
            $dbRow = reset($dbRows);
399
            $primaryKeys = $dbRow->_getPrimaryKeys();
400
401
            foreach ($primaryKeys as $column => $value) {
402
                $paramName = 'tdbmparam'.$counter;
403
                $sqlParts[] = $this->connection->quoteIdentifier($dbRow->_getDbTableName()).'.'.$this->connection->quoteIdentifier($column).' = :'.$paramName;
404
                $parameters[$paramName] = $value;
405
                ++$counter;
406
            }
407
408
            return [implode(' AND ', $sqlParts), $parameters];
409
        } elseif ($filter_bag instanceof \Iterator) {
410
            return $this->buildFilterFromFilterBag(iterator_to_array($filter_bag), $counter);
411
        } else {
412
            throw new TDBMException('Error in filter. An object has been passed that is neither a SQL string, nor an array, nor a bean, nor null.');
413
        }
414
    }
415
416
    /**
417
     * @param string $table
418
     *
419
     * @return string[]
420
     */
421
    public function getPrimaryKeyColumns($table)
422
    {
423
        if (!isset($this->primaryKeysColumns[$table])) {
424
            $this->primaryKeysColumns[$table] = $this->tdbmSchemaAnalyzer->getSchema()->getTable($table)->getPrimaryKeyColumns();
425
426
            // TODO TDBM4: See if we need to improve error reporting if table name does not exist.
427
428
            /*$arr = array();
429
            foreach ($this->connection->getPrimaryKey($table) as $col) {
430
                $arr[] = $col->name;
431
            }
432
            // The primaryKeysColumns contains only the column's name, not the DB_Column object.
433
            $this->primaryKeysColumns[$table] = $arr;
434
            if (empty($this->primaryKeysColumns[$table]))
435
            {
436
                // Unable to find primary key.... this is an error
437
                // Let's try to be precise in error reporting. Let's try to find the table.
438
                $tables = $this->connection->checkTableExist($table);
439
                if ($tables === true)
440
                throw new TDBMException("Could not find table primary key for table '$table'. Please define a primary key for this table.");
441
                elseif ($tables !== null) {
442
                    if (count($tables)==1)
443
                    $str = "Could not find table '$table'. Maybe you meant this table: '".$tables[0]."'";
444
                    else
445
                    $str = "Could not find table '$table'. Maybe you meant one of those tables: '".implode("', '",$tables)."'";
446
                    throw new TDBMException($str);
447
                }
448
            }*/
449
        }
450
451
        return $this->primaryKeysColumns[$table];
452
    }
453
454
    /**
455
     * This is an internal function, you should not use it in your application.
456
     * This is used internally by TDBM to add an object to the object cache.
457
     *
458
     * @param DbRow $dbRow
459
     */
460
    public function _addToCache(DbRow $dbRow)
461
    {
462
        $primaryKey = $this->getPrimaryKeysForObjectFromDbRow($dbRow);
463
        $hash = $this->getObjectHash($primaryKey);
464
        $this->objectStorage->set($dbRow->_getDbTableName(), $hash, $dbRow);
465
    }
466
467
    /**
468
     * This is an internal function, you should not use it in your application.
469
     * This is used internally by TDBM to remove the object from the list of objects that have been
470
     * created/updated but not saved yet.
471
     *
472
     * @param DbRow $myObject
473
     */
474
    private function removeFromToSaveObjectList(DbRow $myObject)
475
    {
476
        unset($this->toSaveObjects[$myObject]);
477
    }
478
479
    /**
480
     * This is an internal function, you should not use it in your application.
481
     * This is used internally by TDBM to add an object to the list of objects that have been
482
     * created/updated but not saved yet.
483
     *
484
     * @param DbRow $myObject
485
     */
486
    public function _addToToSaveObjectList(DbRow $myObject)
487
    {
488
        $this->toSaveObjects[$myObject] = true;
489
    }
490
491
    /**
492
     * Generates all the daos and beans.
493
     *
494
     * @return \string[] the list of tables (key) and bean name (value)
495
     */
496
    public function generateAllDaosAndBeans()
497
    {
498
        // Purge cache before generating anything.
499
        $this->cache->deleteAll();
0 ignored issues
show
Bug introduced by
The method deleteAll() does not exist on Doctrine\Common\Cache\Cache. Did you maybe mean delete()?

This check marks calls to methods that do not seem to exist on an object.

This is most likely the result of a method being renamed without all references to it being renamed likewise.

Loading history...
500
501
        $tdbmDaoGenerator = new TDBMDaoGenerator($this->configuration, $this->tdbmSchemaAnalyzer);
502
        $tdbmDaoGenerator->generateAllDaosAndBeans();
503
    }
504
505
    /**
506
     * Returns the fully qualified class name of the bean associated with table $tableName.
507
     *
508
     *
509
     * @param string $tableName
510
     *
511
     * @return string
512
     */
513
    public function getBeanClassName(string $tableName) : string
514
    {
515
        if (isset($this->tableToBeanMap[$tableName])) {
516
            return $this->tableToBeanMap[$tableName];
517
        } else {
518
            $className = $this->beanNamespace.'\\'.$this->namingStrategy->getBeanClassName($tableName);
519
520
            if (!class_exists($className)) {
521
                throw new TDBMInvalidArgumentException(sprintf('Could not find class "%s". Does table "%s" exist? If yes, consider regenerating the DAOs and beans.', $className, $tableName));
522
            }
523
524
            $this->tableToBeanMap[$tableName] = $className;
525
            return $className;
526
        }
527
    }
528
529
    /**
530
     * Saves $object by INSERTing or UPDAT(E)ing it in the database.
531
     *
532
     * @param AbstractTDBMObject $object
533
     *
534
     * @throws TDBMException
535
     */
536
    public function save(AbstractTDBMObject $object)
537
    {
538
        $status = $object->_getStatus();
539
540
        if ($status === null) {
541
            throw new TDBMException(sprintf('Your bean for class %s has no status. It is likely that you overloaded the __construct method and forgot to call parent::__construct.', get_class($object)));
542
        }
543
544
        // Let's attach this object if it is in detached state.
545
        if ($status === TDBMObjectStateEnum::STATE_DETACHED) {
546
            $object->_attach($this);
547
            $status = $object->_getStatus();
548
        }
549
550
        if ($status === TDBMObjectStateEnum::STATE_NEW) {
551
            $dbRows = $object->_getDbRows();
552
553
            $unindexedPrimaryKeys = array();
554
555
            foreach ($dbRows as $dbRow) {
556
                if ($dbRow->_getStatus() == TDBMObjectStateEnum::STATE_SAVING) {
557
                    throw TDBMCyclicReferenceException::createCyclicReference($dbRow->_getDbTableName(), $object);
558
                }
559
                $dbRow->_setStatus(TDBMObjectStateEnum::STATE_SAVING);
560
                $tableName = $dbRow->_getDbTableName();
561
562
                $schema = $this->tdbmSchemaAnalyzer->getSchema();
563
                $tableDescriptor = $schema->getTable($tableName);
564
565
                $primaryKeyColumns = $this->getPrimaryKeyColumns($tableName);
566
567
                $references = $dbRow->_getReferences();
568
569
                // Let's save all references in NEW or DETACHED state (we need their primary key)
570
                foreach ($references as $fkName => $reference) {
571
                    if ($reference !== null) {
572
                        $refStatus = $reference->_getStatus();
573
                        if ($refStatus === TDBMObjectStateEnum::STATE_NEW || $refStatus === TDBMObjectStateEnum::STATE_DETACHED) {
574
                            try {
575
                                $this->save($reference);
576
                            } catch (TDBMCyclicReferenceException $e) {
577
                                throw TDBMCyclicReferenceException::extendCyclicReference($e, $dbRow->_getDbTableName(), $object, $fkName);
578
                            }
579
                        }
580
                    }
581
                }
582
583
                if (empty($unindexedPrimaryKeys)) {
584
                    $primaryKeys = $this->getPrimaryKeysForObjectFromDbRow($dbRow);
585
                } else {
586
                    // First insert, the children must have the same primary key as the parent.
587
                    $primaryKeys = $this->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $unindexedPrimaryKeys);
588
                    $dbRow->_setPrimaryKeys($primaryKeys);
589
                }
590
591
                $dbRowData = $dbRow->_getDbRow();
592
593
                // Let's see if the columns for primary key have been set before inserting.
594
                // We assume that if one of the value of the PK has been set, the PK is set.
595
                $isPkSet = !empty($primaryKeys);
596
597
                /*if (!$isPkSet) {
598
                    // if there is no autoincrement and no pk set, let's go in error.
599
                    $isAutoIncrement = true;
600
601
                    foreach ($primaryKeyColumns as $pkColumnName) {
602
                        $pkColumn = $tableDescriptor->getColumn($pkColumnName);
603
                        if (!$pkColumn->getAutoincrement()) {
604
                            $isAutoIncrement = false;
605
                        }
606
                    }
607
608
                    if (!$isAutoIncrement) {
609
                        $msg = "Error! You did not set the primary key(s) for the new object of type '$tableName'. The primary key is not set to 'autoincrement' so you must either set the primary key in the object or modify the DB model to create an primary key with auto-increment.";
610
                        throw new TDBMException($msg);
611
                    }
612
613
                }*/
614
615
                $types = [];
616
                $escapedDbRowData = [];
617
618 View Code Duplication
                foreach ($dbRowData as $columnName => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
619
                    $columnDescriptor = $tableDescriptor->getColumn($columnName);
620
                    $types[] = $columnDescriptor->getType();
621
                    $escapedDbRowData[$this->connection->quoteIdentifier($columnName)] = $value;
622
                }
623
624
                $this->connection->insert($tableName, $escapedDbRowData, $types);
625
626
                if (!$isPkSet && count($primaryKeyColumns) == 1) {
627
                    $id = $this->connection->lastInsertId();
628
                    $pkColumn = $primaryKeyColumns[0];
629
                    // lastInsertId returns a string but the column type is usually a int. Let's convert it back to the correct type.
630
                    $id = $tableDescriptor->getColumn($pkColumn)->getType()->convertToPHPValue($id, $this->getConnection()->getDatabasePlatform());
631
                    $primaryKeys[$pkColumn] = $id;
632
                }
633
634
                // TODO: change this to some private magic accessor in future
635
                $dbRow->_setPrimaryKeys($primaryKeys);
636
                $unindexedPrimaryKeys = array_values($primaryKeys);
637
638
                /*
639
                 * When attached, on "save", we check if the column updated is part of a primary key
640
                 * If this is part of a primary key, we call the _update_id method that updates the id in the list of known objects.
641
                 * This method should first verify that the id is not already used (and is not auto-incremented)
642
                 *
643
                 * In the object, the key is stored in an array of  (column => value), that can be directly used to update the record.
644
                 *
645
                 *
646
                 */
647
648
                /*try {
0 ignored issues
show
Unused Code Comprehensibility introduced by
42% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
649
                    $this->db_connection->exec($sql);
650
                } catch (TDBMException $e) {
651
                    $this->db_onerror = true;
652
653
                    // Strange..... if we do not have the line below, bad inserts are not catched.
654
                    // It seems that destructors are called before the registered shutdown function (PHP >=5.0.5)
655
                    //if ($this->tdbmService->isProgramExiting())
656
                    //	trigger_error("program exiting");
657
                    trigger_error($e->getMessage(), E_USER_ERROR);
658
659
                    if (!$this->tdbmService->isProgramExiting())
660
                        throw $e;
661
                    else
662
                    {
663
                        trigger_error($e->getMessage(), E_USER_ERROR);
664
                    }
665
                }*/
666
667
                // Let's remove this object from the $new_objects static table.
668
                $this->removeFromToSaveObjectList($dbRow);
669
670
                // TODO: change this behaviour to something more sensible performance-wise
671
                // Maybe a setting to trigger this globally?
672
                //$this->status = TDBMObjectStateEnum::STATE_NOT_LOADED;
0 ignored issues
show
Unused Code Comprehensibility introduced by
40% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
673
                //$this->db_modified_state = false;
0 ignored issues
show
Unused Code Comprehensibility introduced by
50% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
674
                //$dbRow = array();
0 ignored issues
show
Unused Code Comprehensibility introduced by
63% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
675
676
                // Let's add this object to the list of objects in cache.
677
                $this->_addToCache($dbRow);
678
            }
679
680
            $object->_setStatus(TDBMObjectStateEnum::STATE_LOADED);
681
        } elseif ($status === TDBMObjectStateEnum::STATE_DIRTY) {
682
            $dbRows = $object->_getDbRows();
683
684
            foreach ($dbRows as $dbRow) {
685
                $references = $dbRow->_getReferences();
686
687
                // Let's save all references in NEW state (we need their primary key)
688
                foreach ($references as $fkName => $reference) {
689
                    if ($reference !== null && $reference->_getStatus() === TDBMObjectStateEnum::STATE_NEW) {
690
                        $this->save($reference);
691
                    }
692
                }
693
694
                // Let's first get the primary keys
695
                $tableName = $dbRow->_getDbTableName();
696
                $dbRowData = $dbRow->_getDbRow();
697
698
                $schema = $this->tdbmSchemaAnalyzer->getSchema();
699
                $tableDescriptor = $schema->getTable($tableName);
700
701
                $primaryKeys = $dbRow->_getPrimaryKeys();
702
703
                $types = [];
704
                $escapedDbRowData = [];
705
                $escapedPrimaryKeys = [];
706
707 View Code Duplication
                foreach ($dbRowData as $columnName => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
708
                    $columnDescriptor = $tableDescriptor->getColumn($columnName);
709
                    $types[] = $columnDescriptor->getType();
710
                    $escapedDbRowData[$this->connection->quoteIdentifier($columnName)] = $value;
711
                }
712 View Code Duplication
                foreach ($primaryKeys as $columnName => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
713
                    $columnDescriptor = $tableDescriptor->getColumn($columnName);
714
                    $types[] = $columnDescriptor->getType();
715
                    $escapedPrimaryKeys[$this->connection->quoteIdentifier($columnName)] = $value;
716
                }
717
718
                $this->connection->update($tableName, $escapedDbRowData, $escapedPrimaryKeys, $types);
719
720
                // Let's check if the primary key has been updated...
721
                $needsUpdatePk = false;
722
                foreach ($primaryKeys as $column => $value) {
723
                    if (!isset($dbRowData[$column]) || $dbRowData[$column] != $value) {
724
                        $needsUpdatePk = true;
725
                        break;
726
                    }
727
                }
728
                if ($needsUpdatePk) {
729
                    $this->objectStorage->remove($tableName, $this->getObjectHash($primaryKeys));
730
                    $newPrimaryKeys = $this->getPrimaryKeysForObjectFromDbRow($dbRow);
731
                    $dbRow->_setPrimaryKeys($newPrimaryKeys);
732
                    $this->objectStorage->set($tableName, $this->getObjectHash($primaryKeys), $dbRow);
733
                }
734
735
                // Let's remove this object from the list of objects to save.
736
                $this->removeFromToSaveObjectList($dbRow);
737
            }
738
739
            $object->_setStatus(TDBMObjectStateEnum::STATE_LOADED);
740
        } elseif ($status === TDBMObjectStateEnum::STATE_DELETED) {
741
            throw new TDBMInvalidOperationException('This object has been deleted. It cannot be saved.');
742
        }
743
744
        // Finally, let's save all the many to many relationships to this bean.
745
        $this->persistManyToManyRelationships($object);
746
    }
747
748
    private function persistManyToManyRelationships(AbstractTDBMObject $object)
749
    {
750
        foreach ($object->_getCachedRelationships() as $pivotTableName => $storage) {
751
            $tableDescriptor = $this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName);
752
            list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $object);
753
754
            $toRemoveFromStorage = [];
755
756
            foreach ($storage as $remoteBean) {
757
                /* @var $remoteBean AbstractTDBMObject */
758
                $statusArr = $storage[$remoteBean];
759
                $status = $statusArr['status'];
760
                $reverse = $statusArr['reverse'];
761
                if ($reverse) {
762
                    continue;
763
                }
764
765
                if ($status === 'new') {
766
                    $remoteBeanStatus = $remoteBean->_getStatus();
767
                    if ($remoteBeanStatus === TDBMObjectStateEnum::STATE_NEW || $remoteBeanStatus === TDBMObjectStateEnum::STATE_DETACHED) {
768
                        // Let's save remote bean if needed.
769
                        $this->save($remoteBean);
770
                    }
771
772
                    $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk);
773
774
                    $types = [];
775
                    $escapedFilters = [];
776
777 View Code Duplication
                    foreach ($filters as $columnName => $value) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
778
                        $columnDescriptor = $tableDescriptor->getColumn($columnName);
779
                        $types[] = $columnDescriptor->getType();
780
                        $escapedFilters[$this->connection->quoteIdentifier($columnName)] = $value;
781
                    }
782
783
                    $this->connection->insert($pivotTableName, $escapedFilters, $types);
784
785
                    // Finally, let's mark relationships as saved.
786
                    $statusArr['status'] = 'loaded';
787
                    $storage[$remoteBean] = $statusArr;
788
                    $remoteStorage = $remoteBean->_getCachedRelationships()[$pivotTableName];
789
                    $remoteStatusArr = $remoteStorage[$object];
790
                    $remoteStatusArr['status'] = 'loaded';
791
                    $remoteStorage[$object] = $remoteStatusArr;
792
                } elseif ($status === 'delete') {
793
                    $filters = $this->getPivotFilters($object, $remoteBean, $localFk, $remoteFk);
794
795
                    $types = [];
796
797
                    foreach ($filters as $columnName => $value) {
798
                        $columnDescriptor = $tableDescriptor->getColumn($columnName);
799
                        $types[] = $columnDescriptor->getType();
800
                    }
801
802
                    $this->connection->delete($pivotTableName, $filters, $types);
803
804
                    // Finally, let's remove relationships completely from bean.
805
                    $toRemoveFromStorage[] = $remoteBean;
806
807
                    $remoteBean->_getCachedRelationships()[$pivotTableName]->detach($object);
808
                }
809
            }
810
811
            // Note: due to https://bugs.php.net/bug.php?id=65629, we cannot delete an element inside a foreach loop on a SplStorageObject.
812
            // Therefore, we cache elements in the $toRemoveFromStorage to remove them at a later stage.
813
            foreach ($toRemoveFromStorage as $remoteBean) {
814
                $storage->detach($remoteBean);
815
            }
816
        }
817
    }
818
819
    private function getPivotFilters(AbstractTDBMObject $localBean, AbstractTDBMObject $remoteBean, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk)
820
    {
821
        $localBeanPk = $this->getPrimaryKeyValues($localBean);
822
        $remoteBeanPk = $this->getPrimaryKeyValues($remoteBean);
823
        $localColumns = $localFk->getLocalColumns();
824
        $remoteColumns = $remoteFk->getLocalColumns();
825
826
        $localFilters = array_combine($localColumns, $localBeanPk);
827
        $remoteFilters = array_combine($remoteColumns, $remoteBeanPk);
828
829
        return array_merge($localFilters, $remoteFilters);
830
    }
831
832
    /**
833
     * Returns the "values" of the primary key.
834
     * This returns the primary key from the $primaryKey attribute, not the one stored in the columns.
835
     *
836
     * @param AbstractTDBMObject $bean
837
     *
838
     * @return array numerically indexed array of values
839
     */
840
    private function getPrimaryKeyValues(AbstractTDBMObject $bean)
841
    {
842
        $dbRows = $bean->_getDbRows();
843
        $dbRow = reset($dbRows);
844
845
        return array_values($dbRow->_getPrimaryKeys());
846
    }
847
848
    /**
849
     * Returns a unique hash used to store the object based on its primary key.
850
     * If the array contains only one value, then the value is returned.
851
     * Otherwise, a hash representing the array is returned.
852
     *
853
     * @param array $primaryKeys An array of columns => values forming the primary key
854
     *
855
     * @return string
856
     */
857
    public function getObjectHash(array $primaryKeys)
858
    {
859
        if (count($primaryKeys) === 1) {
860
            return reset($primaryKeys);
861
        } else {
862
            ksort($primaryKeys);
863
864
            return md5(json_encode($primaryKeys));
865
        }
866
    }
867
868
    /**
869
     * Returns an array of primary keys from the object.
870
     * The primary keys are extracted from the object columns and not from the primary keys stored in the
871
     * $primaryKeys variable of the object.
872
     *
873
     * @param DbRow $dbRow
874
     *
875
     * @return array Returns an array of column => value
876
     */
877
    public function getPrimaryKeysForObjectFromDbRow(DbRow $dbRow)
878
    {
879
        $table = $dbRow->_getDbTableName();
880
        $dbRowData = $dbRow->_getDbRow();
881
882
        return $this->_getPrimaryKeysFromObjectData($table, $dbRowData);
883
    }
884
885
    /**
886
     * Returns an array of primary keys for the given row.
887
     * The primary keys are extracted from the object columns.
888
     *
889
     * @param $table
890
     * @param array $columns
891
     *
892
     * @return array
893
     */
894
    public function _getPrimaryKeysFromObjectData($table, array $columns)
895
    {
896
        $primaryKeyColumns = $this->getPrimaryKeyColumns($table);
897
        $values = array();
898
        foreach ($primaryKeyColumns as $column) {
0 ignored issues
show
Bug introduced by
The expression $primaryKeyColumns of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
899
            if (isset($columns[$column])) {
900
                $values[$column] = $columns[$column];
901
            }
902
        }
903
904
        return $values;
905
    }
906
907
    /**
908
     * Attaches $object to this TDBMService.
909
     * The $object must be in DETACHED state and will pass in NEW state.
910
     *
911
     * @param AbstractTDBMObject $object
912
     *
913
     * @throws TDBMInvalidOperationException
914
     */
915
    public function attach(AbstractTDBMObject $object)
916
    {
917
        $object->_attach($this);
918
    }
919
920
    /**
921
     * Returns an associative array (column => value) for the primary keys from the table name and an
922
     * indexed array of primary key values.
923
     *
924
     * @param string $tableName
925
     * @param array  $indexedPrimaryKeys
926
     */
927
    public function _getPrimaryKeysFromIndexedPrimaryKeys($tableName, array $indexedPrimaryKeys)
928
    {
929
        $primaryKeyColumns = $this->tdbmSchemaAnalyzer->getSchema()->getTable($tableName)->getPrimaryKeyColumns();
930
931
        if (count($primaryKeyColumns) !== count($indexedPrimaryKeys)) {
932
            throw new TDBMException(sprintf('Wrong number of columns passed for primary key. Expected %s columns for table "%s",
933
			got %s instead.', count($primaryKeyColumns), $tableName, count($indexedPrimaryKeys)));
934
        }
935
936
        return array_combine($primaryKeyColumns, $indexedPrimaryKeys);
937
    }
938
939
    /**
940
     * Return the list of tables (from child to parent) joining the tables passed in parameter.
941
     * Tables must be in a single line of inheritance. The method will find missing tables.
942
     *
943
     * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent,
944
     * we must be able to find all other tables.
945
     *
946
     * @param string[] $tables
947
     *
948
     * @return string[]
949
     */
950
    public function _getLinkBetweenInheritedTables(array $tables)
951
    {
952
        sort($tables);
953
954
        return $this->fromCache($this->cachePrefix.'_linkbetweeninheritedtables_'.implode('__split__', $tables),
955
            function () use ($tables) {
956
                return $this->_getLinkBetweenInheritedTablesWithoutCache($tables);
957
            });
958
    }
959
960
    /**
961
     * Return the list of tables (from child to parent) joining the tables passed in parameter.
962
     * Tables must be in a single line of inheritance. The method will find missing tables.
963
     *
964
     * Algorithm: one of those tables is the ultimate child. From this child, by recursively getting the parent,
965
     * we must be able to find all other tables.
966
     *
967
     * @param string[] $tables
968
     *
969
     * @return string[]
970
     */
971
    private function _getLinkBetweenInheritedTablesWithoutCache(array $tables)
972
    {
973
        $schemaAnalyzer = $this->schemaAnalyzer;
974
975
        foreach ($tables as $currentTable) {
976
            $allParents = [$currentTable];
977
            while ($currentFk = $schemaAnalyzer->getParentRelationship($currentTable)) {
978
                $currentTable = $currentFk->getForeignTableName();
979
                $allParents[] = $currentTable;
980
            }
981
982
            // Now, does the $allParents contain all the tables we want?
983
            $notFoundTables = array_diff($tables, $allParents);
984
            if (empty($notFoundTables)) {
985
                // We have a winner!
986
                return $allParents;
987
            }
988
        }
989
990
        throw TDBMInheritanceException::create($tables);
991
    }
992
993
    /**
994
     * Returns the list of tables related to this table (via a parent or child inheritance relationship).
995
     *
996
     * @param string $table
997
     *
998
     * @return string[]
999
     */
1000
    public function _getRelatedTablesByInheritance($table)
1001
    {
1002
        return $this->fromCache($this->cachePrefix.'_relatedtables_'.$table, function () use ($table) {
1003
            return $this->_getRelatedTablesByInheritanceWithoutCache($table);
1004
        });
1005
    }
1006
1007
    /**
1008
     * Returns the list of tables related to this table (via a parent or child inheritance relationship).
1009
     *
1010
     * @param string $table
1011
     *
1012
     * @return string[]
1013
     */
1014
    private function _getRelatedTablesByInheritanceWithoutCache($table)
1015
    {
1016
        $schemaAnalyzer = $this->schemaAnalyzer;
1017
1018
        // Let's scan the parent tables
1019
        $currentTable = $table;
1020
1021
        $parentTables = [];
1022
1023
        // Get parent relationship
1024
        while ($currentFk = $schemaAnalyzer->getParentRelationship($currentTable)) {
1025
            $currentTable = $currentFk->getForeignTableName();
1026
            $parentTables[] = $currentTable;
1027
        }
1028
1029
        // Let's recurse in children
1030
        $childrenTables = $this->exploreChildrenTablesRelationships($schemaAnalyzer, $table);
1031
1032
        return array_merge(array_reverse($parentTables), $childrenTables);
1033
    }
1034
1035
    /**
1036
     * Explore all the children and descendant of $table and returns ForeignKeyConstraints on those.
1037
     *
1038
     * @param string $table
1039
     *
1040
     * @return string[]
1041
     */
1042
    private function exploreChildrenTablesRelationships(SchemaAnalyzer $schemaAnalyzer, $table)
1043
    {
1044
        $tables = [$table];
1045
        $keys = $schemaAnalyzer->getChildrenRelationships($table);
1046
1047
        foreach ($keys as $key) {
1048
            $tables = array_merge($tables, $this->exploreChildrenTablesRelationships($schemaAnalyzer, $key->getLocalTableName()));
1049
        }
1050
1051
        return $tables;
1052
    }
1053
1054
    /**
1055
     * Casts a foreign key into SQL, assuming table name is used with no alias.
1056
     * The returned value does contain only one table. For instance:.
1057
     *
1058
     * " LEFT JOIN table2 ON table1.id = table2.table1_id"
1059
     *
1060
     * @param ForeignKeyConstraint $fk
1061
     * @param bool                 $leftTableIsLocal
1062
     *
1063
     * @return string
1064
     */
1065
    /*private function foreignKeyToSql(ForeignKeyConstraint $fk, $leftTableIsLocal) {
0 ignored issues
show
Unused Code Comprehensibility introduced by
59% of this comment could be valid code. Did you maybe forget this after debugging?

Sometimes obsolete code just ends up commented out instead of removed. In this case it is better to remove the code once you have checked you do not need it.

The code might also have been commented out for debugging purposes. In this case it is vital that someone uncomments it again or your project may behave in very unexpected ways in production.

This check looks for comments that seem to be mostly valid code and reports them.

Loading history...
1066
        $onClauses = [];
1067
        $foreignTableName = $this->connection->quoteIdentifier($fk->getForeignTableName());
1068
        $foreignColumns = $fk->getForeignColumns();
1069
        $localTableName = $this->connection->quoteIdentifier($fk->getLocalTableName());
1070
        $localColumns = $fk->getLocalColumns();
1071
        $columnCount = count($localTableName);
1072
1073
        for ($i = 0; $i < $columnCount; $i++) {
1074
            $onClauses[] = sprintf("%s.%s = %s.%s",
1075
                $localTableName,
1076
                $this->connection->quoteIdentifier($localColumns[$i]),
1077
                $foreignColumns,
1078
                $this->connection->quoteIdentifier($foreignColumns[$i])
1079
                );
1080
        }
1081
1082
        $onClause = implode(' AND ', $onClauses);
1083
1084
        if ($leftTableIsLocal) {
1085
            return sprintf(" LEFT JOIN %s ON (%s)", $foreignTableName, $onClause);
1086
        } else {
1087
            return sprintf(" LEFT JOIN %s ON (%s)", $localTableName, $onClause);
1088
        }
1089
    }*/
1090
1091
    /**
1092
     * Returns a `ResultIterator` object representing filtered records of "$mainTable" .
1093
     *
1094
     * The findObjects method should be the most used query method in TDBM if you want to query the database for objects.
1095
     * (Note: if you want to query the database for an object by its primary key, use the findObjectByPk method).
1096
     *
1097
     * The findObjects method takes in parameter:
1098
     * 	- mainTable: the kind of bean you want to retrieve. In TDBM, a bean matches a database row, so the
1099
     * 			`$mainTable` parameter should be the name of an existing table in database.
1100
     *  - filter: The filter is a filter bag. It is what you use to filter your request (the WHERE part in SQL).
1101
     *          It can be a string (SQL Where clause), or even a bean or an associative array (key = column to filter, value = value to find)
1102
     *  - parameters: The parameters used in the filter. If you pass a SQL string as a filter, be sure to avoid
1103
     *          concatenating parameters in the string (this leads to SQL injection and also to poor caching performance).
1104
     *          Instead, please consider passing parameters (see documentation for more details).
1105
     *  - additionalTablesFetch: An array of SQL tables names. The beans related to those tables will be fetched along
1106
     *          the main table. This is useful to avoid hitting the database with numerous subqueries.
1107
     *  - mode: The fetch mode of the result. See `setFetchMode()` method for more details.
1108
     *
1109
     * The `findObjects` method will return a `ResultIterator`. A `ResultIterator` is an object that behaves as an array
1110
     * (in ARRAY mode) at least. It can be iterated using a `foreach` loop.
1111
     *
1112
     * Finally, if filter_bag is null, the whole table is returned.
1113
     *
1114
     * @param string                       $mainTable             The name of the table queried
1115
     * @param string|array|null            $filter                The SQL filters to apply to the query (the WHERE part). Columns from tables different from $mainTable must be prefixed by the table name (in the form: table.column)
1116
     * @param array                        $parameters
1117
     * @param string|UncheckedOrderBy|null $orderString           The ORDER BY part of the query. Columns from tables different from $mainTable must be prefixed by the table name (in the form: table.column)
1118
     * @param array                        $additionalTablesFetch
1119
     * @param int                          $mode
1120
     * @param string                       $className             Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1121
     *
1122
     * @return ResultIterator An object representing an array of results
1123
     *
1124
     * @throws TDBMException
1125
     */
1126
    public function findObjects(string $mainTable, $filter = null, array $parameters = array(), $orderString = null, array $additionalTablesFetch = array(), $mode = null, string $className = null)
1127
    {
1128
        // $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection.
1129
        if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) {
1130
            throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable));
1131
        }
1132
1133
        $mode = $mode ?: $this->mode;
1134
1135
        list($filterString, $additionalParameters) = $this->buildFilterFromFilterBag($filter);
1136
1137
        $parameters = array_merge($parameters, $additionalParameters);
1138
1139
        $queryFactory = new FindObjectsQueryFactory($mainTable, $additionalTablesFetch, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer);
1140
1141
        return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1142
    }
1143
1144
    /**
1145
     * @param string                       $mainTable   The name of the table queried
1146
     * @param string                       $from        The from sql statement
1147
     * @param string|array|null            $filter      The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column)
1148
     * @param array                        $parameters
1149
     * @param string|UncheckedOrderBy|null $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column)
1150
     * @param int                          $mode
1151
     * @param string                       $className   Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1152
     *
1153
     * @return ResultIterator An object representing an array of results
1154
     *
1155
     * @throws TDBMException
1156
     */
1157
    public function findObjectsFromSql(string $mainTable, string $from, $filter = null, array $parameters = array(), $orderString = null, $mode = null, string $className = null)
1158
    {
1159
        // $mainTable is not secured in MagicJoin, let's add a bit of security to avoid SQL injection.
1160
        if (!preg_match('/^[a-zA-Z_][a-zA-Z0-9_]*$/', $mainTable)) {
1161
            throw new TDBMException(sprintf("Invalid table name: '%s'", $mainTable));
1162
        }
1163
1164
        $mode = $mode ?: $this->mode;
1165
1166
        list($filterString, $additionalParameters) = $this->buildFilterFromFilterBag($filter);
1167
1168
        $parameters = array_merge($parameters, $additionalParameters);
1169
1170
        $queryFactory = new FindObjectsFromSqlQueryFactory($mainTable, $from, $filterString, $orderString, $this, $this->tdbmSchemaAnalyzer->getSchema(), $this->orderByAnalyzer, $this->schemaAnalyzer, $this->cache, $this->cachePrefix);
0 ignored issues
show
Bug introduced by
It seems like $this->cache can be null; however, __construct() does not accept null, maybe add an additional type check?

Unless you are absolutely sure that the expression can never be null because of other conditions, we strongly recommend to add an additional type check to your code:

/** @return stdClass|null */
function mayReturnNull() { }

function doesNotAcceptNull(stdClass $x) { }

// With potential error.
function withoutCheck() {
    $x = mayReturnNull();
    doesNotAcceptNull($x); // Potential error here.
}

// Safe - Alternative 1
function withCheck1() {
    $x = mayReturnNull();
    if ( ! $x instanceof stdClass) {
        throw new \LogicException('$x must be defined.');
    }
    doesNotAcceptNull($x);
}

// Safe - Alternative 2
function withCheck2() {
    $x = mayReturnNull();
    if ($x instanceof stdClass) {
        doesNotAcceptNull($x);
    }
}
Loading history...
1171
1172
        return new ResultIterator($queryFactory, $parameters, $this->objectStorage, $className, $this, $this->magicQuery, $mode, $this->logger);
1173
    }
1174
1175
    /**
1176
     * @param $table
1177
     * @param array  $primaryKeys
1178
     * @param array  $additionalTablesFetch
1179
     * @param bool   $lazy                  Whether to perform lazy loading on this object or not
1180
     * @param string $className
1181
     *
1182
     * @return AbstractTDBMObject
1183
     *
1184
     * @throws TDBMException
1185
     */
1186
    public function findObjectByPk(string $table, array $primaryKeys, array $additionalTablesFetch = array(), bool $lazy = false, string $className = null)
1187
    {
1188
        $primaryKeys = $this->_getPrimaryKeysFromObjectData($table, $primaryKeys);
1189
        $hash = $this->getObjectHash($primaryKeys);
1190
1191
        if ($this->objectStorage->has($table, $hash)) {
1192
            $dbRow = $this->objectStorage->get($table, $hash);
1193
            $bean = $dbRow->getTDBMObject();
1194
            if ($className !== null && !is_a($bean, $className)) {
1195
                throw new TDBMException("TDBM cannot create a bean of class '".$className."'. The requested object was already loaded and its class is '".get_class($bean)."'");
1196
            }
1197
1198
            return $bean;
1199
        }
1200
1201
        // Are we performing lazy fetching?
1202
        if ($lazy === true) {
1203
            // Can we perform lazy fetching?
1204
            $tables = $this->_getRelatedTablesByInheritance($table);
1205
            // Only allowed if no inheritance.
1206
            if (count($tables) === 1) {
1207
                if ($className === null) {
1208
                    try {
1209
                        $className = $this->getBeanClassName($table);
1210
                    } catch (TDBMInvalidArgumentException $e) {
1211
                        $className = TDBMObject::class;
1212
                    }
1213
                }
1214
1215
                // Let's construct the bean
1216
                if (!isset($this->reflectionClassCache[$className])) {
1217
                    $this->reflectionClassCache[$className] = new \ReflectionClass($className);
1218
                }
1219
                // Let's bypass the constructor when creating the bean!
1220
                $bean = $this->reflectionClassCache[$className]->newInstanceWithoutConstructor();
1221
                /* @var $bean AbstractTDBMObject */
1222
                $bean->_constructLazy($table, $primaryKeys, $this);
1223
1224
                return $bean;
1225
            }
1226
        }
1227
1228
        // Did not find the object in cache? Let's query it!
1229
        return $this->findObjectOrFail($table, $primaryKeys, [], $additionalTablesFetch, $className);
1230
    }
1231
1232
    /**
1233
     * Returns a unique bean (or null) according to the filters passed in parameter.
1234
     *
1235
     * @param string            $mainTable             The name of the table queried
1236
     * @param string|array|null $filter                The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column)
1237
     * @param array             $parameters
1238
     * @param array             $additionalTablesFetch
1239
     * @param string            $className             Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1240
     *
1241
     * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters
1242
     *
1243
     * @throws TDBMException
1244
     */
1245 View Code Duplication
    public function findObject(string $mainTable, $filter = null, array $parameters = array(), array $additionalTablesFetch = array(), string $className = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1246
    {
1247
        $objects = $this->findObjects($mainTable, $filter, $parameters, null, $additionalTablesFetch, self::MODE_ARRAY, $className);
1248
        $page = $objects->take(0, 2);
1249
        $count = $page->count();
1250
        if ($count > 1) {
1251
            throw new DuplicateRowException("Error while querying an object for table '$mainTable': More than 1 row have been returned, but we should have received at most one.");
1252
        } elseif ($count === 0) {
1253
            return;
1254
        }
1255
1256
        return $page[0];
1257
    }
1258
1259
    /**
1260
     * Returns a unique bean (or null) according to the filters passed in parameter.
1261
     *
1262
     * @param string            $mainTable  The name of the table queried
1263
     * @param string            $from       The from sql statement
1264
     * @param string|array|null $filter     The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column)
1265
     * @param array             $parameters
1266
     * @param string            $className  Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1267
     *
1268
     * @return AbstractTDBMObject|null The object we want, or null if no object matches the filters
1269
     *
1270
     * @throws TDBMException
1271
     */
1272 View Code Duplication
    public function findObjectFromSql($mainTable, $from, $filter = null, array $parameters = array(), $className = null)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1273
    {
1274
        $objects = $this->findObjectsFromSql($mainTable, $from, $filter, $parameters, null, self::MODE_ARRAY, $className);
1275
        $page = $objects->take(0, 2);
1276
        $count = $page->count();
1277
        if ($count > 1) {
1278
            throw new DuplicateRowException("Error while querying an object for table '$mainTable': More than 1 row have been returned, but we should have received at most one.");
1279
        } elseif ($count === 0) {
1280
            return;
1281
        }
1282
1283
        return $page[0];
1284
    }
1285
1286
    /**
1287
     * Returns a unique bean according to the filters passed in parameter.
1288
     * Throws a NoBeanFoundException if no bean was found for the filter passed in parameter.
1289
     *
1290
     * @param string            $mainTable             The name of the table queried
1291
     * @param string|array|null $filter                The SQL filters to apply to the query (the WHERE part). All columns must be prefixed by the table name (in the form: table.column)
1292
     * @param array             $parameters
1293
     * @param array             $additionalTablesFetch
1294
     * @param string            $className             Optional: The name of the class to instantiate. This class must extend the TDBMObject class. If none is specified, a TDBMObject instance will be returned
1295
     *
1296
     * @return AbstractTDBMObject The object we want
1297
     *
1298
     * @throws TDBMException
1299
     */
1300
    public function findObjectOrFail(string $mainTable, $filter = null, array $parameters = array(), array $additionalTablesFetch = array(), string $className = null)
1301
    {
1302
        $bean = $this->findObject($mainTable, $filter, $parameters, $additionalTablesFetch, $className);
1303
        if ($bean === null) {
1304
            throw new NoBeanFoundException("No result found for query on table '".$mainTable."'");
1305
        }
1306
1307
        return $bean;
1308
    }
1309
1310
    /**
1311
     * @param array $beanData An array of data: array<table, array<column, value>>
1312
     *
1313
     * @return array an array with first item = class name, second item = table name and third item = list of tables needed
1314
     *
1315
     * @throws TDBMInheritanceException
1316
     */
1317
    public function _getClassNameFromBeanData(array $beanData)
1318
    {
1319
        if (count($beanData) === 1) {
1320
            $tableName = array_keys($beanData)[0];
1321
            $allTables = [$tableName];
1322
        } else {
1323
            $tables = [];
1324
            foreach ($beanData as $table => $row) {
1325
                $primaryKeyColumns = $this->getPrimaryKeyColumns($table);
1326
                $pkSet = false;
1327
                foreach ($primaryKeyColumns as $columnName) {
0 ignored issues
show
Bug introduced by
The expression $primaryKeyColumns of type string|array is not guaranteed to be traversable. How about adding an additional type check?

There are different options of fixing this problem.

  1. If you want to be on the safe side, you can add an additional type-check:

    $collection = json_decode($data, true);
    if ( ! is_array($collection)) {
        throw new \RuntimeException('$collection must be an array.');
    }
    
    foreach ($collection as $item) { /** ... */ }
    
  2. If you are sure that the expression is traversable, you might want to add a doc comment cast to improve IDE auto-completion and static analysis:

    /** @var array $collection */
    $collection = json_decode($data, true);
    
    foreach ($collection as $item) { /** .. */ }
    
  3. Mark the issue as a false-positive: Just hover the remove button, in the top-right corner of this issue for more options.

Loading history...
1328
                    if ($row[$columnName] !== null) {
1329
                        $pkSet = true;
1330
                        break;
1331
                    }
1332
                }
1333
                if ($pkSet) {
1334
                    $tables[] = $table;
1335
                }
1336
            }
1337
1338
            // $tables contains the tables for this bean. Let's view the top most part of the hierarchy
1339
            try {
1340
                $allTables = $this->_getLinkBetweenInheritedTables($tables);
1341
            } catch (TDBMInheritanceException $e) {
1342
                throw TDBMInheritanceException::extendException($e, $this, $beanData);
1343
            }
1344
            $tableName = $allTables[0];
1345
        }
1346
1347
        // Only one table in this bean. Life is sweat, let's look at its type:
1348
        try {
1349
            $className = $this->getBeanClassName($tableName);
1350
        } catch (TDBMInvalidArgumentException $e) {
1351
            $className = 'Mouf\\Database\\TDBM\\TDBMObject';
1352
        }
1353
1354
        return [$className, $tableName, $allTables];
1355
    }
1356
1357
    /**
1358
     * Returns an item from cache or computes it using $closure and puts it in cache.
1359
     *
1360
     * @param string   $key
1361
     * @param callable $closure
1362
     *
1363
     * @return mixed
1364
     */
1365 View Code Duplication
    private function fromCache(string $key, callable $closure)
0 ignored issues
show
Duplication introduced by
This method seems to be duplicated in your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
1366
    {
1367
        $item = $this->cache->fetch($key);
1368
        if ($item === false) {
1369
            $item = $closure();
1370
            $this->cache->save($key, $item);
1371
        }
1372
1373
        return $item;
1374
    }
1375
1376
    /**
1377
     * Returns the foreign key object.
1378
     *
1379
     * @param string $table
1380
     * @param string $fkName
1381
     *
1382
     * @return ForeignKeyConstraint
1383
     */
1384
    public function _getForeignKeyByName(string $table, string $fkName)
1385
    {
1386
        return $this->tdbmSchemaAnalyzer->getSchema()->getTable($table)->getForeignKey($fkName);
1387
    }
1388
1389
    /**
1390
     * @param $pivotTableName
1391
     * @param AbstractTDBMObject $bean
1392
     *
1393
     * @return AbstractTDBMObject[]
1394
     */
1395
    public function _getRelatedBeans(string $pivotTableName, AbstractTDBMObject $bean)
1396
    {
1397
        list($localFk, $remoteFk) = $this->getPivotTableForeignKeys($pivotTableName, $bean);
1398
        /* @var $localFk ForeignKeyConstraint */
1399
        /* @var $remoteFk ForeignKeyConstraint */
1400
        $remoteTable = $remoteFk->getForeignTableName();
1401
1402
        $primaryKeys = $this->getPrimaryKeyValues($bean);
1403
        $columnNames = array_map(function ($name) use ($pivotTableName) {
1404
            return $pivotTableName.'.'.$name;
1405
        }, $localFk->getLocalColumns());
1406
1407
        $filter = array_combine($columnNames, $primaryKeys);
1408
1409
        return $this->findObjects($remoteTable, $filter);
1410
    }
1411
1412
    /**
1413
     * @param $pivotTableName
1414
     * @param AbstractTDBMObject $bean The LOCAL bean
1415
     *
1416
     * @return ForeignKeyConstraint[] First item: the LOCAL bean, second item: the REMOTE bean
1417
     *
1418
     * @throws TDBMException
1419
     */
1420
    private function getPivotTableForeignKeys(string $pivotTableName, AbstractTDBMObject $bean)
1421
    {
1422
        $fks = array_values($this->tdbmSchemaAnalyzer->getSchema()->getTable($pivotTableName)->getForeignKeys());
1423
        $table1 = $fks[0]->getForeignTableName();
1424
        $table2 = $fks[1]->getForeignTableName();
1425
1426
        $beanTables = array_map(function (DbRow $dbRow) {
1427
            return $dbRow->_getDbTableName();
1428
        }, $bean->_getDbRows());
1429
1430
        if (in_array($table1, $beanTables)) {
1431
            return [$fks[0], $fks[1]];
1432
        } elseif (in_array($table2, $beanTables)) {
1433
            return [$fks[1], $fks[0]];
1434
        } else {
1435
            throw new TDBMException("Unexpected bean type in getPivotTableForeignKeys. Awaiting beans from table {$table1} and {$table2} for pivot table {$pivotTableName}");
1436
        }
1437
    }
1438
1439
    /**
1440
     * Returns a list of pivot tables linked to $bean.
1441
     *
1442
     * @param AbstractTDBMObject $bean
1443
     *
1444
     * @return string[]
1445
     */
1446
    public function _getPivotTablesLinkedToBean(AbstractTDBMObject $bean)
1447
    {
1448
        $junctionTables = [];
1449
        $allJunctionTables = $this->schemaAnalyzer->detectJunctionTables(true);
1450
        foreach ($bean->_getDbRows() as $dbRow) {
1451
            foreach ($allJunctionTables as $table) {
1452
                // There are exactly 2 FKs since this is a pivot table.
1453
                $fks = array_values($table->getForeignKeys());
1454
1455
                if ($fks[0]->getForeignTableName() === $dbRow->_getDbTableName() || $fks[1]->getForeignTableName() === $dbRow->_getDbTableName()) {
1456
                    $junctionTables[] = $table->getName();
1457
                }
1458
            }
1459
        }
1460
1461
        return $junctionTables;
1462
    }
1463
1464
    /**
1465
     * Array of types for tables.
1466
     * Key: table name
1467
     * Value: array of types indexed by column.
1468
     *
1469
     * @var array[]
1470
     */
1471
    private $typesForTable = [];
1472
1473
    /**
1474
     * @internal
1475
     *
1476
     * @param string $tableName
1477
     *
1478
     * @return Type[]
1479
     */
1480
    public function _getColumnTypesForTable(string $tableName)
1481
    {
1482
        if (!isset($typesForTable[$tableName])) {
0 ignored issues
show
Bug introduced by
The variable $typesForTable seems only to be defined at a later point. As such the call to isset() seems to always evaluate to false.

This check marks calls to isset(...) or empty(...) that are found before the variable itself is defined. These will always have the same result.

This is likely the result of code being shifted around. Consider removing these calls.

Loading history...
1483
            $columns = $this->tdbmSchemaAnalyzer->getSchema()->getTable($tableName)->getColumns();
1484
            $typesForTable[$tableName] = array_map(function (Column $column) {
0 ignored issues
show
Coding Style Comprehensibility introduced by
$typesForTable was never initialized. Although not strictly required by PHP, it is generally a good practice to add $typesForTable = array(); before regardless.

Adding an explicit array definition is generally preferable to implicit array definition as it guarantees a stable state of the code.

Let’s take a look at an example:

foreach ($collection as $item) {
    $myArray['foo'] = $item->getFoo();

    if ($item->hasBar()) {
        $myArray['bar'] = $item->getBar();
    }

    // do something with $myArray
}

As you can see in this example, the array $myArray is initialized the first time when the foreach loop is entered. You can also see that the value of the bar key is only written conditionally; thus, its value might result from a previous iteration.

This might or might not be intended. To make your intention clear, your code more readible and to avoid accidental bugs, we recommend to add an explicit initialization $myArray = array() either outside or inside the foreach loop.

Loading history...
1485
                return $column->getType();
1486
            }, $columns);
1487
        }
1488
1489
        return $typesForTable[$tableName];
1490
    }
1491
1492
    /**
1493
     * Sets the minimum log level.
1494
     * $level must be one of Psr\Log\LogLevel::xxx.
1495
     *
1496
     * Defaults to LogLevel::WARNING
1497
     *
1498
     * @param string $level
1499
     */
1500
    public function setLogLevel(string $level)
1501
    {
1502
        $this->logger = new LevelFilter($this->rootLogger, $level);
1503
    }
1504
}
1505