@@ -537,10 +537,10 @@ discard block |
||
| 537 | 537 | return $sql_where; |
| 538 | 538 | } |
| 539 | 539 | |
| 540 | - /** |
|
| 541 | - * Override the native php clone function for TDBMObjects |
|
| 542 | - */ |
|
| 543 | - public function __clone() { |
|
| 540 | + /** |
|
| 541 | + * Override the native php clone function for TDBMObjects |
|
| 542 | + */ |
|
| 543 | + public function __clone() { |
|
| 544 | 544 | // Let's clone the many to many relationships |
| 545 | 545 | if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
| 546 | 546 | $pivotTableList = array_keys($this->relationships); |
@@ -566,10 +566,10 @@ discard block |
||
| 566 | 566 | } |
| 567 | 567 | |
| 568 | 568 | // Let's set the status to new (to enter the save function) |
| 569 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
| 569 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
| 570 | 570 | |
| 571 | 571 | |
| 572 | - } |
|
| 572 | + } |
|
| 573 | 573 | |
| 574 | 574 | /** |
| 575 | 575 | * Returns raw database rows. |
@@ -18,7 +18,6 @@ |
||
| 18 | 18 | along with this program; if not, write to the Free Software |
| 19 | 19 | Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
| 20 | 20 | */ |
| 21 | -use Doctrine\DBAL\Driver\Connection; |
|
| 22 | 21 | |
| 23 | 22 | |
| 24 | 23 | /** |
@@ -75,7 +75,7 @@ discard block |
||
| 75 | 75 | * @throws TDBMException |
| 76 | 76 | * @throws TDBMInvalidOperationException |
| 77 | 77 | */ |
| 78 | - public function __construct($tableName=null, array $primaryKeys=array(), TDBMService $tdbmService=null) { |
|
| 78 | + public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null) { |
|
| 79 | 79 | // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
| 80 | 80 | if (!empty($tableName)) { |
| 81 | 81 | $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
@@ -159,7 +159,7 @@ discard block |
||
| 159 | 159 | * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
| 160 | 160 | * @param string $state |
| 161 | 161 | */ |
| 162 | - public function _setStatus($state){ |
|
| 162 | + public function _setStatus($state) { |
|
| 163 | 163 | $this->status = $state; |
| 164 | 164 | |
| 165 | 165 | // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
@@ -170,7 +170,7 @@ discard block |
||
| 170 | 170 | |
| 171 | 171 | public function get($var, $tableName = null) { |
| 172 | 172 | if ($tableName === null) { |
| 173 | - if (count($this->dbRows) > 1) { |
|
| 173 | + if (count($this->dbRows)>1) { |
|
| 174 | 174 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 175 | 175 | } elseif (count($this->dbRows) === 1) { |
| 176 | 176 | $tableName = array_keys($this->dbRows)[0]; |
@@ -196,7 +196,7 @@ discard block |
||
| 196 | 196 | */ |
| 197 | 197 | public function has($var, $tableName = null) { |
| 198 | 198 | if ($tableName === null) { |
| 199 | - if (count($this->dbRows) > 1) { |
|
| 199 | + if (count($this->dbRows)>1) { |
|
| 200 | 200 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 201 | 201 | } elseif (count($this->dbRows) === 1) { |
| 202 | 202 | $tableName = array_keys($this->dbRows)[0]; |
@@ -216,7 +216,7 @@ discard block |
||
| 216 | 216 | |
| 217 | 217 | public function set($var, $value, $tableName = null) { |
| 218 | 218 | if ($tableName === null) { |
| 219 | - if (count($this->dbRows) > 1) { |
|
| 219 | + if (count($this->dbRows)>1) { |
|
| 220 | 220 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 221 | 221 | } elseif (count($this->dbRows) === 1) { |
| 222 | 222 | $tableName = array_keys($this->dbRows)[0]; |
@@ -241,7 +241,7 @@ discard block |
||
| 241 | 241 | */ |
| 242 | 242 | public function setRef($foreignKeyName, AbstractTDBMObject $bean, $tableName = null) { |
| 243 | 243 | if ($tableName === null) { |
| 244 | - if (count($this->dbRows) > 1) { |
|
| 244 | + if (count($this->dbRows)>1) { |
|
| 245 | 245 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 246 | 246 | } elseif (count($this->dbRows) === 1) { |
| 247 | 247 | $tableName = array_keys($this->dbRows)[0]; |
@@ -266,7 +266,7 @@ discard block |
||
| 266 | 266 | */ |
| 267 | 267 | public function getRef($foreignKeyName, $tableName = null) { |
| 268 | 268 | if ($tableName === null) { |
| 269 | - if (count($this->dbRows) > 1) { |
|
| 269 | + if (count($this->dbRows)>1) { |
|
| 270 | 270 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 271 | 271 | } elseif (count($this->dbRows) === 1) { |
| 272 | 272 | $tableName = array_keys($this->dbRows)[0]; |
@@ -382,13 +382,13 @@ discard block |
||
| 382 | 382 | */ |
| 383 | 383 | private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) { |
| 384 | 384 | $storage = $this->getRelationshipStorage($pivotTableName); |
| 385 | - $storage->attach($remoteBean, [ 'status' => $status, 'reverse' => false ]); |
|
| 385 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
| 386 | 386 | if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
| 387 | 387 | $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
| 388 | 388 | } |
| 389 | 389 | |
| 390 | 390 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
| 391 | - $remoteStorage->attach($this, [ 'status' => $status, 'reverse' => true ]); |
|
| 391 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
| 392 | 392 | } |
| 393 | 393 | |
| 394 | 394 | /** |
@@ -458,7 +458,7 @@ discard block |
||
| 458 | 458 | $metadata = $storage[$remoteBean]; |
| 459 | 459 | |
| 460 | 460 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
| 461 | - $remoteStorage->attach($this, [ 'status' => $metadata['status'], 'reverse' => !$metadata['reverse'] ]); |
|
| 461 | + $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
|
| 462 | 462 | } |
| 463 | 463 | } |
| 464 | 464 | |
@@ -485,7 +485,7 @@ discard block |
||
| 485 | 485 | private function registerTable($tableName) { |
| 486 | 486 | $dbRow = new DbRow($this, $tableName); |
| 487 | 487 | |
| 488 | - if (in_array($this->status, [ TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY ])) { |
|
| 488 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
| 489 | 489 | // Let's get the primary key for the new table |
| 490 | 490 | $anotherDbRow = array_values($this->dbRows)[0]; |
| 491 | 491 | /* @var $anotherDbRow DbRow */ |
@@ -16,126 +16,126 @@ |
||
| 16 | 16 | class TDBMSchemaAnalyzer |
| 17 | 17 | { |
| 18 | 18 | |
| 19 | - private $connection; |
|
| 20 | - |
|
| 21 | - /** |
|
| 22 | - * @var Schema |
|
| 23 | - */ |
|
| 24 | - private $schema; |
|
| 25 | - |
|
| 26 | - /** |
|
| 27 | - * @var string |
|
| 28 | - */ |
|
| 29 | - private $cachePrefix; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @var Cache |
|
| 33 | - */ |
|
| 34 | - private $cache; |
|
| 35 | - |
|
| 36 | - /** |
|
| 37 | - * @var SchemaAnalyzer |
|
| 38 | - */ |
|
| 39 | - private $schemaAnalyzer; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * @param Connection $connection The DBAL DB connection to use |
|
| 43 | - * @param Cache $cache A cache service to be used |
|
| 44 | - * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
| 45 | - * Will be automatically created if not passed. |
|
| 46 | - */ |
|
| 47 | - public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) { |
|
| 48 | - $this->connection = $connection; |
|
| 49 | - $this->cache = $cache; |
|
| 50 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
| 55 | - * @return string |
|
| 56 | - */ |
|
| 57 | - public function getCachePrefix() { |
|
| 58 | - if ($this->cachePrefix === null) { |
|
| 59 | - $this->cachePrefix = hash('md4', $this->connection->getHost()."-".$this->connection->getPort()."-".$this->connection->getDatabase()."-".$this->connection->getDriver()->getName()); |
|
| 60 | - } |
|
| 61 | - return $this->cachePrefix; |
|
| 62 | - } |
|
| 63 | - |
|
| 64 | - /** |
|
| 65 | - * Returns the (cached) schema. |
|
| 66 | - * |
|
| 67 | - * @return Schema |
|
| 68 | - */ |
|
| 69 | - public function getSchema() { |
|
| 70 | - if ($this->schema === null) { |
|
| 71 | - $cacheKey = $this->getCachePrefix().'_schema'; |
|
| 72 | - if ($this->cache->contains($cacheKey)) { |
|
| 73 | - $this->schema = $this->cache->fetch($cacheKey); |
|
| 74 | - } else { |
|
| 75 | - $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
| 76 | - $this->cache->save($cacheKey, $this->schema); |
|
| 77 | - } |
|
| 78 | - } |
|
| 79 | - return $this->schema; |
|
| 80 | - } |
|
| 81 | - |
|
| 82 | - /** |
|
| 83 | - * Returns the list of pivot tables linked to table $tableName |
|
| 84 | - * @param string $tableName |
|
| 85 | - * @return array|string[] |
|
| 86 | - */ |
|
| 87 | - public function getPivotTableLinkedToTable($tableName) { |
|
| 88 | - $cacheKey = $this->getCachePrefix().'_pivottables_link'; |
|
| 89 | - if ($this->cache->contains($cacheKey)) { |
|
| 90 | - return $this->cache->fetch($cacheKey); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - $pivotTables = []; |
|
| 94 | - |
|
| 95 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 96 | - foreach ($junctionTables as $table) { |
|
| 97 | - $fks = $table->getForeignKeys(); |
|
| 98 | - foreach ($fks as $fk) { |
|
| 99 | - if ($fk->getForeignTableName() == $tableName) { |
|
| 100 | - $pivotTables[] = $table->getName(); |
|
| 101 | - break; |
|
| 102 | - } |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - $this->cache->save($cacheKey, $pivotTables); |
|
| 107 | - return $pivotTables; |
|
| 108 | - } |
|
| 109 | - |
|
| 110 | - /** |
|
| 111 | - * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
| 112 | - * from junction tables and from inheritance. |
|
| 113 | - * |
|
| 114 | - * @return ForeignKeyConstraint[] |
|
| 115 | - */ |
|
| 116 | - public function getIncomingForeignKeys($tableName) { |
|
| 117 | - |
|
| 118 | - $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 119 | - $junctionTableNames = array_map(function(Table $table) { return $table->getName(); }, $junctionTables); |
|
| 120 | - $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 121 | - |
|
| 122 | - $fks = []; |
|
| 123 | - foreach ($this->getSchema()->getTables() as $table) { |
|
| 124 | - foreach ($table->getForeignKeys() as $fk) { |
|
| 125 | - if ($fk->getForeignTableName() === $tableName) { |
|
| 126 | - if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 127 | - continue; |
|
| 128 | - } |
|
| 129 | - foreach ($childrenRelationships as $childFk) { |
|
| 130 | - if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 131 | - continue 2; |
|
| 132 | - } |
|
| 133 | - } |
|
| 134 | - $fks[] = $fk; |
|
| 135 | - } |
|
| 136 | - } |
|
| 137 | - } |
|
| 138 | - |
|
| 139 | - return $fks; |
|
| 140 | - } |
|
| 19 | + private $connection; |
|
| 20 | + |
|
| 21 | + /** |
|
| 22 | + * @var Schema |
|
| 23 | + */ |
|
| 24 | + private $schema; |
|
| 25 | + |
|
| 26 | + /** |
|
| 27 | + * @var string |
|
| 28 | + */ |
|
| 29 | + private $cachePrefix; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @var Cache |
|
| 33 | + */ |
|
| 34 | + private $cache; |
|
| 35 | + |
|
| 36 | + /** |
|
| 37 | + * @var SchemaAnalyzer |
|
| 38 | + */ |
|
| 39 | + private $schemaAnalyzer; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * @param Connection $connection The DBAL DB connection to use |
|
| 43 | + * @param Cache $cache A cache service to be used |
|
| 44 | + * @param SchemaAnalyzer $schemaAnalyzer The schema analyzer that will be used to find shortest paths... |
|
| 45 | + * Will be automatically created if not passed. |
|
| 46 | + */ |
|
| 47 | + public function __construct(Connection $connection, Cache $cache, SchemaAnalyzer $schemaAnalyzer) { |
|
| 48 | + $this->connection = $connection; |
|
| 49 | + $this->cache = $cache; |
|
| 50 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Returns a unique ID for the current connection. Useful for namespacing cache entries in the current connection. |
|
| 55 | + * @return string |
|
| 56 | + */ |
|
| 57 | + public function getCachePrefix() { |
|
| 58 | + if ($this->cachePrefix === null) { |
|
| 59 | + $this->cachePrefix = hash('md4', $this->connection->getHost()."-".$this->connection->getPort()."-".$this->connection->getDatabase()."-".$this->connection->getDriver()->getName()); |
|
| 60 | + } |
|
| 61 | + return $this->cachePrefix; |
|
| 62 | + } |
|
| 63 | + |
|
| 64 | + /** |
|
| 65 | + * Returns the (cached) schema. |
|
| 66 | + * |
|
| 67 | + * @return Schema |
|
| 68 | + */ |
|
| 69 | + public function getSchema() { |
|
| 70 | + if ($this->schema === null) { |
|
| 71 | + $cacheKey = $this->getCachePrefix().'_schema'; |
|
| 72 | + if ($this->cache->contains($cacheKey)) { |
|
| 73 | + $this->schema = $this->cache->fetch($cacheKey); |
|
| 74 | + } else { |
|
| 75 | + $this->schema = $this->connection->getSchemaManager()->createSchema(); |
|
| 76 | + $this->cache->save($cacheKey, $this->schema); |
|
| 77 | + } |
|
| 78 | + } |
|
| 79 | + return $this->schema; |
|
| 80 | + } |
|
| 81 | + |
|
| 82 | + /** |
|
| 83 | + * Returns the list of pivot tables linked to table $tableName |
|
| 84 | + * @param string $tableName |
|
| 85 | + * @return array|string[] |
|
| 86 | + */ |
|
| 87 | + public function getPivotTableLinkedToTable($tableName) { |
|
| 88 | + $cacheKey = $this->getCachePrefix().'_pivottables_link'; |
|
| 89 | + if ($this->cache->contains($cacheKey)) { |
|
| 90 | + return $this->cache->fetch($cacheKey); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + $pivotTables = []; |
|
| 94 | + |
|
| 95 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 96 | + foreach ($junctionTables as $table) { |
|
| 97 | + $fks = $table->getForeignKeys(); |
|
| 98 | + foreach ($fks as $fk) { |
|
| 99 | + if ($fk->getForeignTableName() == $tableName) { |
|
| 100 | + $pivotTables[] = $table->getName(); |
|
| 101 | + break; |
|
| 102 | + } |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + $this->cache->save($cacheKey, $pivotTables); |
|
| 107 | + return $pivotTables; |
|
| 108 | + } |
|
| 109 | + |
|
| 110 | + /** |
|
| 111 | + * Returns the list of foreign keys pointing to the table represented by this bean, excluding foreign keys |
|
| 112 | + * from junction tables and from inheritance. |
|
| 113 | + * |
|
| 114 | + * @return ForeignKeyConstraint[] |
|
| 115 | + */ |
|
| 116 | + public function getIncomingForeignKeys($tableName) { |
|
| 117 | + |
|
| 118 | + $junctionTables = $this->schemaAnalyzer->detectJunctionTables(); |
|
| 119 | + $junctionTableNames = array_map(function(Table $table) { return $table->getName(); }, $junctionTables); |
|
| 120 | + $childrenRelationships = $this->schemaAnalyzer->getChildrenRelationships($tableName); |
|
| 121 | + |
|
| 122 | + $fks = []; |
|
| 123 | + foreach ($this->getSchema()->getTables() as $table) { |
|
| 124 | + foreach ($table->getForeignKeys() as $fk) { |
|
| 125 | + if ($fk->getForeignTableName() === $tableName) { |
|
| 126 | + if (in_array($fk->getLocalTableName(), $junctionTableNames)) { |
|
| 127 | + continue; |
|
| 128 | + } |
|
| 129 | + foreach ($childrenRelationships as $childFk) { |
|
| 130 | + if ($fk->getLocalTableName() === $childFk->getLocalTableName() && $fk->getLocalColumns() === $childFk->getLocalColumns()) { |
|
| 131 | + continue 2; |
|
| 132 | + } |
|
| 133 | + } |
|
| 134 | + $fks[] = $fk; |
|
| 135 | + } |
|
| 136 | + } |
|
| 137 | + } |
|
| 138 | + |
|
| 139 | + return $fks; |
|
| 140 | + } |
|
| 141 | 141 | } |