@@ -1,6 +1,6 @@ |
||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -declare(strict_types=1); |
|
| 3 | +declare(strict_types = 1); |
|
| 4 | 4 | |
| 5 | 5 | namespace Mouf\Database\TDBM; |
| 6 | 6 | |
@@ -31,615 +31,615 @@ |
||
| 31 | 31 | */ |
| 32 | 32 | abstract class AbstractTDBMObject implements JsonSerializable |
| 33 | 33 | { |
| 34 | - /** |
|
| 35 | - * The service this object is bound to. |
|
| 36 | - * |
|
| 37 | - * @var TDBMService |
|
| 38 | - */ |
|
| 39 | - protected $tdbmService; |
|
| 40 | - |
|
| 41 | - /** |
|
| 42 | - * An array of DbRow, indexed by table name. |
|
| 43 | - * |
|
| 44 | - * @var DbRow[] |
|
| 45 | - */ |
|
| 46 | - protected $dbRows = array(); |
|
| 47 | - |
|
| 48 | - /** |
|
| 49 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 50 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 51 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 52 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 53 | - * |
|
| 54 | - * @var string |
|
| 55 | - */ |
|
| 56 | - private $status; |
|
| 57 | - |
|
| 58 | - /** |
|
| 59 | - * Array storing beans related via many to many relationships (pivot tables). |
|
| 60 | - * |
|
| 61 | - * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
| 62 | - */ |
|
| 63 | - private $relationships = []; |
|
| 64 | - |
|
| 65 | - /** |
|
| 66 | - * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
| 67 | - */ |
|
| 68 | - private $loadedRelationships = []; |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
| 72 | - * |
|
| 73 | - * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
| 74 | - */ |
|
| 75 | - private $manyToOneRelationships = []; |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Used with $primaryKeys when we want to retrieve an existing object |
|
| 79 | - * and $primaryKeys=[] if we want a new object. |
|
| 80 | - * |
|
| 81 | - * @param string $tableName |
|
| 82 | - * @param array $primaryKeys |
|
| 83 | - * @param TDBMService $tdbmService |
|
| 84 | - * |
|
| 85 | - * @throws TDBMException |
|
| 86 | - * @throws TDBMInvalidOperationException |
|
| 87 | - */ |
|
| 88 | - public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null) |
|
| 89 | - { |
|
| 90 | - // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
| 91 | - if (!empty($tableName)) { |
|
| 92 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - if ($tdbmService === null) { |
|
| 96 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
| 97 | - } else { |
|
| 98 | - $this->_attach($tdbmService); |
|
| 99 | - if (!empty($primaryKeys)) { |
|
| 100 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
| 101 | - } else { |
|
| 102 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
| 103 | - } |
|
| 104 | - } |
|
| 105 | - } |
|
| 106 | - |
|
| 107 | - /** |
|
| 108 | - * Alternative constructor called when data is fetched from database via a SELECT. |
|
| 109 | - * |
|
| 110 | - * @param array $beanData array<table, array<column, value>> |
|
| 111 | - * @param TDBMService $tdbmService |
|
| 112 | - */ |
|
| 113 | - public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
| 114 | - { |
|
| 115 | - $this->tdbmService = $tdbmService; |
|
| 116 | - |
|
| 117 | - foreach ($beanData as $table => $columns) { |
|
| 118 | - $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
| 119 | - } |
|
| 120 | - |
|
| 121 | - $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
| 122 | - } |
|
| 123 | - |
|
| 124 | - /** |
|
| 125 | - * Alternative constructor called when bean is lazily loaded. |
|
| 126 | - * |
|
| 127 | - * @param string $tableName |
|
| 128 | - * @param array $primaryKeys |
|
| 129 | - * @param TDBMService $tdbmService |
|
| 130 | - */ |
|
| 131 | - public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
| 132 | - { |
|
| 133 | - $this->tdbmService = $tdbmService; |
|
| 134 | - |
|
| 135 | - $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
| 136 | - |
|
| 137 | - $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
| 138 | - } |
|
| 139 | - |
|
| 140 | - public function _attach(TDBMService $tdbmService) |
|
| 141 | - { |
|
| 142 | - if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 143 | - throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
| 144 | - } |
|
| 145 | - $this->tdbmService = $tdbmService; |
|
| 146 | - |
|
| 147 | - // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
| 148 | - $tableNames = $this->getUsedTables(); |
|
| 149 | - |
|
| 150 | - $newDbRows = []; |
|
| 151 | - |
|
| 152 | - foreach ($tableNames as $table) { |
|
| 153 | - if (!isset($this->dbRows[$table])) { |
|
| 154 | - $this->registerTable($table); |
|
| 155 | - } |
|
| 156 | - $newDbRows[$table] = $this->dbRows[$table]; |
|
| 157 | - } |
|
| 158 | - $this->dbRows = $newDbRows; |
|
| 159 | - |
|
| 160 | - $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
| 161 | - foreach ($this->dbRows as $dbRow) { |
|
| 162 | - $dbRow->_attach($tdbmService); |
|
| 163 | - } |
|
| 164 | - } |
|
| 165 | - |
|
| 166 | - /** |
|
| 167 | - * Sets the state of the TDBM Object |
|
| 168 | - * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 169 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 170 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 171 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 172 | - * |
|
| 173 | - * @param string $state |
|
| 174 | - */ |
|
| 175 | - public function _setStatus($state) |
|
| 176 | - { |
|
| 177 | - $this->status = $state; |
|
| 178 | - |
|
| 179 | - // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
| 180 | - foreach ($this->dbRows as $dbRow) { |
|
| 181 | - $dbRow->_setStatus($state); |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
| 185 | - $this->onDelete(); |
|
| 186 | - } |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
| 191 | - * or throws an error. |
|
| 192 | - * |
|
| 193 | - * @param string $tableName |
|
| 194 | - * |
|
| 195 | - * @return string |
|
| 196 | - */ |
|
| 197 | - private function checkTableName($tableName = null) |
|
| 198 | - { |
|
| 199 | - if ($tableName === null) { |
|
| 200 | - if (count($this->dbRows) > 1) { |
|
| 201 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 202 | - } elseif (count($this->dbRows) === 1) { |
|
| 203 | - $tableName = array_keys($this->dbRows)[0]; |
|
| 204 | - } |
|
| 205 | - } |
|
| 206 | - |
|
| 207 | - if (!isset($this->dbRows[$tableName])) { |
|
| 208 | - if (count($this->dbRows === 0)) { |
|
| 209 | - throw new TDBMException('Object is not yet bound to any table.'); |
|
| 210 | - } else { |
|
| 211 | - throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
| 212 | - } |
|
| 213 | - } |
|
| 214 | - |
|
| 215 | - return $tableName; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - protected function get($var, $tableName = null) |
|
| 219 | - { |
|
| 220 | - $tableName = $this->checkTableName($tableName); |
|
| 221 | - |
|
| 222 | - return $this->dbRows[$tableName]->get($var); |
|
| 223 | - } |
|
| 224 | - |
|
| 225 | - protected function set($var, $value, $tableName = null) |
|
| 226 | - { |
|
| 227 | - if ($tableName === null) { |
|
| 228 | - if (count($this->dbRows) > 1) { |
|
| 229 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 230 | - } elseif (count($this->dbRows) === 1) { |
|
| 231 | - $tableName = array_keys($this->dbRows)[0]; |
|
| 232 | - } else { |
|
| 233 | - throw new TDBMException('Please specify a table for this object.'); |
|
| 234 | - } |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - if (!isset($this->dbRows[$tableName])) { |
|
| 238 | - $this->registerTable($tableName); |
|
| 239 | - } |
|
| 240 | - |
|
| 241 | - $this->dbRows[$tableName]->set($var, $value); |
|
| 242 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 243 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 244 | - } |
|
| 245 | - } |
|
| 246 | - |
|
| 247 | - /** |
|
| 248 | - * @param string $foreignKeyName |
|
| 249 | - * @param AbstractTDBMObject $bean |
|
| 250 | - */ |
|
| 251 | - protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
| 252 | - { |
|
| 253 | - if ($tableName === null) { |
|
| 254 | - if (count($this->dbRows) > 1) { |
|
| 255 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 256 | - } elseif (count($this->dbRows) === 1) { |
|
| 257 | - $tableName = array_keys($this->dbRows)[0]; |
|
| 258 | - } else { |
|
| 259 | - throw new TDBMException('Please specify a table for this object.'); |
|
| 260 | - } |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - if (!isset($this->dbRows[$tableName])) { |
|
| 264 | - $this->registerTable($tableName); |
|
| 265 | - } |
|
| 266 | - |
|
| 267 | - $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 268 | - if ($oldLinkedBean !== null) { |
|
| 269 | - $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
| 273 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 274 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 275 | - } |
|
| 276 | - |
|
| 277 | - if ($bean !== null) { |
|
| 278 | - $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 279 | - } |
|
| 280 | - } |
|
| 281 | - |
|
| 282 | - /** |
|
| 283 | - * @param string $foreignKeyName A unique name for this reference |
|
| 284 | - * |
|
| 285 | - * @return AbstractTDBMObject|null |
|
| 286 | - */ |
|
| 287 | - protected function getRef($foreignKeyName, $tableName = null) |
|
| 288 | - { |
|
| 289 | - $tableName = $this->checkTableName($tableName); |
|
| 290 | - |
|
| 291 | - return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 292 | - } |
|
| 293 | - |
|
| 294 | - /** |
|
| 295 | - * Adds a many to many relationship to this bean. |
|
| 296 | - * |
|
| 297 | - * @param string $pivotTableName |
|
| 298 | - * @param AbstractTDBMObject $remoteBean |
|
| 299 | - */ |
|
| 300 | - protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 301 | - { |
|
| 302 | - $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
| 303 | - } |
|
| 304 | - |
|
| 305 | - /** |
|
| 306 | - * Returns true if there is a relationship to this bean. |
|
| 307 | - * |
|
| 308 | - * @param string $pivotTableName |
|
| 309 | - * @param AbstractTDBMObject $remoteBean |
|
| 310 | - * |
|
| 311 | - * @return bool |
|
| 312 | - */ |
|
| 313 | - protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 314 | - { |
|
| 315 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
| 316 | - |
|
| 317 | - if ($storage->contains($remoteBean)) { |
|
| 318 | - if ($storage[$remoteBean]['status'] !== 'delete') { |
|
| 319 | - return true; |
|
| 320 | - } |
|
| 321 | - } |
|
| 322 | - |
|
| 323 | - return false; |
|
| 324 | - } |
|
| 325 | - |
|
| 326 | - /** |
|
| 327 | - * Internal TDBM method. Removes a many to many relationship from this bean. |
|
| 328 | - * |
|
| 329 | - * @param string $pivotTableName |
|
| 330 | - * @param AbstractTDBMObject $remoteBean |
|
| 331 | - */ |
|
| 332 | - public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 333 | - { |
|
| 334 | - if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
| 335 | - unset($this->relationships[$pivotTableName][$remoteBean]); |
|
| 336 | - unset($remoteBean->relationships[$pivotTableName][$this]); |
|
| 337 | - } else { |
|
| 338 | - $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
| 339 | - } |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - /** |
|
| 343 | - * Sets many to many relationships for this bean. |
|
| 344 | - * Adds new relationships and removes unused ones. |
|
| 345 | - * |
|
| 346 | - * @param $pivotTableName |
|
| 347 | - * @param array $remoteBeans |
|
| 348 | - */ |
|
| 349 | - protected function setRelationships($pivotTableName, array $remoteBeans) |
|
| 350 | - { |
|
| 351 | - $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
| 352 | - |
|
| 353 | - foreach ($storage as $oldRemoteBean) { |
|
| 354 | - if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
| 355 | - // $oldRemoteBean must be removed |
|
| 356 | - $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
| 357 | - } |
|
| 358 | - } |
|
| 359 | - |
|
| 360 | - foreach ($remoteBeans as $remoteBean) { |
|
| 361 | - if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
| 362 | - // $remoteBean must be added |
|
| 363 | - $this->addRelationship($pivotTableName, $remoteBean); |
|
| 364 | - } |
|
| 365 | - } |
|
| 366 | - } |
|
| 367 | - |
|
| 368 | - /** |
|
| 369 | - * Returns the list of objects linked to this bean via $pivotTableName. |
|
| 370 | - * |
|
| 371 | - * @param $pivotTableName |
|
| 372 | - * |
|
| 373 | - * @return \SplObjectStorage |
|
| 374 | - */ |
|
| 375 | - private function retrieveRelationshipsStorage($pivotTableName) |
|
| 376 | - { |
|
| 377 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
| 378 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
| 379 | - return $storage; |
|
| 380 | - } |
|
| 381 | - |
|
| 382 | - $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
| 383 | - $this->loadedRelationships[$pivotTableName] = true; |
|
| 384 | - |
|
| 385 | - foreach ($beans as $bean) { |
|
| 386 | - if (isset($storage[$bean])) { |
|
| 387 | - $oldStatus = $storage[$bean]['status']; |
|
| 388 | - if ($oldStatus === 'delete') { |
|
| 389 | - // Keep deleted things deleted |
|
| 390 | - continue; |
|
| 391 | - } |
|
| 392 | - } |
|
| 393 | - $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
| 394 | - } |
|
| 395 | - |
|
| 396 | - return $storage; |
|
| 397 | - } |
|
| 398 | - |
|
| 399 | - /** |
|
| 400 | - * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
| 401 | - * |
|
| 402 | - * @param $pivotTableName |
|
| 403 | - * |
|
| 404 | - * @return AbstractTDBMObject[] |
|
| 405 | - */ |
|
| 406 | - public function _getRelationships($pivotTableName) |
|
| 407 | - { |
|
| 408 | - return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
| 409 | - } |
|
| 410 | - |
|
| 411 | - private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
| 412 | - { |
|
| 413 | - $beans = []; |
|
| 414 | - foreach ($storage as $bean) { |
|
| 415 | - $statusArr = $storage[$bean]; |
|
| 416 | - if ($statusArr['status'] !== 'delete') { |
|
| 417 | - $beans[] = $bean; |
|
| 418 | - } |
|
| 419 | - } |
|
| 420 | - |
|
| 421 | - return $beans; |
|
| 422 | - } |
|
| 423 | - |
|
| 424 | - /** |
|
| 425 | - * Declares a relationship between. |
|
| 426 | - * |
|
| 427 | - * @param string $pivotTableName |
|
| 428 | - * @param AbstractTDBMObject $remoteBean |
|
| 429 | - * @param string $status |
|
| 430 | - */ |
|
| 431 | - private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
| 432 | - { |
|
| 433 | - $storage = $this->getRelationshipStorage($pivotTableName); |
|
| 434 | - $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
| 435 | - if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
| 436 | - $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
| 437 | - } |
|
| 438 | - |
|
| 439 | - $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
| 440 | - $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
| 441 | - } |
|
| 442 | - |
|
| 443 | - /** |
|
| 444 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
| 445 | - * |
|
| 446 | - * @param string $pivotTableName |
|
| 447 | - * |
|
| 448 | - * @return \SplObjectStorage |
|
| 449 | - */ |
|
| 450 | - private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
| 451 | - { |
|
| 452 | - return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
| 453 | - } |
|
| 454 | - |
|
| 455 | - /** |
|
| 456 | - * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
| 457 | - * |
|
| 458 | - * @param string $tableName |
|
| 459 | - * @param string $foreignKeyName |
|
| 460 | - * |
|
| 461 | - * @return AlterableResultIterator |
|
| 462 | - */ |
|
| 463 | - private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
| 464 | - { |
|
| 465 | - $key = $tableName.'___'.$foreignKeyName; |
|
| 466 | - |
|
| 467 | - return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
| 468 | - } |
|
| 469 | - |
|
| 470 | - /** |
|
| 471 | - * Declares a relationship between this bean and the bean pointing to it. |
|
| 472 | - * |
|
| 473 | - * @param string $tableName |
|
| 474 | - * @param string $foreignKeyName |
|
| 475 | - * @param AbstractTDBMObject $remoteBean |
|
| 476 | - */ |
|
| 477 | - private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
| 478 | - { |
|
| 479 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 480 | - $alterableResultIterator->add($remoteBean); |
|
| 481 | - } |
|
| 482 | - |
|
| 483 | - /** |
|
| 484 | - * Declares a relationship between this bean and the bean pointing to it. |
|
| 485 | - * |
|
| 486 | - * @param string $tableName |
|
| 487 | - * @param string $foreignKeyName |
|
| 488 | - * @param AbstractTDBMObject $remoteBean |
|
| 489 | - */ |
|
| 490 | - private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
| 491 | - { |
|
| 492 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 493 | - $alterableResultIterator->remove($remoteBean); |
|
| 494 | - } |
|
| 495 | - |
|
| 496 | - /** |
|
| 497 | - * Returns the list of objects linked to this bean via a given foreign key. |
|
| 498 | - * |
|
| 499 | - * @param string $tableName |
|
| 500 | - * @param string $foreignKeyName |
|
| 501 | - * @param string $searchTableName |
|
| 502 | - * @param array $searchFilter |
|
| 503 | - * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
| 504 | - * |
|
| 505 | - * @return AlterableResultIterator |
|
| 506 | - */ |
|
| 507 | - protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
| 508 | - { |
|
| 509 | - $key = $tableName.'___'.$foreignKeyName; |
|
| 510 | - $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 511 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
| 512 | - return $alterableResultIterator; |
|
| 513 | - } |
|
| 514 | - |
|
| 515 | - $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
| 516 | - |
|
| 517 | - $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
| 518 | - |
|
| 519 | - return $alterableResultIterator; |
|
| 520 | - } |
|
| 521 | - |
|
| 522 | - /** |
|
| 523 | - * Reverts any changes made to the object and resumes it to its DB state. |
|
| 524 | - * This can only be called on objects that come from database and that have not been deleted. |
|
| 525 | - * Otherwise, this will throw an exception. |
|
| 526 | - * |
|
| 527 | - * @throws TDBMException |
|
| 528 | - */ |
|
| 529 | - public function discardChanges() |
|
| 530 | - { |
|
| 531 | - if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 532 | - throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
| 533 | - } |
|
| 534 | - |
|
| 535 | - if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
| 536 | - throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
| 537 | - } |
|
| 538 | - |
|
| 539 | - $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
| 540 | - } |
|
| 541 | - |
|
| 542 | - /** |
|
| 543 | - * Method used internally by TDBM. You should not use it directly. |
|
| 544 | - * This method returns the status of the TDBMObject. |
|
| 545 | - * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 546 | - * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 547 | - * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 548 | - * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 549 | - * |
|
| 550 | - * @return string |
|
| 551 | - */ |
|
| 552 | - public function _getStatus() |
|
| 553 | - { |
|
| 554 | - return $this->status; |
|
| 555 | - } |
|
| 556 | - |
|
| 557 | - /** |
|
| 558 | - * Override the native php clone function for TDBMObjects. |
|
| 559 | - */ |
|
| 560 | - public function __clone() |
|
| 561 | - { |
|
| 562 | - // Let's clone the many to many relationships |
|
| 563 | - if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 564 | - $pivotTableList = array_keys($this->relationships); |
|
| 565 | - } else { |
|
| 566 | - $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
| 567 | - } |
|
| 568 | - |
|
| 569 | - foreach ($pivotTableList as $pivotTable) { |
|
| 570 | - $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
| 571 | - |
|
| 572 | - // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
| 573 | - /*foreach ($storage as $remoteBean) { |
|
| 34 | + /** |
|
| 35 | + * The service this object is bound to. |
|
| 36 | + * |
|
| 37 | + * @var TDBMService |
|
| 38 | + */ |
|
| 39 | + protected $tdbmService; |
|
| 40 | + |
|
| 41 | + /** |
|
| 42 | + * An array of DbRow, indexed by table name. |
|
| 43 | + * |
|
| 44 | + * @var DbRow[] |
|
| 45 | + */ |
|
| 46 | + protected $dbRows = array(); |
|
| 47 | + |
|
| 48 | + /** |
|
| 49 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 50 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 51 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 52 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 53 | + * |
|
| 54 | + * @var string |
|
| 55 | + */ |
|
| 56 | + private $status; |
|
| 57 | + |
|
| 58 | + /** |
|
| 59 | + * Array storing beans related via many to many relationships (pivot tables). |
|
| 60 | + * |
|
| 61 | + * @var \SplObjectStorage[] Key: pivot table name, value: SplObjectStorage |
|
| 62 | + */ |
|
| 63 | + private $relationships = []; |
|
| 64 | + |
|
| 65 | + /** |
|
| 66 | + * @var bool[] Key: pivot table name, value: whether a query was performed to load the data |
|
| 67 | + */ |
|
| 68 | + private $loadedRelationships = []; |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Array storing beans related via many to one relationships (this bean is pointed by external beans). |
|
| 72 | + * |
|
| 73 | + * @var AlterableResultIterator[] Key: [external_table]___[external_column], value: SplObjectStorage |
|
| 74 | + */ |
|
| 75 | + private $manyToOneRelationships = []; |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Used with $primaryKeys when we want to retrieve an existing object |
|
| 79 | + * and $primaryKeys=[] if we want a new object. |
|
| 80 | + * |
|
| 81 | + * @param string $tableName |
|
| 82 | + * @param array $primaryKeys |
|
| 83 | + * @param TDBMService $tdbmService |
|
| 84 | + * |
|
| 85 | + * @throws TDBMException |
|
| 86 | + * @throws TDBMInvalidOperationException |
|
| 87 | + */ |
|
| 88 | + public function __construct($tableName = null, array $primaryKeys = array(), TDBMService $tdbmService = null) |
|
| 89 | + { |
|
| 90 | + // FIXME: lazy loading should be forbidden on tables with inheritance and dynamic type assignation... |
|
| 91 | + if (!empty($tableName)) { |
|
| 92 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + if ($tdbmService === null) { |
|
| 96 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DETACHED); |
|
| 97 | + } else { |
|
| 98 | + $this->_attach($tdbmService); |
|
| 99 | + if (!empty($primaryKeys)) { |
|
| 100 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
| 101 | + } else { |
|
| 102 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NEW); |
|
| 103 | + } |
|
| 104 | + } |
|
| 105 | + } |
|
| 106 | + |
|
| 107 | + /** |
|
| 108 | + * Alternative constructor called when data is fetched from database via a SELECT. |
|
| 109 | + * |
|
| 110 | + * @param array $beanData array<table, array<column, value>> |
|
| 111 | + * @param TDBMService $tdbmService |
|
| 112 | + */ |
|
| 113 | + public function _constructFromData(array $beanData, TDBMService $tdbmService) |
|
| 114 | + { |
|
| 115 | + $this->tdbmService = $tdbmService; |
|
| 116 | + |
|
| 117 | + foreach ($beanData as $table => $columns) { |
|
| 118 | + $this->dbRows[$table] = new DbRow($this, $table, $tdbmService->_getPrimaryKeysFromObjectData($table, $columns), $tdbmService, $columns); |
|
| 119 | + } |
|
| 120 | + |
|
| 121 | + $this->status = TDBMObjectStateEnum::STATE_LOADED; |
|
| 122 | + } |
|
| 123 | + |
|
| 124 | + /** |
|
| 125 | + * Alternative constructor called when bean is lazily loaded. |
|
| 126 | + * |
|
| 127 | + * @param string $tableName |
|
| 128 | + * @param array $primaryKeys |
|
| 129 | + * @param TDBMService $tdbmService |
|
| 130 | + */ |
|
| 131 | + public function _constructLazy($tableName, array $primaryKeys, TDBMService $tdbmService) |
|
| 132 | + { |
|
| 133 | + $this->tdbmService = $tdbmService; |
|
| 134 | + |
|
| 135 | + $this->dbRows[$tableName] = new DbRow($this, $tableName, $primaryKeys, $tdbmService); |
|
| 136 | + |
|
| 137 | + $this->status = TDBMObjectStateEnum::STATE_NOT_LOADED; |
|
| 138 | + } |
|
| 139 | + |
|
| 140 | + public function _attach(TDBMService $tdbmService) |
|
| 141 | + { |
|
| 142 | + if ($this->status !== TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 143 | + throw new TDBMInvalidOperationException('Cannot attach an object that is already attached to TDBM.'); |
|
| 144 | + } |
|
| 145 | + $this->tdbmService = $tdbmService; |
|
| 146 | + |
|
| 147 | + // If we attach this object, we must work to make sure the tables are in ascending order (from low level to top level) |
|
| 148 | + $tableNames = $this->getUsedTables(); |
|
| 149 | + |
|
| 150 | + $newDbRows = []; |
|
| 151 | + |
|
| 152 | + foreach ($tableNames as $table) { |
|
| 153 | + if (!isset($this->dbRows[$table])) { |
|
| 154 | + $this->registerTable($table); |
|
| 155 | + } |
|
| 156 | + $newDbRows[$table] = $this->dbRows[$table]; |
|
| 157 | + } |
|
| 158 | + $this->dbRows = $newDbRows; |
|
| 159 | + |
|
| 160 | + $this->status = TDBMObjectStateEnum::STATE_NEW; |
|
| 161 | + foreach ($this->dbRows as $dbRow) { |
|
| 162 | + $dbRow->_attach($tdbmService); |
|
| 163 | + } |
|
| 164 | + } |
|
| 165 | + |
|
| 166 | + /** |
|
| 167 | + * Sets the state of the TDBM Object |
|
| 168 | + * One of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 169 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 170 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 171 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 172 | + * |
|
| 173 | + * @param string $state |
|
| 174 | + */ |
|
| 175 | + public function _setStatus($state) |
|
| 176 | + { |
|
| 177 | + $this->status = $state; |
|
| 178 | + |
|
| 179 | + // TODO: we might ignore the loaded => dirty state here! dirty status comes from the db_row itself. |
|
| 180 | + foreach ($this->dbRows as $dbRow) { |
|
| 181 | + $dbRow->_setStatus($state); |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + if ($state === TDBMObjectStateEnum::STATE_DELETED) { |
|
| 185 | + $this->onDelete(); |
|
| 186 | + } |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Checks that $tableName is ok, or returns the only possible table name if "$tableName = null" |
|
| 191 | + * or throws an error. |
|
| 192 | + * |
|
| 193 | + * @param string $tableName |
|
| 194 | + * |
|
| 195 | + * @return string |
|
| 196 | + */ |
|
| 197 | + private function checkTableName($tableName = null) |
|
| 198 | + { |
|
| 199 | + if ($tableName === null) { |
|
| 200 | + if (count($this->dbRows) > 1) { |
|
| 201 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 202 | + } elseif (count($this->dbRows) === 1) { |
|
| 203 | + $tableName = array_keys($this->dbRows)[0]; |
|
| 204 | + } |
|
| 205 | + } |
|
| 206 | + |
|
| 207 | + if (!isset($this->dbRows[$tableName])) { |
|
| 208 | + if (count($this->dbRows === 0)) { |
|
| 209 | + throw new TDBMException('Object is not yet bound to any table.'); |
|
| 210 | + } else { |
|
| 211 | + throw new TDBMException('Unknown table "'.$tableName.'"" in object.'); |
|
| 212 | + } |
|
| 213 | + } |
|
| 214 | + |
|
| 215 | + return $tableName; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + protected function get($var, $tableName = null) |
|
| 219 | + { |
|
| 220 | + $tableName = $this->checkTableName($tableName); |
|
| 221 | + |
|
| 222 | + return $this->dbRows[$tableName]->get($var); |
|
| 223 | + } |
|
| 224 | + |
|
| 225 | + protected function set($var, $value, $tableName = null) |
|
| 226 | + { |
|
| 227 | + if ($tableName === null) { |
|
| 228 | + if (count($this->dbRows) > 1) { |
|
| 229 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 230 | + } elseif (count($this->dbRows) === 1) { |
|
| 231 | + $tableName = array_keys($this->dbRows)[0]; |
|
| 232 | + } else { |
|
| 233 | + throw new TDBMException('Please specify a table for this object.'); |
|
| 234 | + } |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + if (!isset($this->dbRows[$tableName])) { |
|
| 238 | + $this->registerTable($tableName); |
|
| 239 | + } |
|
| 240 | + |
|
| 241 | + $this->dbRows[$tableName]->set($var, $value); |
|
| 242 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 243 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 244 | + } |
|
| 245 | + } |
|
| 246 | + |
|
| 247 | + /** |
|
| 248 | + * @param string $foreignKeyName |
|
| 249 | + * @param AbstractTDBMObject $bean |
|
| 250 | + */ |
|
| 251 | + protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
| 252 | + { |
|
| 253 | + if ($tableName === null) { |
|
| 254 | + if (count($this->dbRows) > 1) { |
|
| 255 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 256 | + } elseif (count($this->dbRows) === 1) { |
|
| 257 | + $tableName = array_keys($this->dbRows)[0]; |
|
| 258 | + } else { |
|
| 259 | + throw new TDBMException('Please specify a table for this object.'); |
|
| 260 | + } |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + if (!isset($this->dbRows[$tableName])) { |
|
| 264 | + $this->registerTable($tableName); |
|
| 265 | + } |
|
| 266 | + |
|
| 267 | + $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 268 | + if ($oldLinkedBean !== null) { |
|
| 269 | + $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
| 273 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 274 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 275 | + } |
|
| 276 | + |
|
| 277 | + if ($bean !== null) { |
|
| 278 | + $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 279 | + } |
|
| 280 | + } |
|
| 281 | + |
|
| 282 | + /** |
|
| 283 | + * @param string $foreignKeyName A unique name for this reference |
|
| 284 | + * |
|
| 285 | + * @return AbstractTDBMObject|null |
|
| 286 | + */ |
|
| 287 | + protected function getRef($foreignKeyName, $tableName = null) |
|
| 288 | + { |
|
| 289 | + $tableName = $this->checkTableName($tableName); |
|
| 290 | + |
|
| 291 | + return $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 292 | + } |
|
| 293 | + |
|
| 294 | + /** |
|
| 295 | + * Adds a many to many relationship to this bean. |
|
| 296 | + * |
|
| 297 | + * @param string $pivotTableName |
|
| 298 | + * @param AbstractTDBMObject $remoteBean |
|
| 299 | + */ |
|
| 300 | + protected function addRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 301 | + { |
|
| 302 | + $this->setRelationship($pivotTableName, $remoteBean, 'new'); |
|
| 303 | + } |
|
| 304 | + |
|
| 305 | + /** |
|
| 306 | + * Returns true if there is a relationship to this bean. |
|
| 307 | + * |
|
| 308 | + * @param string $pivotTableName |
|
| 309 | + * @param AbstractTDBMObject $remoteBean |
|
| 310 | + * |
|
| 311 | + * @return bool |
|
| 312 | + */ |
|
| 313 | + protected function hasRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 314 | + { |
|
| 315 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
| 316 | + |
|
| 317 | + if ($storage->contains($remoteBean)) { |
|
| 318 | + if ($storage[$remoteBean]['status'] !== 'delete') { |
|
| 319 | + return true; |
|
| 320 | + } |
|
| 321 | + } |
|
| 322 | + |
|
| 323 | + return false; |
|
| 324 | + } |
|
| 325 | + |
|
| 326 | + /** |
|
| 327 | + * Internal TDBM method. Removes a many to many relationship from this bean. |
|
| 328 | + * |
|
| 329 | + * @param string $pivotTableName |
|
| 330 | + * @param AbstractTDBMObject $remoteBean |
|
| 331 | + */ |
|
| 332 | + public function _removeRelationship($pivotTableName, AbstractTDBMObject $remoteBean) |
|
| 333 | + { |
|
| 334 | + if (isset($this->relationships[$pivotTableName][$remoteBean]) && $this->relationships[$pivotTableName][$remoteBean]['status'] === 'new') { |
|
| 335 | + unset($this->relationships[$pivotTableName][$remoteBean]); |
|
| 336 | + unset($remoteBean->relationships[$pivotTableName][$this]); |
|
| 337 | + } else { |
|
| 338 | + $this->setRelationship($pivotTableName, $remoteBean, 'delete'); |
|
| 339 | + } |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + /** |
|
| 343 | + * Sets many to many relationships for this bean. |
|
| 344 | + * Adds new relationships and removes unused ones. |
|
| 345 | + * |
|
| 346 | + * @param $pivotTableName |
|
| 347 | + * @param array $remoteBeans |
|
| 348 | + */ |
|
| 349 | + protected function setRelationships($pivotTableName, array $remoteBeans) |
|
| 350 | + { |
|
| 351 | + $storage = $this->retrieveRelationshipsStorage($pivotTableName); |
|
| 352 | + |
|
| 353 | + foreach ($storage as $oldRemoteBean) { |
|
| 354 | + if (!in_array($oldRemoteBean, $remoteBeans, true)) { |
|
| 355 | + // $oldRemoteBean must be removed |
|
| 356 | + $this->_removeRelationship($pivotTableName, $oldRemoteBean); |
|
| 357 | + } |
|
| 358 | + } |
|
| 359 | + |
|
| 360 | + foreach ($remoteBeans as $remoteBean) { |
|
| 361 | + if (!$storage->contains($remoteBean) || $storage[$remoteBean]['status'] === 'delete') { |
|
| 362 | + // $remoteBean must be added |
|
| 363 | + $this->addRelationship($pivotTableName, $remoteBean); |
|
| 364 | + } |
|
| 365 | + } |
|
| 366 | + } |
|
| 367 | + |
|
| 368 | + /** |
|
| 369 | + * Returns the list of objects linked to this bean via $pivotTableName. |
|
| 370 | + * |
|
| 371 | + * @param $pivotTableName |
|
| 372 | + * |
|
| 373 | + * @return \SplObjectStorage |
|
| 374 | + */ |
|
| 375 | + private function retrieveRelationshipsStorage($pivotTableName) |
|
| 376 | + { |
|
| 377 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
| 378 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->loadedRelationships[$pivotTableName]) && $this->loadedRelationships[$pivotTableName])) { |
|
| 379 | + return $storage; |
|
| 380 | + } |
|
| 381 | + |
|
| 382 | + $beans = $this->tdbmService->_getRelatedBeans($pivotTableName, $this); |
|
| 383 | + $this->loadedRelationships[$pivotTableName] = true; |
|
| 384 | + |
|
| 385 | + foreach ($beans as $bean) { |
|
| 386 | + if (isset($storage[$bean])) { |
|
| 387 | + $oldStatus = $storage[$bean]['status']; |
|
| 388 | + if ($oldStatus === 'delete') { |
|
| 389 | + // Keep deleted things deleted |
|
| 390 | + continue; |
|
| 391 | + } |
|
| 392 | + } |
|
| 393 | + $this->setRelationship($pivotTableName, $bean, 'loaded'); |
|
| 394 | + } |
|
| 395 | + |
|
| 396 | + return $storage; |
|
| 397 | + } |
|
| 398 | + |
|
| 399 | + /** |
|
| 400 | + * Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName. |
|
| 401 | + * |
|
| 402 | + * @param $pivotTableName |
|
| 403 | + * |
|
| 404 | + * @return AbstractTDBMObject[] |
|
| 405 | + */ |
|
| 406 | + public function _getRelationships($pivotTableName) |
|
| 407 | + { |
|
| 408 | + return $this->relationshipStorageToArray($this->retrieveRelationshipsStorage($pivotTableName)); |
|
| 409 | + } |
|
| 410 | + |
|
| 411 | + private function relationshipStorageToArray(\SplObjectStorage $storage) |
|
| 412 | + { |
|
| 413 | + $beans = []; |
|
| 414 | + foreach ($storage as $bean) { |
|
| 415 | + $statusArr = $storage[$bean]; |
|
| 416 | + if ($statusArr['status'] !== 'delete') { |
|
| 417 | + $beans[] = $bean; |
|
| 418 | + } |
|
| 419 | + } |
|
| 420 | + |
|
| 421 | + return $beans; |
|
| 422 | + } |
|
| 423 | + |
|
| 424 | + /** |
|
| 425 | + * Declares a relationship between. |
|
| 426 | + * |
|
| 427 | + * @param string $pivotTableName |
|
| 428 | + * @param AbstractTDBMObject $remoteBean |
|
| 429 | + * @param string $status |
|
| 430 | + */ |
|
| 431 | + private function setRelationship($pivotTableName, AbstractTDBMObject $remoteBean, $status) |
|
| 432 | + { |
|
| 433 | + $storage = $this->getRelationshipStorage($pivotTableName); |
|
| 434 | + $storage->attach($remoteBean, ['status' => $status, 'reverse' => false]); |
|
| 435 | + if ($this->status === TDBMObjectStateEnum::STATE_LOADED) { |
|
| 436 | + $this->_setStatus(TDBMObjectStateEnum::STATE_DIRTY); |
|
| 437 | + } |
|
| 438 | + |
|
| 439 | + $remoteStorage = $remoteBean->getRelationshipStorage($pivotTableName); |
|
| 440 | + $remoteStorage->attach($this, ['status' => $status, 'reverse' => true]); |
|
| 441 | + } |
|
| 442 | + |
|
| 443 | + /** |
|
| 444 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
| 445 | + * |
|
| 446 | + * @param string $pivotTableName |
|
| 447 | + * |
|
| 448 | + * @return \SplObjectStorage |
|
| 449 | + */ |
|
| 450 | + private function getRelationshipStorage(string $pivotTableName) : \SplObjectStorage |
|
| 451 | + { |
|
| 452 | + return $this->relationships[$pivotTableName] ?? $this->relationships[$pivotTableName] = new \SplObjectStorage(); |
|
| 453 | + } |
|
| 454 | + |
|
| 455 | + /** |
|
| 456 | + * Returns the SplObjectStorage associated to this relationship (creates it if it does not exists). |
|
| 457 | + * |
|
| 458 | + * @param string $tableName |
|
| 459 | + * @param string $foreignKeyName |
|
| 460 | + * |
|
| 461 | + * @return AlterableResultIterator |
|
| 462 | + */ |
|
| 463 | + private function getManyToOneAlterableResultIterator(string $tableName, string $foreignKeyName) : AlterableResultIterator |
|
| 464 | + { |
|
| 465 | + $key = $tableName.'___'.$foreignKeyName; |
|
| 466 | + |
|
| 467 | + return $this->manyToOneRelationships[$key] ?? $this->manyToOneRelationships[$key] = new AlterableResultIterator(); |
|
| 468 | + } |
|
| 469 | + |
|
| 470 | + /** |
|
| 471 | + * Declares a relationship between this bean and the bean pointing to it. |
|
| 472 | + * |
|
| 473 | + * @param string $tableName |
|
| 474 | + * @param string $foreignKeyName |
|
| 475 | + * @param AbstractTDBMObject $remoteBean |
|
| 476 | + */ |
|
| 477 | + private function setManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
| 478 | + { |
|
| 479 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 480 | + $alterableResultIterator->add($remoteBean); |
|
| 481 | + } |
|
| 482 | + |
|
| 483 | + /** |
|
| 484 | + * Declares a relationship between this bean and the bean pointing to it. |
|
| 485 | + * |
|
| 486 | + * @param string $tableName |
|
| 487 | + * @param string $foreignKeyName |
|
| 488 | + * @param AbstractTDBMObject $remoteBean |
|
| 489 | + */ |
|
| 490 | + private function removeManyToOneRelationship(string $tableName, string $foreignKeyName, AbstractTDBMObject $remoteBean) |
|
| 491 | + { |
|
| 492 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 493 | + $alterableResultIterator->remove($remoteBean); |
|
| 494 | + } |
|
| 495 | + |
|
| 496 | + /** |
|
| 497 | + * Returns the list of objects linked to this bean via a given foreign key. |
|
| 498 | + * |
|
| 499 | + * @param string $tableName |
|
| 500 | + * @param string $foreignKeyName |
|
| 501 | + * @param string $searchTableName |
|
| 502 | + * @param array $searchFilter |
|
| 503 | + * @param string $orderString The ORDER BY part of the query. All columns must be prefixed by the table name (in the form: table.column). WARNING : This parameter is not kept when there is an additionnal or removal object ! |
|
| 504 | + * |
|
| 505 | + * @return AlterableResultIterator |
|
| 506 | + */ |
|
| 507 | + protected function retrieveManyToOneRelationshipsStorage(string $tableName, string $foreignKeyName, string $searchTableName, array $searchFilter, $orderString = null) : AlterableResultIterator |
|
| 508 | + { |
|
| 509 | + $key = $tableName.'___'.$foreignKeyName; |
|
| 510 | + $alterableResultIterator = $this->getManyToOneAlterableResultIterator($tableName, $foreignKeyName); |
|
| 511 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED || $this->status === TDBMObjectStateEnum::STATE_NEW || (isset($this->manyToOneRelationships[$key]) && $this->manyToOneRelationships[$key]->getUnderlyingResultIterator() !== null)) { |
|
| 512 | + return $alterableResultIterator; |
|
| 513 | + } |
|
| 514 | + |
|
| 515 | + $unalteredResultIterator = $this->tdbmService->findObjects($searchTableName, $searchFilter, [], $orderString); |
|
| 516 | + |
|
| 517 | + $alterableResultIterator->setResultIterator($unalteredResultIterator->getIterator()); |
|
| 518 | + |
|
| 519 | + return $alterableResultIterator; |
|
| 520 | + } |
|
| 521 | + |
|
| 522 | + /** |
|
| 523 | + * Reverts any changes made to the object and resumes it to its DB state. |
|
| 524 | + * This can only be called on objects that come from database and that have not been deleted. |
|
| 525 | + * Otherwise, this will throw an exception. |
|
| 526 | + * |
|
| 527 | + * @throws TDBMException |
|
| 528 | + */ |
|
| 529 | + public function discardChanges() |
|
| 530 | + { |
|
| 531 | + if ($this->status === TDBMObjectStateEnum::STATE_NEW || $this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 532 | + throw new TDBMException("You cannot call discardChanges() on an object that has been created with the 'new' keyword and that has not yet been saved."); |
|
| 533 | + } |
|
| 534 | + |
|
| 535 | + if ($this->status === TDBMObjectStateEnum::STATE_DELETED) { |
|
| 536 | + throw new TDBMException('You cannot call discardChanges() on an object that has been deleted.'); |
|
| 537 | + } |
|
| 538 | + |
|
| 539 | + $this->_setStatus(TDBMObjectStateEnum::STATE_NOT_LOADED); |
|
| 540 | + } |
|
| 541 | + |
|
| 542 | + /** |
|
| 543 | + * Method used internally by TDBM. You should not use it directly. |
|
| 544 | + * This method returns the status of the TDBMObject. |
|
| 545 | + * This is one of TDBMObjectStateEnum::STATE_NEW, TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DELETED. |
|
| 546 | + * $status = TDBMObjectStateEnum::STATE_NEW when a new object is created with DBMObject:getNewObject. |
|
| 547 | + * $status = TDBMObjectStateEnum::STATE_NOT_LOADED when the object has been retrieved with getObject but when no data has been accessed in it yet. |
|
| 548 | + * $status = TDBMObjectStateEnum::STATE_LOADED when the object is cached in memory. |
|
| 549 | + * |
|
| 550 | + * @return string |
|
| 551 | + */ |
|
| 552 | + public function _getStatus() |
|
| 553 | + { |
|
| 554 | + return $this->status; |
|
| 555 | + } |
|
| 556 | + |
|
| 557 | + /** |
|
| 558 | + * Override the native php clone function for TDBMObjects. |
|
| 559 | + */ |
|
| 560 | + public function __clone() |
|
| 561 | + { |
|
| 562 | + // Let's clone the many to many relationships |
|
| 563 | + if ($this->status === TDBMObjectStateEnum::STATE_DETACHED) { |
|
| 564 | + $pivotTableList = array_keys($this->relationships); |
|
| 565 | + } else { |
|
| 566 | + $pivotTableList = $this->tdbmService->_getPivotTablesLinkedToBean($this); |
|
| 567 | + } |
|
| 568 | + |
|
| 569 | + foreach ($pivotTableList as $pivotTable) { |
|
| 570 | + $storage = $this->retrieveRelationshipsStorage($pivotTable); |
|
| 571 | + |
|
| 572 | + // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!! |
|
| 573 | + /*foreach ($storage as $remoteBean) { |
|
| 574 | 574 | $metadata = $storage[$remoteBean]; |
| 575 | 575 | |
| 576 | 576 | $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable); |
| 577 | 577 | $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]); |
| 578 | 578 | }*/ |
| 579 | - } |
|
| 580 | - |
|
| 581 | - // Let's clone each row |
|
| 582 | - foreach ($this->dbRows as $key => &$dbRow) { |
|
| 583 | - $dbRow = clone $dbRow; |
|
| 584 | - $dbRow->setTDBMObject($this); |
|
| 585 | - } |
|
| 586 | - |
|
| 587 | - $this->manyToOneRelationships = []; |
|
| 588 | - |
|
| 589 | - // Let's set the status to new (to enter the save function) |
|
| 590 | - $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
| 591 | - } |
|
| 592 | - |
|
| 593 | - /** |
|
| 594 | - * Returns raw database rows. |
|
| 595 | - * |
|
| 596 | - * @return DbRow[] Key: table name, Value: DbRow object |
|
| 597 | - */ |
|
| 598 | - public function _getDbRows() |
|
| 599 | - { |
|
| 600 | - return $this->dbRows; |
|
| 601 | - } |
|
| 602 | - |
|
| 603 | - private function registerTable($tableName) |
|
| 604 | - { |
|
| 605 | - $dbRow = new DbRow($this, $tableName); |
|
| 606 | - |
|
| 607 | - if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
| 608 | - // Let's get the primary key for the new table |
|
| 609 | - $anotherDbRow = array_values($this->dbRows)[0]; |
|
| 610 | - /* @var $anotherDbRow DbRow */ |
|
| 611 | - $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
| 612 | - $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
| 613 | - $dbRow->_setPrimaryKeys($primaryKeys); |
|
| 614 | - } |
|
| 615 | - |
|
| 616 | - $dbRow->_setStatus($this->status); |
|
| 617 | - |
|
| 618 | - $this->dbRows[$tableName] = $dbRow; |
|
| 619 | - // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
| 620 | - } |
|
| 621 | - |
|
| 622 | - /** |
|
| 623 | - * Internal function: return the list of relationships. |
|
| 624 | - * |
|
| 625 | - * @return \SplObjectStorage[] |
|
| 626 | - */ |
|
| 627 | - public function _getCachedRelationships() |
|
| 628 | - { |
|
| 629 | - return $this->relationships; |
|
| 630 | - } |
|
| 631 | - |
|
| 632 | - /** |
|
| 633 | - * Returns an array of used tables by this bean (from parent to child relationship). |
|
| 634 | - * |
|
| 635 | - * @return string[] |
|
| 636 | - */ |
|
| 637 | - abstract protected function getUsedTables(); |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Method called when the bean is removed from database. |
|
| 641 | - */ |
|
| 642 | - protected function onDelete() |
|
| 643 | - { |
|
| 644 | - } |
|
| 579 | + } |
|
| 580 | + |
|
| 581 | + // Let's clone each row |
|
| 582 | + foreach ($this->dbRows as $key => &$dbRow) { |
|
| 583 | + $dbRow = clone $dbRow; |
|
| 584 | + $dbRow->setTDBMObject($this); |
|
| 585 | + } |
|
| 586 | + |
|
| 587 | + $this->manyToOneRelationships = []; |
|
| 588 | + |
|
| 589 | + // Let's set the status to new (to enter the save function) |
|
| 590 | + $this->status = TDBMObjectStateEnum::STATE_DETACHED; |
|
| 591 | + } |
|
| 592 | + |
|
| 593 | + /** |
|
| 594 | + * Returns raw database rows. |
|
| 595 | + * |
|
| 596 | + * @return DbRow[] Key: table name, Value: DbRow object |
|
| 597 | + */ |
|
| 598 | + public function _getDbRows() |
|
| 599 | + { |
|
| 600 | + return $this->dbRows; |
|
| 601 | + } |
|
| 602 | + |
|
| 603 | + private function registerTable($tableName) |
|
| 604 | + { |
|
| 605 | + $dbRow = new DbRow($this, $tableName); |
|
| 606 | + |
|
| 607 | + if (in_array($this->status, [TDBMObjectStateEnum::STATE_NOT_LOADED, TDBMObjectStateEnum::STATE_LOADED, TDBMObjectStateEnum::STATE_DIRTY])) { |
|
| 608 | + // Let's get the primary key for the new table |
|
| 609 | + $anotherDbRow = array_values($this->dbRows)[0]; |
|
| 610 | + /* @var $anotherDbRow DbRow */ |
|
| 611 | + $indexedPrimaryKeys = array_values($anotherDbRow->_getPrimaryKeys()); |
|
| 612 | + $primaryKeys = $this->tdbmService->_getPrimaryKeysFromIndexedPrimaryKeys($tableName, $indexedPrimaryKeys); |
|
| 613 | + $dbRow->_setPrimaryKeys($primaryKeys); |
|
| 614 | + } |
|
| 615 | + |
|
| 616 | + $dbRow->_setStatus($this->status); |
|
| 617 | + |
|
| 618 | + $this->dbRows[$tableName] = $dbRow; |
|
| 619 | + // TODO: look at status (if not new)=> get primary key from tdbmservice |
|
| 620 | + } |
|
| 621 | + |
|
| 622 | + /** |
|
| 623 | + * Internal function: return the list of relationships. |
|
| 624 | + * |
|
| 625 | + * @return \SplObjectStorage[] |
|
| 626 | + */ |
|
| 627 | + public function _getCachedRelationships() |
|
| 628 | + { |
|
| 629 | + return $this->relationships; |
|
| 630 | + } |
|
| 631 | + |
|
| 632 | + /** |
|
| 633 | + * Returns an array of used tables by this bean (from parent to child relationship). |
|
| 634 | + * |
|
| 635 | + * @return string[] |
|
| 636 | + */ |
|
| 637 | + abstract protected function getUsedTables(); |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Method called when the bean is removed from database. |
|
| 641 | + */ |
|
| 642 | + protected function onDelete() |
|
| 643 | + { |
|
| 644 | + } |
|
| 645 | 645 | } |
@@ -17,228 +17,228 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class BeanDescriptor |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var Table |
|
| 22 | - */ |
|
| 23 | - private $table; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * @var SchemaAnalyzer |
|
| 27 | - */ |
|
| 28 | - private $schemaAnalyzer; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @var Schema |
|
| 32 | - */ |
|
| 33 | - private $schema; |
|
| 34 | - |
|
| 35 | - /** |
|
| 36 | - * @var AbstractBeanPropertyDescriptor[] |
|
| 37 | - */ |
|
| 38 | - private $beanPropertyDescriptors = []; |
|
| 39 | - |
|
| 40 | - /** |
|
| 41 | - * @var TDBMSchemaAnalyzer |
|
| 42 | - */ |
|
| 43 | - private $tdbmSchemaAnalyzer; |
|
| 44 | - |
|
| 45 | - public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 46 | - { |
|
| 47 | - $this->table = $table; |
|
| 48 | - $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 49 | - $this->schema = $schema; |
|
| 50 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 51 | - $this->initBeanPropertyDescriptors(); |
|
| 52 | - } |
|
| 53 | - |
|
| 54 | - private function initBeanPropertyDescriptors() |
|
| 55 | - { |
|
| 56 | - $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
| 57 | - } |
|
| 58 | - |
|
| 59 | - /** |
|
| 60 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 61 | - * |
|
| 62 | - * @param Table $table |
|
| 63 | - * @param Column $column |
|
| 64 | - * |
|
| 65 | - * @return ForeignKeyConstraint|null |
|
| 66 | - */ |
|
| 67 | - private function isPartOfForeignKey(Table $table, Column $column) |
|
| 68 | - { |
|
| 69 | - $localColumnName = $column->getName(); |
|
| 70 | - foreach ($table->getForeignKeys() as $foreignKey) { |
|
| 71 | - foreach ($foreignKey->getColumns() as $columnName) { |
|
| 72 | - if ($columnName === $localColumnName) { |
|
| 73 | - return $foreignKey; |
|
| 74 | - } |
|
| 75 | - } |
|
| 76 | - } |
|
| 77 | - |
|
| 78 | - return; |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 83 | - */ |
|
| 84 | - public function getBeanPropertyDescriptors() |
|
| 85 | - { |
|
| 86 | - return $this->beanPropertyDescriptors; |
|
| 87 | - } |
|
| 88 | - |
|
| 89 | - /** |
|
| 90 | - * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
| 91 | - * |
|
| 92 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 93 | - */ |
|
| 94 | - public function getConstructorProperties() |
|
| 95 | - { |
|
| 96 | - $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 97 | - return $property->isCompulsory(); |
|
| 98 | - }); |
|
| 99 | - |
|
| 100 | - return $constructorProperties; |
|
| 101 | - } |
|
| 102 | - |
|
| 103 | - /** |
|
| 104 | - * Returns the list of columns that have default values for a given table. |
|
| 105 | - * |
|
| 106 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 107 | - */ |
|
| 108 | - public function getPropertiesWithDefault() |
|
| 109 | - { |
|
| 110 | - $properties = $this->getPropertiesForTable($this->table); |
|
| 111 | - $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
| 112 | - return $property->hasDefault(); |
|
| 113 | - }); |
|
| 114 | - |
|
| 115 | - return $defaultProperties; |
|
| 116 | - } |
|
| 117 | - |
|
| 118 | - /** |
|
| 119 | - * Returns the list of properties exposed as getters and setters in this class. |
|
| 120 | - * |
|
| 121 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 122 | - */ |
|
| 123 | - public function getExposedProperties() |
|
| 124 | - { |
|
| 125 | - $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 126 | - return $property->getTable()->getName() == $this->table->getName(); |
|
| 127 | - }); |
|
| 128 | - |
|
| 129 | - return $exposedProperties; |
|
| 130 | - } |
|
| 131 | - |
|
| 132 | - /** |
|
| 133 | - * Returns the list of properties for this table (including parent tables). |
|
| 134 | - * |
|
| 135 | - * @param Table $table |
|
| 136 | - * |
|
| 137 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 138 | - */ |
|
| 139 | - private function getProperties(Table $table) |
|
| 140 | - { |
|
| 141 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 142 | - if ($parentRelationship) { |
|
| 143 | - $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
| 144 | - $properties = $this->getProperties($parentTable); |
|
| 145 | - // we merge properties by overriding property names. |
|
| 146 | - $localProperties = $this->getPropertiesForTable($table); |
|
| 147 | - foreach ($localProperties as $name => $property) { |
|
| 148 | - // We do not override properties if this is a primary key! |
|
| 149 | - if ($property->isPrimaryKey()) { |
|
| 150 | - continue; |
|
| 151 | - } |
|
| 152 | - $properties[$name] = $property; |
|
| 153 | - } |
|
| 154 | - } else { |
|
| 155 | - $properties = $this->getPropertiesForTable($table); |
|
| 156 | - } |
|
| 157 | - |
|
| 158 | - return $properties; |
|
| 159 | - } |
|
| 160 | - |
|
| 161 | - /** |
|
| 162 | - * Returns the list of properties for this table (ignoring parent tables). |
|
| 163 | - * |
|
| 164 | - * @param Table $table |
|
| 165 | - * |
|
| 166 | - * @return AbstractBeanPropertyDescriptor[] |
|
| 167 | - */ |
|
| 168 | - private function getPropertiesForTable(Table $table) |
|
| 169 | - { |
|
| 170 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 171 | - if ($parentRelationship) { |
|
| 172 | - $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
| 173 | - } else { |
|
| 174 | - $ignoreColumns = []; |
|
| 175 | - } |
|
| 176 | - |
|
| 177 | - $beanPropertyDescriptors = []; |
|
| 178 | - |
|
| 179 | - foreach ($table->getColumns() as $column) { |
|
| 180 | - if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
| 181 | - continue; |
|
| 182 | - } |
|
| 183 | - |
|
| 184 | - $fk = $this->isPartOfForeignKey($table, $column); |
|
| 185 | - if ($fk !== null) { |
|
| 186 | - // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
| 187 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 188 | - if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
| 189 | - continue 2; |
|
| 190 | - } |
|
| 191 | - } |
|
| 192 | - // Check that this property is not an inheritance relationship |
|
| 193 | - $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 194 | - if ($parentRelationship === $fk) { |
|
| 195 | - continue; |
|
| 196 | - } |
|
| 197 | - |
|
| 198 | - $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
| 199 | - } else { |
|
| 200 | - $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
| 201 | - } |
|
| 202 | - } |
|
| 203 | - |
|
| 204 | - // Now, let's get the name of all properties and let's check there is no duplicate. |
|
| 205 | - /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
| 206 | - $names = []; |
|
| 207 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 208 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 209 | - if (isset($names[$name])) { |
|
| 210 | - $names[$name]->useAlternativeName(); |
|
| 211 | - $beanDescriptor->useAlternativeName(); |
|
| 212 | - } else { |
|
| 213 | - $names[$name] = $beanDescriptor; |
|
| 214 | - } |
|
| 215 | - } |
|
| 216 | - |
|
| 217 | - // Final check (throw exceptions if problem arises) |
|
| 218 | - $names = []; |
|
| 219 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 220 | - $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 221 | - if (isset($names[$name])) { |
|
| 222 | - throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
| 223 | - } else { |
|
| 224 | - $names[$name] = $beanDescriptor; |
|
| 225 | - } |
|
| 226 | - } |
|
| 227 | - |
|
| 228 | - // Last step, let's rebuild the list with a map: |
|
| 229 | - $beanPropertyDescriptorsMap = []; |
|
| 230 | - foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 231 | - $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
| 232 | - } |
|
| 233 | - |
|
| 234 | - return $beanPropertyDescriptorsMap; |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - public function generateBeanConstructor() |
|
| 238 | - { |
|
| 239 | - $constructorProperties = $this->getConstructorProperties(); |
|
| 240 | - |
|
| 241 | - $constructorCode = ' /** |
|
| 20 | + /** |
|
| 21 | + * @var Table |
|
| 22 | + */ |
|
| 23 | + private $table; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * @var SchemaAnalyzer |
|
| 27 | + */ |
|
| 28 | + private $schemaAnalyzer; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @var Schema |
|
| 32 | + */ |
|
| 33 | + private $schema; |
|
| 34 | + |
|
| 35 | + /** |
|
| 36 | + * @var AbstractBeanPropertyDescriptor[] |
|
| 37 | + */ |
|
| 38 | + private $beanPropertyDescriptors = []; |
|
| 39 | + |
|
| 40 | + /** |
|
| 41 | + * @var TDBMSchemaAnalyzer |
|
| 42 | + */ |
|
| 43 | + private $tdbmSchemaAnalyzer; |
|
| 44 | + |
|
| 45 | + public function __construct(Table $table, SchemaAnalyzer $schemaAnalyzer, Schema $schema, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 46 | + { |
|
| 47 | + $this->table = $table; |
|
| 48 | + $this->schemaAnalyzer = $schemaAnalyzer; |
|
| 49 | + $this->schema = $schema; |
|
| 50 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 51 | + $this->initBeanPropertyDescriptors(); |
|
| 52 | + } |
|
| 53 | + |
|
| 54 | + private function initBeanPropertyDescriptors() |
|
| 55 | + { |
|
| 56 | + $this->beanPropertyDescriptors = $this->getProperties($this->table); |
|
| 57 | + } |
|
| 58 | + |
|
| 59 | + /** |
|
| 60 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 61 | + * |
|
| 62 | + * @param Table $table |
|
| 63 | + * @param Column $column |
|
| 64 | + * |
|
| 65 | + * @return ForeignKeyConstraint|null |
|
| 66 | + */ |
|
| 67 | + private function isPartOfForeignKey(Table $table, Column $column) |
|
| 68 | + { |
|
| 69 | + $localColumnName = $column->getName(); |
|
| 70 | + foreach ($table->getForeignKeys() as $foreignKey) { |
|
| 71 | + foreach ($foreignKey->getColumns() as $columnName) { |
|
| 72 | + if ($columnName === $localColumnName) { |
|
| 73 | + return $foreignKey; |
|
| 74 | + } |
|
| 75 | + } |
|
| 76 | + } |
|
| 77 | + |
|
| 78 | + return; |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 83 | + */ |
|
| 84 | + public function getBeanPropertyDescriptors() |
|
| 85 | + { |
|
| 86 | + return $this->beanPropertyDescriptors; |
|
| 87 | + } |
|
| 88 | + |
|
| 89 | + /** |
|
| 90 | + * Returns the list of columns that are not nullable and not autogenerated for a given table and its parent. |
|
| 91 | + * |
|
| 92 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 93 | + */ |
|
| 94 | + public function getConstructorProperties() |
|
| 95 | + { |
|
| 96 | + $constructorProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 97 | + return $property->isCompulsory(); |
|
| 98 | + }); |
|
| 99 | + |
|
| 100 | + return $constructorProperties; |
|
| 101 | + } |
|
| 102 | + |
|
| 103 | + /** |
|
| 104 | + * Returns the list of columns that have default values for a given table. |
|
| 105 | + * |
|
| 106 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 107 | + */ |
|
| 108 | + public function getPropertiesWithDefault() |
|
| 109 | + { |
|
| 110 | + $properties = $this->getPropertiesForTable($this->table); |
|
| 111 | + $defaultProperties = array_filter($properties, function (AbstractBeanPropertyDescriptor $property) { |
|
| 112 | + return $property->hasDefault(); |
|
| 113 | + }); |
|
| 114 | + |
|
| 115 | + return $defaultProperties; |
|
| 116 | + } |
|
| 117 | + |
|
| 118 | + /** |
|
| 119 | + * Returns the list of properties exposed as getters and setters in this class. |
|
| 120 | + * |
|
| 121 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 122 | + */ |
|
| 123 | + public function getExposedProperties() |
|
| 124 | + { |
|
| 125 | + $exposedProperties = array_filter($this->beanPropertyDescriptors, function (AbstractBeanPropertyDescriptor $property) { |
|
| 126 | + return $property->getTable()->getName() == $this->table->getName(); |
|
| 127 | + }); |
|
| 128 | + |
|
| 129 | + return $exposedProperties; |
|
| 130 | + } |
|
| 131 | + |
|
| 132 | + /** |
|
| 133 | + * Returns the list of properties for this table (including parent tables). |
|
| 134 | + * |
|
| 135 | + * @param Table $table |
|
| 136 | + * |
|
| 137 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 138 | + */ |
|
| 139 | + private function getProperties(Table $table) |
|
| 140 | + { |
|
| 141 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 142 | + if ($parentRelationship) { |
|
| 143 | + $parentTable = $this->schema->getTable($parentRelationship->getForeignTableName()); |
|
| 144 | + $properties = $this->getProperties($parentTable); |
|
| 145 | + // we merge properties by overriding property names. |
|
| 146 | + $localProperties = $this->getPropertiesForTable($table); |
|
| 147 | + foreach ($localProperties as $name => $property) { |
|
| 148 | + // We do not override properties if this is a primary key! |
|
| 149 | + if ($property->isPrimaryKey()) { |
|
| 150 | + continue; |
|
| 151 | + } |
|
| 152 | + $properties[$name] = $property; |
|
| 153 | + } |
|
| 154 | + } else { |
|
| 155 | + $properties = $this->getPropertiesForTable($table); |
|
| 156 | + } |
|
| 157 | + |
|
| 158 | + return $properties; |
|
| 159 | + } |
|
| 160 | + |
|
| 161 | + /** |
|
| 162 | + * Returns the list of properties for this table (ignoring parent tables). |
|
| 163 | + * |
|
| 164 | + * @param Table $table |
|
| 165 | + * |
|
| 166 | + * @return AbstractBeanPropertyDescriptor[] |
|
| 167 | + */ |
|
| 168 | + private function getPropertiesForTable(Table $table) |
|
| 169 | + { |
|
| 170 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 171 | + if ($parentRelationship) { |
|
| 172 | + $ignoreColumns = $parentRelationship->getLocalColumns(); |
|
| 173 | + } else { |
|
| 174 | + $ignoreColumns = []; |
|
| 175 | + } |
|
| 176 | + |
|
| 177 | + $beanPropertyDescriptors = []; |
|
| 178 | + |
|
| 179 | + foreach ($table->getColumns() as $column) { |
|
| 180 | + if (array_search($column->getName(), $ignoreColumns) !== false) { |
|
| 181 | + continue; |
|
| 182 | + } |
|
| 183 | + |
|
| 184 | + $fk = $this->isPartOfForeignKey($table, $column); |
|
| 185 | + if ($fk !== null) { |
|
| 186 | + // Check that previously added descriptors are not added on same FK (can happen with multi key FK). |
|
| 187 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 188 | + if ($beanDescriptor instanceof ObjectBeanPropertyDescriptor && $beanDescriptor->getForeignKey() === $fk) { |
|
| 189 | + continue 2; |
|
| 190 | + } |
|
| 191 | + } |
|
| 192 | + // Check that this property is not an inheritance relationship |
|
| 193 | + $parentRelationship = $this->schemaAnalyzer->getParentRelationship($table->getName()); |
|
| 194 | + if ($parentRelationship === $fk) { |
|
| 195 | + continue; |
|
| 196 | + } |
|
| 197 | + |
|
| 198 | + $beanPropertyDescriptors[] = new ObjectBeanPropertyDescriptor($table, $fk, $this->schemaAnalyzer); |
|
| 199 | + } else { |
|
| 200 | + $beanPropertyDescriptors[] = new ScalarBeanPropertyDescriptor($table, $column); |
|
| 201 | + } |
|
| 202 | + } |
|
| 203 | + |
|
| 204 | + // Now, let's get the name of all properties and let's check there is no duplicate. |
|
| 205 | + /** @var $names AbstractBeanPropertyDescriptor[] */ |
|
| 206 | + $names = []; |
|
| 207 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 208 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 209 | + if (isset($names[$name])) { |
|
| 210 | + $names[$name]->useAlternativeName(); |
|
| 211 | + $beanDescriptor->useAlternativeName(); |
|
| 212 | + } else { |
|
| 213 | + $names[$name] = $beanDescriptor; |
|
| 214 | + } |
|
| 215 | + } |
|
| 216 | + |
|
| 217 | + // Final check (throw exceptions if problem arises) |
|
| 218 | + $names = []; |
|
| 219 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 220 | + $name = $beanDescriptor->getUpperCamelCaseName(); |
|
| 221 | + if (isset($names[$name])) { |
|
| 222 | + throw new TDBMException('Unsolvable name conflict while generating method name'); |
|
| 223 | + } else { |
|
| 224 | + $names[$name] = $beanDescriptor; |
|
| 225 | + } |
|
| 226 | + } |
|
| 227 | + |
|
| 228 | + // Last step, let's rebuild the list with a map: |
|
| 229 | + $beanPropertyDescriptorsMap = []; |
|
| 230 | + foreach ($beanPropertyDescriptors as $beanDescriptor) { |
|
| 231 | + $beanPropertyDescriptorsMap[$beanDescriptor->getLowerCamelCaseName()] = $beanDescriptor; |
|
| 232 | + } |
|
| 233 | + |
|
| 234 | + return $beanPropertyDescriptorsMap; |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + public function generateBeanConstructor() |
|
| 238 | + { |
|
| 239 | + $constructorProperties = $this->getConstructorProperties(); |
|
| 240 | + |
|
| 241 | + $constructorCode = ' /** |
|
| 242 | 242 | * The constructor takes all compulsory arguments. |
| 243 | 243 | * |
| 244 | 244 | %s |
@@ -249,69 +249,69 @@ discard block |
||
| 249 | 249 | } |
| 250 | 250 | '; |
| 251 | 251 | |
| 252 | - $paramAnnotations = []; |
|
| 253 | - $arguments = []; |
|
| 254 | - $assigns = []; |
|
| 255 | - $parentConstructorArguments = []; |
|
| 256 | - |
|
| 257 | - foreach ($constructorProperties as $property) { |
|
| 258 | - $className = $property->getClassName(); |
|
| 259 | - if ($className) { |
|
| 260 | - $arguments[] = $className.' '.$property->getVariableName(); |
|
| 261 | - } else { |
|
| 262 | - $arguments[] = $property->getVariableName(); |
|
| 263 | - } |
|
| 264 | - $paramAnnotations[] = $property->getParamAnnotation(); |
|
| 265 | - if ($property->getTable()->getName() === $this->table->getName()) { |
|
| 266 | - $assigns[] = $property->getConstructorAssignCode(); |
|
| 267 | - } else { |
|
| 268 | - $parentConstructorArguments[] = $property->getVariableName(); |
|
| 269 | - } |
|
| 270 | - } |
|
| 271 | - |
|
| 272 | - $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
| 273 | - |
|
| 274 | - foreach ($this->getPropertiesWithDefault() as $property) { |
|
| 275 | - $assigns[] = $property->assignToDefaultCode(); |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns)); |
|
| 279 | - } |
|
| 280 | - |
|
| 281 | - public function generateDirectForeignKeysCode() |
|
| 282 | - { |
|
| 283 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 284 | - |
|
| 285 | - $fksByTable = []; |
|
| 286 | - |
|
| 287 | - foreach ($fks as $fk) { |
|
| 288 | - $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
| 289 | - } |
|
| 290 | - |
|
| 291 | - /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
| 292 | - $fksByMethodName = []; |
|
| 293 | - |
|
| 294 | - foreach ($fksByTable as $tableName => $fksForTable) { |
|
| 295 | - if (count($fksForTable) > 1) { |
|
| 296 | - foreach ($fksForTable as $fk) { |
|
| 297 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
| 298 | - |
|
| 299 | - $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
| 300 | - |
|
| 301 | - $methodName .= implode('And', $camelizedColumns); |
|
| 302 | - |
|
| 303 | - $fksByMethodName[$methodName] = $fk; |
|
| 304 | - } |
|
| 305 | - } else { |
|
| 306 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
| 307 | - $fksByMethodName[$methodName] = $fksForTable[0]; |
|
| 308 | - } |
|
| 309 | - } |
|
| 310 | - |
|
| 311 | - $code = ''; |
|
| 312 | - |
|
| 313 | - foreach ($fksByMethodName as $methodName => $fk) { |
|
| 314 | - $getterCode = ' /** |
|
| 252 | + $paramAnnotations = []; |
|
| 253 | + $arguments = []; |
|
| 254 | + $assigns = []; |
|
| 255 | + $parentConstructorArguments = []; |
|
| 256 | + |
|
| 257 | + foreach ($constructorProperties as $property) { |
|
| 258 | + $className = $property->getClassName(); |
|
| 259 | + if ($className) { |
|
| 260 | + $arguments[] = $className.' '.$property->getVariableName(); |
|
| 261 | + } else { |
|
| 262 | + $arguments[] = $property->getVariableName(); |
|
| 263 | + } |
|
| 264 | + $paramAnnotations[] = $property->getParamAnnotation(); |
|
| 265 | + if ($property->getTable()->getName() === $this->table->getName()) { |
|
| 266 | + $assigns[] = $property->getConstructorAssignCode(); |
|
| 267 | + } else { |
|
| 268 | + $parentConstructorArguments[] = $property->getVariableName(); |
|
| 269 | + } |
|
| 270 | + } |
|
| 271 | + |
|
| 272 | + $parentConstructorCode = sprintf(" parent::__construct(%s);\n", implode(', ', $parentConstructorArguments)); |
|
| 273 | + |
|
| 274 | + foreach ($this->getPropertiesWithDefault() as $property) { |
|
| 275 | + $assigns[] = $property->assignToDefaultCode(); |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + return sprintf($constructorCode, implode("\n", $paramAnnotations), implode(', ', $arguments), $parentConstructorCode, implode("\n", $assigns)); |
|
| 279 | + } |
|
| 280 | + |
|
| 281 | + public function generateDirectForeignKeysCode() |
|
| 282 | + { |
|
| 283 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 284 | + |
|
| 285 | + $fksByTable = []; |
|
| 286 | + |
|
| 287 | + foreach ($fks as $fk) { |
|
| 288 | + $fksByTable[$fk->getLocalTableName()][] = $fk; |
|
| 289 | + } |
|
| 290 | + |
|
| 291 | + /* @var $fksByMethodName ForeignKeyConstraint[] */ |
|
| 292 | + $fksByMethodName = []; |
|
| 293 | + |
|
| 294 | + foreach ($fksByTable as $tableName => $fksForTable) { |
|
| 295 | + if (count($fksForTable) > 1) { |
|
| 296 | + foreach ($fksForTable as $fk) { |
|
| 297 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fk->getLocalTableName()).'By'; |
|
| 298 | + |
|
| 299 | + $camelizedColumns = array_map(['Mouf\\Database\\TDBM\\Utils\\TDBMDaoGenerator', 'toCamelCase'], $fk->getLocalColumns()); |
|
| 300 | + |
|
| 301 | + $methodName .= implode('And', $camelizedColumns); |
|
| 302 | + |
|
| 303 | + $fksByMethodName[$methodName] = $fk; |
|
| 304 | + } |
|
| 305 | + } else { |
|
| 306 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($fksForTable[0]->getLocalTableName()); |
|
| 307 | + $fksByMethodName[$methodName] = $fksForTable[0]; |
|
| 308 | + } |
|
| 309 | + } |
|
| 310 | + |
|
| 311 | + $code = ''; |
|
| 312 | + |
|
| 313 | + foreach ($fksByMethodName as $methodName => $fk) { |
|
| 314 | + $getterCode = ' /** |
|
| 315 | 315 | * Returns the list of %s pointing to this bean via the %s column. |
| 316 | 316 | * |
| 317 | 317 | * @return %s[]|AlterableResultIterator |
@@ -323,107 +323,107 @@ discard block |
||
| 323 | 323 | |
| 324 | 324 | '; |
| 325 | 325 | |
| 326 | - $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 327 | - $code .= sprintf($getterCode, |
|
| 328 | - $beanClass, |
|
| 329 | - implode(', ', $fk->getColumns()), |
|
| 330 | - $beanClass, |
|
| 331 | - $methodName, |
|
| 332 | - var_export($fk->getLocalTableName(), true), |
|
| 333 | - var_export($fk->getName(), true), |
|
| 334 | - var_export($fk->getLocalTableName(), true), |
|
| 335 | - $this->getFilters($fk) |
|
| 336 | - ); |
|
| 337 | - } |
|
| 338 | - |
|
| 339 | - return $code; |
|
| 340 | - } |
|
| 341 | - |
|
| 342 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 343 | - { |
|
| 344 | - $counter = 0; |
|
| 345 | - $parameters = []; |
|
| 346 | - |
|
| 347 | - $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
| 348 | - |
|
| 349 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
| 350 | - $pkColumn = $pkColumns[$counter]; |
|
| 351 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
| 352 | - ++$counter; |
|
| 353 | - } |
|
| 354 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 355 | - |
|
| 356 | - return $parametersCode; |
|
| 357 | - } |
|
| 358 | - |
|
| 359 | - /** |
|
| 360 | - * Generate code section about pivot tables. |
|
| 361 | - * |
|
| 362 | - * @return string |
|
| 363 | - */ |
|
| 364 | - public function generatePivotTableCode() |
|
| 365 | - { |
|
| 366 | - $finalDescs = $this->getPivotTableDescriptors(); |
|
| 367 | - |
|
| 368 | - $code = ''; |
|
| 369 | - |
|
| 370 | - foreach ($finalDescs as $desc) { |
|
| 371 | - $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
| 372 | - } |
|
| 373 | - |
|
| 374 | - return $code; |
|
| 375 | - } |
|
| 376 | - |
|
| 377 | - private function getPivotTableDescriptors() |
|
| 378 | - { |
|
| 379 | - $descs = []; |
|
| 380 | - foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
| 381 | - // There are exactly 2 FKs since this is a pivot table. |
|
| 382 | - $fks = array_values($table->getForeignKeys()); |
|
| 383 | - |
|
| 384 | - if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
| 385 | - $localFK = $fks[0]; |
|
| 386 | - $remoteFK = $fks[1]; |
|
| 387 | - } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
| 388 | - $localFK = $fks[1]; |
|
| 389 | - $remoteFK = $fks[0]; |
|
| 390 | - } else { |
|
| 391 | - continue; |
|
| 392 | - } |
|
| 393 | - |
|
| 394 | - $descs[$remoteFK->getForeignTableName()][] = [ |
|
| 395 | - 'table' => $table, |
|
| 396 | - 'localFK' => $localFK, |
|
| 397 | - 'remoteFK' => $remoteFK, |
|
| 398 | - ]; |
|
| 399 | - } |
|
| 400 | - |
|
| 401 | - $finalDescs = []; |
|
| 402 | - foreach ($descs as $descArray) { |
|
| 403 | - if (count($descArray) > 1) { |
|
| 404 | - foreach ($descArray as $desc) { |
|
| 405 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
| 406 | - $finalDescs[] = $desc; |
|
| 407 | - } |
|
| 408 | - } else { |
|
| 409 | - $desc = $descArray[0]; |
|
| 410 | - $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
| 411 | - $finalDescs[] = $desc; |
|
| 412 | - } |
|
| 413 | - } |
|
| 414 | - |
|
| 415 | - return $finalDescs; |
|
| 416 | - } |
|
| 417 | - |
|
| 418 | - public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
| 419 | - { |
|
| 420 | - $singularName = TDBMDaoGenerator::toSingular($name); |
|
| 421 | - $pluralName = $name; |
|
| 422 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 423 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 424 | - $pluralVariableName = $variableName.'s'; |
|
| 425 | - |
|
| 426 | - $str = ' /** |
|
| 326 | + $beanClass = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 327 | + $code .= sprintf($getterCode, |
|
| 328 | + $beanClass, |
|
| 329 | + implode(', ', $fk->getColumns()), |
|
| 330 | + $beanClass, |
|
| 331 | + $methodName, |
|
| 332 | + var_export($fk->getLocalTableName(), true), |
|
| 333 | + var_export($fk->getName(), true), |
|
| 334 | + var_export($fk->getLocalTableName(), true), |
|
| 335 | + $this->getFilters($fk) |
|
| 336 | + ); |
|
| 337 | + } |
|
| 338 | + |
|
| 339 | + return $code; |
|
| 340 | + } |
|
| 341 | + |
|
| 342 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 343 | + { |
|
| 344 | + $counter = 0; |
|
| 345 | + $parameters = []; |
|
| 346 | + |
|
| 347 | + $pkColumns = $this->table->getPrimaryKeyColumns(); |
|
| 348 | + |
|
| 349 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
| 350 | + $pkColumn = $pkColumns[$counter]; |
|
| 351 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->table->getName(), true)); |
|
| 352 | + ++$counter; |
|
| 353 | + } |
|
| 354 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 355 | + |
|
| 356 | + return $parametersCode; |
|
| 357 | + } |
|
| 358 | + |
|
| 359 | + /** |
|
| 360 | + * Generate code section about pivot tables. |
|
| 361 | + * |
|
| 362 | + * @return string |
|
| 363 | + */ |
|
| 364 | + public function generatePivotTableCode() |
|
| 365 | + { |
|
| 366 | + $finalDescs = $this->getPivotTableDescriptors(); |
|
| 367 | + |
|
| 368 | + $code = ''; |
|
| 369 | + |
|
| 370 | + foreach ($finalDescs as $desc) { |
|
| 371 | + $code .= $this->getPivotTableCode($desc['name'], $desc['table'], $desc['localFK'], $desc['remoteFK']); |
|
| 372 | + } |
|
| 373 | + |
|
| 374 | + return $code; |
|
| 375 | + } |
|
| 376 | + |
|
| 377 | + private function getPivotTableDescriptors() |
|
| 378 | + { |
|
| 379 | + $descs = []; |
|
| 380 | + foreach ($this->schemaAnalyzer->detectJunctionTables(true) as $table) { |
|
| 381 | + // There are exactly 2 FKs since this is a pivot table. |
|
| 382 | + $fks = array_values($table->getForeignKeys()); |
|
| 383 | + |
|
| 384 | + if ($fks[0]->getForeignTableName() === $this->table->getName()) { |
|
| 385 | + $localFK = $fks[0]; |
|
| 386 | + $remoteFK = $fks[1]; |
|
| 387 | + } elseif ($fks[1]->getForeignTableName() === $this->table->getName()) { |
|
| 388 | + $localFK = $fks[1]; |
|
| 389 | + $remoteFK = $fks[0]; |
|
| 390 | + } else { |
|
| 391 | + continue; |
|
| 392 | + } |
|
| 393 | + |
|
| 394 | + $descs[$remoteFK->getForeignTableName()][] = [ |
|
| 395 | + 'table' => $table, |
|
| 396 | + 'localFK' => $localFK, |
|
| 397 | + 'remoteFK' => $remoteFK, |
|
| 398 | + ]; |
|
| 399 | + } |
|
| 400 | + |
|
| 401 | + $finalDescs = []; |
|
| 402 | + foreach ($descs as $descArray) { |
|
| 403 | + if (count($descArray) > 1) { |
|
| 404 | + foreach ($descArray as $desc) { |
|
| 405 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($desc['table']->getName()); |
|
| 406 | + $finalDescs[] = $desc; |
|
| 407 | + } |
|
| 408 | + } else { |
|
| 409 | + $desc = $descArray[0]; |
|
| 410 | + $desc['name'] = TDBMDaoGenerator::toCamelCase($desc['remoteFK']->getForeignTableName()); |
|
| 411 | + $finalDescs[] = $desc; |
|
| 412 | + } |
|
| 413 | + } |
|
| 414 | + |
|
| 415 | + return $finalDescs; |
|
| 416 | + } |
|
| 417 | + |
|
| 418 | + public function getPivotTableCode($name, Table $table, ForeignKeyConstraint $localFK, ForeignKeyConstraint $remoteFK) |
|
| 419 | + { |
|
| 420 | + $singularName = TDBMDaoGenerator::toSingular($name); |
|
| 421 | + $pluralName = $name; |
|
| 422 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 423 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 424 | + $pluralVariableName = $variableName.'s'; |
|
| 425 | + |
|
| 426 | + $str = ' /** |
|
| 427 | 427 | * Returns the list of %s associated to this bean via the %s pivot table. |
| 428 | 428 | * |
| 429 | 429 | * @return %s[] |
@@ -434,9 +434,9 @@ discard block |
||
| 434 | 434 | } |
| 435 | 435 | '; |
| 436 | 436 | |
| 437 | - $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
| 437 | + $getterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $name, var_export($remoteFK->getLocalTableName(), true)); |
|
| 438 | 438 | |
| 439 | - $str = ' /** |
|
| 439 | + $str = ' /** |
|
| 440 | 440 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
| 441 | 441 | * |
| 442 | 442 | * @param %s %s |
@@ -447,9 +447,9 @@ discard block |
||
| 447 | 447 | } |
| 448 | 448 | '; |
| 449 | 449 | |
| 450 | - $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 450 | + $adderCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 451 | 451 | |
| 452 | - $str = ' /** |
|
| 452 | + $str = ' /** |
|
| 453 | 453 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
| 454 | 454 | * |
| 455 | 455 | * @param %s %s |
@@ -460,9 +460,9 @@ discard block |
||
| 460 | 460 | } |
| 461 | 461 | '; |
| 462 | 462 | |
| 463 | - $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 463 | + $removerCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 464 | 464 | |
| 465 | - $str = ' /** |
|
| 465 | + $str = ' /** |
|
| 466 | 466 | * Returns whether this bean is associated with %s via the %s pivot table. |
| 467 | 467 | * |
| 468 | 468 | * @param %s %s |
@@ -474,9 +474,9 @@ discard block |
||
| 474 | 474 | } |
| 475 | 475 | '; |
| 476 | 476 | |
| 477 | - $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 477 | + $hasCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($remoteFK->getLocalTableName(), true), $variableName); |
|
| 478 | 478 | |
| 479 | - $str = ' /** |
|
| 479 | + $str = ' /** |
|
| 480 | 480 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
| 481 | 481 | * Exiting relationships will be removed and replaced by the provided relationships. |
| 482 | 482 | * |
@@ -488,24 +488,24 @@ discard block |
||
| 488 | 488 | } |
| 489 | 489 | '; |
| 490 | 490 | |
| 491 | - $setterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($remoteFK->getLocalTableName(), true), $pluralVariableName); |
|
| 491 | + $setterCode = sprintf($str, $remoteBeanName, $table->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($remoteFK->getLocalTableName(), true), $pluralVariableName); |
|
| 492 | 492 | |
| 493 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 493 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 494 | 494 | |
| 495 | - return $code; |
|
| 496 | - } |
|
| 495 | + return $code; |
|
| 496 | + } |
|
| 497 | 497 | |
| 498 | - public function generateJsonSerialize() |
|
| 499 | - { |
|
| 500 | - $tableName = $this->table->getName(); |
|
| 501 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 502 | - if ($parentFk !== null) { |
|
| 503 | - $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
| 504 | - } else { |
|
| 505 | - $initializer = '$array = [];'; |
|
| 506 | - } |
|
| 498 | + public function generateJsonSerialize() |
|
| 499 | + { |
|
| 500 | + $tableName = $this->table->getName(); |
|
| 501 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 502 | + if ($parentFk !== null) { |
|
| 503 | + $initializer = '$array = parent::jsonSerialize($stopRecursion);'; |
|
| 504 | + } else { |
|
| 505 | + $initializer = '$array = [];'; |
|
| 506 | + } |
|
| 507 | 507 | |
| 508 | - $str = ' |
|
| 508 | + $str = ' |
|
| 509 | 509 | /** |
| 510 | 510 | * Serializes the object for JSON encoding. |
| 511 | 511 | * |
@@ -521,97 +521,97 @@ discard block |
||
| 521 | 521 | } |
| 522 | 522 | '; |
| 523 | 523 | |
| 524 | - $propertiesCode = ''; |
|
| 525 | - foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
| 526 | - $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
| 527 | - } |
|
| 524 | + $propertiesCode = ''; |
|
| 525 | + foreach ($this->beanPropertyDescriptors as $beanPropertyDescriptor) { |
|
| 526 | + $propertiesCode .= $beanPropertyDescriptor->getJsonSerializeCode(); |
|
| 527 | + } |
|
| 528 | 528 | |
| 529 | - // Many to many relationships: |
|
| 529 | + // Many to many relationships: |
|
| 530 | 530 | |
| 531 | - $descs = $this->getPivotTableDescriptors(); |
|
| 531 | + $descs = $this->getPivotTableDescriptors(); |
|
| 532 | 532 | |
| 533 | - $many2manyCode = ''; |
|
| 533 | + $many2manyCode = ''; |
|
| 534 | 534 | |
| 535 | - foreach ($descs as $desc) { |
|
| 536 | - $remoteFK = $desc['remoteFK']; |
|
| 537 | - $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 538 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 535 | + foreach ($descs as $desc) { |
|
| 536 | + $remoteFK = $desc['remoteFK']; |
|
| 537 | + $remoteBeanName = TDBMDaoGenerator::getBeanNameFromTableName($remoteFK->getForeignTableName()); |
|
| 538 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 539 | 539 | |
| 540 | - $many2manyCode .= ' if (!$stopRecursion) { |
|
| 540 | + $many2manyCode .= ' if (!$stopRecursion) { |
|
| 541 | 541 | $array[\''.lcfirst($desc['name']).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
| 542 | 542 | return '.$variableName.'->jsonSerialize(true); |
| 543 | 543 | }, $this->get'.$desc['name'].'()); |
| 544 | 544 | } |
| 545 | 545 | '; |
| 546 | - } |
|
| 547 | - |
|
| 548 | - return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
| 549 | - } |
|
| 550 | - |
|
| 551 | - /** |
|
| 552 | - * Returns as an array the class we need to extend from and the list of use statements. |
|
| 553 | - * |
|
| 554 | - * @return array |
|
| 555 | - */ |
|
| 556 | - private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
| 557 | - { |
|
| 558 | - $classes = []; |
|
| 559 | - if ($parentFk !== null) { |
|
| 560 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 561 | - $classes[] = $extends; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
| 565 | - $className = $beanPropertyDescriptor->getClassName(); |
|
| 566 | - if (null !== $className) { |
|
| 567 | - $classes[] = $beanPropertyDescriptor->getClassName(); |
|
| 568 | - } |
|
| 569 | - } |
|
| 570 | - |
|
| 571 | - foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
| 572 | - /* @var $fk ForeignKeyConstraint */ |
|
| 573 | - $fk = $descriptor['remoteFK']; |
|
| 574 | - $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - // Many-to-one relationships |
|
| 578 | - $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 579 | - foreach ($fks as $fk) { |
|
| 580 | - $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 581 | - } |
|
| 582 | - |
|
| 583 | - $classes = array_unique($classes); |
|
| 584 | - |
|
| 585 | - return $classes; |
|
| 586 | - } |
|
| 587 | - |
|
| 588 | - /** |
|
| 589 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 590 | - * |
|
| 591 | - * @param string $beannamespace The namespace of the bean |
|
| 592 | - */ |
|
| 593 | - public function generatePhpCode($beannamespace) |
|
| 594 | - { |
|
| 595 | - $tableName = $this->table->getName(); |
|
| 596 | - $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
| 597 | - $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
| 598 | - $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 599 | - |
|
| 600 | - $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
| 601 | - |
|
| 602 | - $uses = array_map(function ($className) use ($beannamespace) { |
|
| 603 | - return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
| 604 | - }, $classes); |
|
| 605 | - $use = implode('', $uses); |
|
| 606 | - |
|
| 607 | - if ($parentFk !== null) { |
|
| 608 | - $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 609 | - } else { |
|
| 610 | - $extends = 'AbstractTDBMObject'; |
|
| 611 | - $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
| 612 | - } |
|
| 613 | - |
|
| 614 | - $str = "<?php |
|
| 546 | + } |
|
| 547 | + |
|
| 548 | + return sprintf($str, $initializer, $propertiesCode, $many2manyCode); |
|
| 549 | + } |
|
| 550 | + |
|
| 551 | + /** |
|
| 552 | + * Returns as an array the class we need to extend from and the list of use statements. |
|
| 553 | + * |
|
| 554 | + * @return array |
|
| 555 | + */ |
|
| 556 | + private function generateExtendsAndUseStatements(ForeignKeyConstraint $parentFk = null) |
|
| 557 | + { |
|
| 558 | + $classes = []; |
|
| 559 | + if ($parentFk !== null) { |
|
| 560 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 561 | + $classes[] = $extends; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + foreach ($this->getBeanPropertyDescriptors() as $beanPropertyDescriptor) { |
|
| 565 | + $className = $beanPropertyDescriptor->getClassName(); |
|
| 566 | + if (null !== $className) { |
|
| 567 | + $classes[] = $beanPropertyDescriptor->getClassName(); |
|
| 568 | + } |
|
| 569 | + } |
|
| 570 | + |
|
| 571 | + foreach ($this->getPivotTableDescriptors() as $descriptor) { |
|
| 572 | + /* @var $fk ForeignKeyConstraint */ |
|
| 573 | + $fk = $descriptor['remoteFK']; |
|
| 574 | + $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getForeignTableName()); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + // Many-to-one relationships |
|
| 578 | + $fks = $this->tdbmSchemaAnalyzer->getIncomingForeignKeys($this->table->getName()); |
|
| 579 | + foreach ($fks as $fk) { |
|
| 580 | + $classes[] = TDBMDaoGenerator::getBeanNameFromTableName($fk->getLocalTableName()); |
|
| 581 | + } |
|
| 582 | + |
|
| 583 | + $classes = array_unique($classes); |
|
| 584 | + |
|
| 585 | + return $classes; |
|
| 586 | + } |
|
| 587 | + |
|
| 588 | + /** |
|
| 589 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 590 | + * |
|
| 591 | + * @param string $beannamespace The namespace of the bean |
|
| 592 | + */ |
|
| 593 | + public function generatePhpCode($beannamespace) |
|
| 594 | + { |
|
| 595 | + $tableName = $this->table->getName(); |
|
| 596 | + $baseClassName = TDBMDaoGenerator::getBaseBeanNameFromTableName($tableName); |
|
| 597 | + $className = TDBMDaoGenerator::getBeanNameFromTableName($tableName); |
|
| 598 | + $parentFk = $this->schemaAnalyzer->getParentRelationship($tableName); |
|
| 599 | + |
|
| 600 | + $classes = $this->generateExtendsAndUseStatements($parentFk); |
|
| 601 | + |
|
| 602 | + $uses = array_map(function ($className) use ($beannamespace) { |
|
| 603 | + return 'use '.$beannamespace.'\\'.$className.";\n"; |
|
| 604 | + }, $classes); |
|
| 605 | + $use = implode('', $uses); |
|
| 606 | + |
|
| 607 | + if ($parentFk !== null) { |
|
| 608 | + $extends = TDBMDaoGenerator::getBeanNameFromTableName($parentFk->getForeignTableName()); |
|
| 609 | + } else { |
|
| 610 | + $extends = 'AbstractTDBMObject'; |
|
| 611 | + $use .= "use Mouf\\Database\\TDBM\\AbstractTDBMObject;\n"; |
|
| 612 | + } |
|
| 613 | + |
|
| 614 | + $str = "<?php |
|
| 615 | 615 | namespace {$beannamespace}\\Generated; |
| 616 | 616 | |
| 617 | 617 | use Mouf\\Database\\TDBM\\ResultIterator; |
@@ -631,128 +631,128 @@ discard block |
||
| 631 | 631 | { |
| 632 | 632 | "; |
| 633 | 633 | |
| 634 | - $str .= $this->generateBeanConstructor(); |
|
| 634 | + $str .= $this->generateBeanConstructor(); |
|
| 635 | 635 | |
| 636 | - foreach ($this->getExposedProperties() as $property) { |
|
| 637 | - $str .= $property->getGetterSetterCode(); |
|
| 638 | - } |
|
| 636 | + foreach ($this->getExposedProperties() as $property) { |
|
| 637 | + $str .= $property->getGetterSetterCode(); |
|
| 638 | + } |
|
| 639 | 639 | |
| 640 | - $str .= $this->generateDirectForeignKeysCode(); |
|
| 641 | - $str .= $this->generatePivotTableCode(); |
|
| 642 | - $str .= $this->generateJsonSerialize(); |
|
| 640 | + $str .= $this->generateDirectForeignKeysCode(); |
|
| 641 | + $str .= $this->generatePivotTableCode(); |
|
| 642 | + $str .= $this->generateJsonSerialize(); |
|
| 643 | 643 | |
| 644 | - $str .= $this->generateGetUsedTablesCode(); |
|
| 644 | + $str .= $this->generateGetUsedTablesCode(); |
|
| 645 | 645 | |
| 646 | - $str .= $this->generateOnDeleteCode(); |
|
| 646 | + $str .= $this->generateOnDeleteCode(); |
|
| 647 | 647 | |
| 648 | - $str .= '} |
|
| 648 | + $str .= '} |
|
| 649 | 649 | '; |
| 650 | 650 | |
| 651 | - return $str; |
|
| 652 | - } |
|
| 653 | - |
|
| 654 | - /** |
|
| 655 | - * @param string $beanNamespace |
|
| 656 | - * @param string $beanClassName |
|
| 657 | - * |
|
| 658 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
| 659 | - */ |
|
| 660 | - public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
| 661 | - { |
|
| 662 | - $code = ''; |
|
| 663 | - $usedBeans = []; |
|
| 664 | - foreach ($this->table->getIndexes() as $index) { |
|
| 665 | - if (!$index->isPrimary()) { |
|
| 666 | - list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
| 667 | - $code .= $codeForIndex; |
|
| 668 | - $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
| 669 | - } |
|
| 670 | - } |
|
| 671 | - |
|
| 672 | - return [$usedBeans, $code]; |
|
| 673 | - } |
|
| 674 | - |
|
| 675 | - /** |
|
| 676 | - * @param Index $index |
|
| 677 | - * @param string $beanNamespace |
|
| 678 | - * @param string $beanClassName |
|
| 679 | - * |
|
| 680 | - * @return array first element: list of used beans, second item: PHP code as a string |
|
| 681 | - */ |
|
| 682 | - private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
| 683 | - { |
|
| 684 | - $columns = $index->getColumns(); |
|
| 685 | - $usedBeans = []; |
|
| 686 | - |
|
| 687 | - /* |
|
| 651 | + return $str; |
|
| 652 | + } |
|
| 653 | + |
|
| 654 | + /** |
|
| 655 | + * @param string $beanNamespace |
|
| 656 | + * @param string $beanClassName |
|
| 657 | + * |
|
| 658 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
| 659 | + */ |
|
| 660 | + public function generateFindByDaoCode($beanNamespace, $beanClassName) |
|
| 661 | + { |
|
| 662 | + $code = ''; |
|
| 663 | + $usedBeans = []; |
|
| 664 | + foreach ($this->table->getIndexes() as $index) { |
|
| 665 | + if (!$index->isPrimary()) { |
|
| 666 | + list($usedBeansForIndex, $codeForIndex) = $this->generateFindByDaoCodeForIndex($index, $beanNamespace, $beanClassName); |
|
| 667 | + $code .= $codeForIndex; |
|
| 668 | + $usedBeans = array_merge($usedBeans, $usedBeansForIndex); |
|
| 669 | + } |
|
| 670 | + } |
|
| 671 | + |
|
| 672 | + return [$usedBeans, $code]; |
|
| 673 | + } |
|
| 674 | + |
|
| 675 | + /** |
|
| 676 | + * @param Index $index |
|
| 677 | + * @param string $beanNamespace |
|
| 678 | + * @param string $beanClassName |
|
| 679 | + * |
|
| 680 | + * @return array first element: list of used beans, second item: PHP code as a string |
|
| 681 | + */ |
|
| 682 | + private function generateFindByDaoCodeForIndex(Index $index, $beanNamespace, $beanClassName) |
|
| 683 | + { |
|
| 684 | + $columns = $index->getColumns(); |
|
| 685 | + $usedBeans = []; |
|
| 686 | + |
|
| 687 | + /* |
|
| 688 | 688 | * The list of elements building this index (expressed as columns or foreign keys) |
| 689 | 689 | * @var AbstractBeanPropertyDescriptor[] |
| 690 | 690 | */ |
| 691 | - $elements = []; |
|
| 692 | - |
|
| 693 | - foreach ($columns as $column) { |
|
| 694 | - $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
| 695 | - if ($fk !== null) { |
|
| 696 | - if (!in_array($fk, $elements)) { |
|
| 697 | - $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
| 698 | - } |
|
| 699 | - } else { |
|
| 700 | - $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
| 701 | - } |
|
| 702 | - } |
|
| 703 | - |
|
| 704 | - // If the index is actually only a foreign key, let's bypass it entirely. |
|
| 705 | - if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
| 706 | - return [[], '']; |
|
| 707 | - } |
|
| 708 | - |
|
| 709 | - $methodNameComponent = []; |
|
| 710 | - $functionParameters = []; |
|
| 711 | - $first = true; |
|
| 712 | - foreach ($elements as $element) { |
|
| 713 | - $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
| 714 | - $functionParameter = $element->getClassName(); |
|
| 715 | - if ($functionParameter) { |
|
| 716 | - $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
| 717 | - $functionParameter .= ' '; |
|
| 718 | - } |
|
| 719 | - $functionParameter .= $element->getVariableName(); |
|
| 720 | - if ($first) { |
|
| 721 | - $first = false; |
|
| 722 | - } else { |
|
| 723 | - $functionParameter .= ' = null'; |
|
| 724 | - } |
|
| 725 | - $functionParameters[] = $functionParameter; |
|
| 726 | - } |
|
| 727 | - if ($index->isUnique()) { |
|
| 728 | - $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
| 729 | - $calledMethod = 'findOne'; |
|
| 730 | - $returnType = "{$beanClassName}"; |
|
| 731 | - } else { |
|
| 732 | - $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
| 733 | - $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
| 734 | - $calledMethod = 'find'; |
|
| 735 | - } |
|
| 736 | - $functionParametersString = implode(', ', $functionParameters); |
|
| 737 | - |
|
| 738 | - $count = 0; |
|
| 739 | - |
|
| 740 | - $params = []; |
|
| 741 | - $filterArrayCode = ''; |
|
| 742 | - $commentArguments = []; |
|
| 743 | - foreach ($elements as $element) { |
|
| 744 | - $params[] = $element->getParamAnnotation(); |
|
| 745 | - if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
| 746 | - $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
| 747 | - } else { |
|
| 748 | - ++$count; |
|
| 749 | - $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
| 750 | - } |
|
| 751 | - $commentArguments[] = substr($element->getVariableName(), 1); |
|
| 752 | - } |
|
| 753 | - $paramsString = implode("\n", $params); |
|
| 754 | - |
|
| 755 | - $code = " |
|
| 691 | + $elements = []; |
|
| 692 | + |
|
| 693 | + foreach ($columns as $column) { |
|
| 694 | + $fk = $this->isPartOfForeignKey($this->table, $this->table->getColumn($column)); |
|
| 695 | + if ($fk !== null) { |
|
| 696 | + if (!in_array($fk, $elements)) { |
|
| 697 | + $elements[] = new ObjectBeanPropertyDescriptor($this->table, $fk, $this->schemaAnalyzer); |
|
| 698 | + } |
|
| 699 | + } else { |
|
| 700 | + $elements[] = new ScalarBeanPropertyDescriptor($this->table, $this->table->getColumn($column)); |
|
| 701 | + } |
|
| 702 | + } |
|
| 703 | + |
|
| 704 | + // If the index is actually only a foreign key, let's bypass it entirely. |
|
| 705 | + if (count($elements) === 1 && $elements[0] instanceof ObjectBeanPropertyDescriptor) { |
|
| 706 | + return [[], '']; |
|
| 707 | + } |
|
| 708 | + |
|
| 709 | + $methodNameComponent = []; |
|
| 710 | + $functionParameters = []; |
|
| 711 | + $first = true; |
|
| 712 | + foreach ($elements as $element) { |
|
| 713 | + $methodNameComponent[] = $element->getUpperCamelCaseName(); |
|
| 714 | + $functionParameter = $element->getClassName(); |
|
| 715 | + if ($functionParameter) { |
|
| 716 | + $usedBeans[] = $beanNamespace.'\\'.$functionParameter; |
|
| 717 | + $functionParameter .= ' '; |
|
| 718 | + } |
|
| 719 | + $functionParameter .= $element->getVariableName(); |
|
| 720 | + if ($first) { |
|
| 721 | + $first = false; |
|
| 722 | + } else { |
|
| 723 | + $functionParameter .= ' = null'; |
|
| 724 | + } |
|
| 725 | + $functionParameters[] = $functionParameter; |
|
| 726 | + } |
|
| 727 | + if ($index->isUnique()) { |
|
| 728 | + $methodName = 'findOneBy'.implode('And', $methodNameComponent); |
|
| 729 | + $calledMethod = 'findOne'; |
|
| 730 | + $returnType = "{$beanClassName}"; |
|
| 731 | + } else { |
|
| 732 | + $methodName = 'findBy'.implode('And', $methodNameComponent); |
|
| 733 | + $returnType = "{$beanClassName}[]|ResultIterator|ResultArray"; |
|
| 734 | + $calledMethod = 'find'; |
|
| 735 | + } |
|
| 736 | + $functionParametersString = implode(', ', $functionParameters); |
|
| 737 | + |
|
| 738 | + $count = 0; |
|
| 739 | + |
|
| 740 | + $params = []; |
|
| 741 | + $filterArrayCode = ''; |
|
| 742 | + $commentArguments = []; |
|
| 743 | + foreach ($elements as $element) { |
|
| 744 | + $params[] = $element->getParamAnnotation(); |
|
| 745 | + if ($element instanceof ScalarBeanPropertyDescriptor) { |
|
| 746 | + $filterArrayCode .= ' '.var_export($element->getColumnName(), true).' => '.$element->getVariableName().",\n"; |
|
| 747 | + } else { |
|
| 748 | + ++$count; |
|
| 749 | + $filterArrayCode .= ' '.$count.' => '.$element->getVariableName().",\n"; |
|
| 750 | + } |
|
| 751 | + $commentArguments[] = substr($element->getVariableName(), 1); |
|
| 752 | + } |
|
| 753 | + $paramsString = implode("\n", $params); |
|
| 754 | + |
|
| 755 | + $code = " |
|
| 756 | 756 | /** |
| 757 | 757 | * Get a list of $beanClassName filtered by ".implode(', ', $commentArguments).". |
| 758 | 758 | * |
@@ -770,27 +770,27 @@ discard block |
||
| 770 | 770 | } |
| 771 | 771 | "; |
| 772 | 772 | |
| 773 | - return [$usedBeans, $code]; |
|
| 774 | - } |
|
| 775 | - |
|
| 776 | - /** |
|
| 777 | - * Generates the code for the getUsedTable protected method. |
|
| 778 | - * |
|
| 779 | - * @return string |
|
| 780 | - */ |
|
| 781 | - private function generateGetUsedTablesCode() |
|
| 782 | - { |
|
| 783 | - $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
| 784 | - if ($hasParentRelationship) { |
|
| 785 | - $code = sprintf(' $tables = parent::getUsedTables(); |
|
| 773 | + return [$usedBeans, $code]; |
|
| 774 | + } |
|
| 775 | + |
|
| 776 | + /** |
|
| 777 | + * Generates the code for the getUsedTable protected method. |
|
| 778 | + * |
|
| 779 | + * @return string |
|
| 780 | + */ |
|
| 781 | + private function generateGetUsedTablesCode() |
|
| 782 | + { |
|
| 783 | + $hasParentRelationship = $this->schemaAnalyzer->getParentRelationship($this->table->getName()) !== null; |
|
| 784 | + if ($hasParentRelationship) { |
|
| 785 | + $code = sprintf(' $tables = parent::getUsedTables(); |
|
| 786 | 786 | $tables[] = %s; |
| 787 | 787 | |
| 788 | 788 | return $tables;', var_export($this->table->getName(), true)); |
| 789 | - } else { |
|
| 790 | - $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
| 791 | - } |
|
| 789 | + } else { |
|
| 790 | + $code = sprintf(' return [ %s ];', var_export($this->table->getName(), true)); |
|
| 791 | + } |
|
| 792 | 792 | |
| 793 | - return sprintf(' |
|
| 793 | + return sprintf(' |
|
| 794 | 794 | /** |
| 795 | 795 | * Returns an array of used tables by this bean (from parent to child relationship). |
| 796 | 796 | * |
@@ -801,20 +801,20 @@ discard block |
||
| 801 | 801 | %s |
| 802 | 802 | } |
| 803 | 803 | ', $code); |
| 804 | - } |
|
| 805 | - |
|
| 806 | - private function generateOnDeleteCode() |
|
| 807 | - { |
|
| 808 | - $code = ''; |
|
| 809 | - $relationships = $this->getPropertiesForTable($this->table); |
|
| 810 | - foreach ($relationships as $relationship) { |
|
| 811 | - if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
| 812 | - $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).');'); |
|
| 813 | - } |
|
| 814 | - } |
|
| 815 | - |
|
| 816 | - if ($code) { |
|
| 817 | - return sprintf(' |
|
| 804 | + } |
|
| 805 | + |
|
| 806 | + private function generateOnDeleteCode() |
|
| 807 | + { |
|
| 808 | + $code = ''; |
|
| 809 | + $relationships = $this->getPropertiesForTable($this->table); |
|
| 810 | + foreach ($relationships as $relationship) { |
|
| 811 | + if ($relationship instanceof ObjectBeanPropertyDescriptor) { |
|
| 812 | + $code .= sprintf(' $this->setRef('.var_export($relationship->getForeignKey()->getName(), true).', null, '.var_export($this->table->getName(), true).');'); |
|
| 813 | + } |
|
| 814 | + } |
|
| 815 | + |
|
| 816 | + if ($code) { |
|
| 817 | + return sprintf(' |
|
| 818 | 818 | /** |
| 819 | 819 | * Method called when the bean is removed from database. |
| 820 | 820 | * |
@@ -825,8 +825,8 @@ discard block |
||
| 825 | 825 | %s |
| 826 | 826 | } |
| 827 | 827 | ', $code); |
| 828 | - } |
|
| 828 | + } |
|
| 829 | 829 | |
| 830 | - return ''; |
|
| 831 | - } |
|
| 830 | + return ''; |
|
| 831 | + } |
|
| 832 | 832 | } |