@@ -27,11 +27,11 @@ |
||
| 27 | 27 | */ |
| 28 | 28 | final class TDBMObjectStateEnum |
| 29 | 29 | { |
| 30 | - const STATE_DETACHED = 'detached'; |
|
| 31 | - const STATE_NEW = 'new'; |
|
| 32 | - const STATE_SAVING = 'saving'; |
|
| 33 | - const STATE_NOT_LOADED = 'not loaded'; |
|
| 34 | - const STATE_LOADED = 'loaded'; |
|
| 35 | - const STATE_DIRTY = 'dirty'; |
|
| 36 | - const STATE_DELETED = 'deleted'; |
|
| 30 | + const STATE_DETACHED = 'detached'; |
|
| 31 | + const STATE_NEW = 'new'; |
|
| 32 | + const STATE_SAVING = 'saving'; |
|
| 33 | + const STATE_NOT_LOADED = 'not loaded'; |
|
| 34 | + const STATE_LOADED = 'loaded'; |
|
| 35 | + const STATE_DIRTY = 'dirty'; |
|
| 36 | + const STATE_DELETED = 'deleted'; |
|
| 37 | 37 | } |
@@ -197,7 +197,7 @@ discard block |
||
| 197 | 197 | private function checkTableName($tableName = null) |
| 198 | 198 | { |
| 199 | 199 | if ($tableName === null) { |
| 200 | - if (count($this->dbRows) > 1) { |
|
| 200 | + if (count($this->dbRows)>1) { |
|
| 201 | 201 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 202 | 202 | } elseif (count($this->dbRows) === 1) { |
| 203 | 203 | $tableName = array_keys($this->dbRows)[0]; |
@@ -221,7 +221,7 @@ discard block |
||
| 221 | 221 | protected function set($var, $value, $tableName = null) |
| 222 | 222 | { |
| 223 | 223 | if ($tableName === null) { |
| 224 | - if (count($this->dbRows) > 1) { |
|
| 224 | + if (count($this->dbRows)>1) { |
|
| 225 | 225 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 226 | 226 | } elseif (count($this->dbRows) === 1) { |
| 227 | 227 | $tableName = array_keys($this->dbRows)[0]; |
@@ -247,7 +247,7 @@ discard block |
||
| 247 | 247 | protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
| 248 | 248 | { |
| 249 | 249 | if ($tableName === null) { |
| 250 | - if (count($this->dbRows) > 1) { |
|
| 250 | + if (count($this->dbRows)>1) { |
|
| 251 | 251 | throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
| 252 | 252 | } elseif (count($this->dbRows) === 1) { |
| 253 | 253 | $tableName = array_keys($this->dbRows)[0]; |
@@ -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 = []; |
|
| 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 = [], 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 | - return $tableName; |
|
| 208 | - } |
|
| 209 | - |
|
| 210 | - protected function get($var, $tableName = null) |
|
| 211 | - { |
|
| 212 | - $tableName = $this->checkTableName($tableName); |
|
| 213 | - |
|
| 214 | - if (!isset($this->dbRows[$tableName])) { |
|
| 215 | - return; |
|
| 216 | - } |
|
| 217 | - |
|
| 218 | - return $this->dbRows[$tableName]->get($var); |
|
| 219 | - } |
|
| 220 | - |
|
| 221 | - protected function set($var, $value, $tableName = null) |
|
| 222 | - { |
|
| 223 | - if ($tableName === null) { |
|
| 224 | - if (count($this->dbRows) > 1) { |
|
| 225 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 226 | - } elseif (count($this->dbRows) === 1) { |
|
| 227 | - $tableName = array_keys($this->dbRows)[0]; |
|
| 228 | - } else { |
|
| 229 | - throw new TDBMException('Please specify a table for this object.'); |
|
| 230 | - } |
|
| 231 | - } |
|
| 232 | - |
|
| 233 | - if (!isset($this->dbRows[$tableName])) { |
|
| 234 | - $this->registerTable($tableName); |
|
| 235 | - } |
|
| 236 | - |
|
| 237 | - $this->dbRows[$tableName]->set($var, $value); |
|
| 238 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 239 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 240 | - } |
|
| 241 | - } |
|
| 242 | - |
|
| 243 | - /** |
|
| 244 | - * @param string $foreignKeyName |
|
| 245 | - * @param AbstractTDBMObject $bean |
|
| 246 | - */ |
|
| 247 | - protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
| 248 | - { |
|
| 249 | - if ($tableName === null) { |
|
| 250 | - if (count($this->dbRows) > 1) { |
|
| 251 | - throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 252 | - } elseif (count($this->dbRows) === 1) { |
|
| 253 | - $tableName = array_keys($this->dbRows)[0]; |
|
| 254 | - } else { |
|
| 255 | - throw new TDBMException('Please specify a table for this object.'); |
|
| 256 | - } |
|
| 257 | - } |
|
| 258 | - |
|
| 259 | - if (!isset($this->dbRows[$tableName])) { |
|
| 260 | - $this->registerTable($tableName); |
|
| 261 | - } |
|
| 262 | - |
|
| 263 | - $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 264 | - if ($oldLinkedBean !== null) { |
|
| 265 | - $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 266 | - } |
|
| 267 | - |
|
| 268 | - $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
| 269 | - if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 270 | - $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 271 | - } |
|
| 272 | - |
|
| 273 | - if ($bean !== null) { |
|
| 274 | - $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 275 | - } |
|
| 276 | - } |
|
| 277 | - |
|
| 278 | - /** |
|
| 279 | - * @param string $foreignKeyName A unique name for this reference |
|
| 280 | - * |
|
| 281 | - * @return AbstractTDBMObject|null |
|
| 282 | - */ |
|
| 283 | - protected function getRef($foreignKeyName, $tableName = null) |
|
| 284 | - { |
|
| 285 | - $tableName = $this->checkTableName($tableName); |
|
| 286 | - |
|
| 287 | - if (!isset($this->dbRows[$tableName])) { |
|
| 288 | - return; |
|
| 289 | - } |
|
| 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 = []; |
|
| 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 = [], 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 | + return $tableName; |
|
| 208 | + } |
|
| 209 | + |
|
| 210 | + protected function get($var, $tableName = null) |
|
| 211 | + { |
|
| 212 | + $tableName = $this->checkTableName($tableName); |
|
| 213 | + |
|
| 214 | + if (!isset($this->dbRows[$tableName])) { |
|
| 215 | + return; |
|
| 216 | + } |
|
| 217 | + |
|
| 218 | + return $this->dbRows[$tableName]->get($var); |
|
| 219 | + } |
|
| 220 | + |
|
| 221 | + protected function set($var, $value, $tableName = null) |
|
| 222 | + { |
|
| 223 | + if ($tableName === null) { |
|
| 224 | + if (count($this->dbRows) > 1) { |
|
| 225 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 226 | + } elseif (count($this->dbRows) === 1) { |
|
| 227 | + $tableName = array_keys($this->dbRows)[0]; |
|
| 228 | + } else { |
|
| 229 | + throw new TDBMException('Please specify a table for this object.'); |
|
| 230 | + } |
|
| 231 | + } |
|
| 232 | + |
|
| 233 | + if (!isset($this->dbRows[$tableName])) { |
|
| 234 | + $this->registerTable($tableName); |
|
| 235 | + } |
|
| 236 | + |
|
| 237 | + $this->dbRows[$tableName]->set($var, $value); |
|
| 238 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 239 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 240 | + } |
|
| 241 | + } |
|
| 242 | + |
|
| 243 | + /** |
|
| 244 | + * @param string $foreignKeyName |
|
| 245 | + * @param AbstractTDBMObject $bean |
|
| 246 | + */ |
|
| 247 | + protected function setRef($foreignKeyName, AbstractTDBMObject $bean = null, $tableName = null) |
|
| 248 | + { |
|
| 249 | + if ($tableName === null) { |
|
| 250 | + if (count($this->dbRows) > 1) { |
|
| 251 | + throw new TDBMException('This object is based on several tables. You must specify which table you are retrieving data from.'); |
|
| 252 | + } elseif (count($this->dbRows) === 1) { |
|
| 253 | + $tableName = array_keys($this->dbRows)[0]; |
|
| 254 | + } else { |
|
| 255 | + throw new TDBMException('Please specify a table for this object.'); |
|
| 256 | + } |
|
| 257 | + } |
|
| 258 | + |
|
| 259 | + if (!isset($this->dbRows[$tableName])) { |
|
| 260 | + $this->registerTable($tableName); |
|
| 261 | + } |
|
| 262 | + |
|
| 263 | + $oldLinkedBean = $this->dbRows[$tableName]->getRef($foreignKeyName); |
|
| 264 | + if ($oldLinkedBean !== null) { |
|
| 265 | + $oldLinkedBean->removeManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 266 | + } |
|
| 267 | + |
|
| 268 | + $this->dbRows[$tableName]->setRef($foreignKeyName, $bean); |
|
| 269 | + if ($this->dbRows[$tableName]->_getStatus() === TDBMObjectStateEnum::STATE_DIRTY) { |
|
| 270 | + $this->status = TDBMObjectStateEnum::STATE_DIRTY; |
|
| 271 | + } |
|
| 272 | + |
|
| 273 | + if ($bean !== null) { |
|
| 274 | + $bean->setManyToOneRelationship($tableName, $foreignKeyName, $this); |
|
| 275 | + } |
|
| 276 | + } |
|
| 277 | + |
|
| 278 | + /** |
|
| 279 | + * @param string $foreignKeyName A unique name for this reference |
|
| 280 | + * |
|
| 281 | + * @return AbstractTDBMObject|null |
|
| 282 | + */ |
|
| 283 | + protected function getRef($foreignKeyName, $tableName = null) |
|
| 284 | + { |
|
| 285 | + $tableName = $this->checkTableName($tableName); |
|
| 286 | + |
|
| 287 | + if (!isset($this->dbRows[$tableName])) { |
|
| 288 | + return; |
|
| 289 | + } |
|
| 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() : array; |
|
| 638 | - |
|
| 639 | - /** |
|
| 640 | - * Method called when the bean is removed from database. |
|
| 641 | - */ |
|
| 642 | - protected function onDelete() : void |
|
| 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() : array; |
|
| 638 | + |
|
| 639 | + /** |
|
| 640 | + * Method called when the bean is removed from database. |
|
| 641 | + */ |
|
| 642 | + protected function onDelete() : void |
|
| 643 | + { |
|
| 644 | + } |
|
| 645 | 645 | } |
@@ -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\Utils; |
| 6 | 6 | |
@@ -12,71 +12,71 @@ discard block |
||
| 12 | 12 | */ |
| 13 | 13 | class DirectForeignKeyMethodDescriptor implements MethodDescriptorInterface |
| 14 | 14 | { |
| 15 | - /** |
|
| 16 | - * @var ForeignKeyConstraint |
|
| 17 | - */ |
|
| 18 | - private $fk; |
|
| 19 | - |
|
| 20 | - private $useAlternateName = false; |
|
| 21 | - /** |
|
| 22 | - * @var Table |
|
| 23 | - */ |
|
| 24 | - private $mainTable; |
|
| 25 | - /** |
|
| 26 | - * @var NamingStrategyInterface |
|
| 27 | - */ |
|
| 28 | - private $namingStrategy; |
|
| 29 | - |
|
| 30 | - /** |
|
| 31 | - * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
| 32 | - * @param Table $mainTable The main table that is pointed to |
|
| 33 | - * @param NamingStrategyInterface $namingStrategy |
|
| 34 | - */ |
|
| 35 | - public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
| 36 | - { |
|
| 37 | - $this->fk = $fk; |
|
| 38 | - $this->mainTable = $mainTable; |
|
| 39 | - $this->namingStrategy = $namingStrategy; |
|
| 40 | - } |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * Returns the name of the method to be generated. |
|
| 44 | - * |
|
| 45 | - * @return string |
|
| 46 | - */ |
|
| 47 | - public function getName() : string |
|
| 48 | - { |
|
| 49 | - if (!$this->useAlternateName) { |
|
| 50 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
| 51 | - } else { |
|
| 52 | - $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
| 53 | - |
|
| 54 | - $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
| 55 | - |
|
| 56 | - $methodName .= implode('And', $camelizedColumns); |
|
| 57 | - |
|
| 58 | - return $methodName; |
|
| 59 | - } |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - /** |
|
| 63 | - * Requests the use of an alternative name for this method. |
|
| 64 | - */ |
|
| 65 | - public function useAlternativeName() |
|
| 66 | - { |
|
| 67 | - $this->useAlternateName = true; |
|
| 68 | - } |
|
| 69 | - |
|
| 70 | - /** |
|
| 71 | - * Returns the code of the method. |
|
| 72 | - * |
|
| 73 | - * @return string |
|
| 74 | - */ |
|
| 75 | - public function getCode() : string |
|
| 76 | - { |
|
| 77 | - $code = ''; |
|
| 78 | - |
|
| 79 | - $getterCode = ' /** |
|
| 15 | + /** |
|
| 16 | + * @var ForeignKeyConstraint |
|
| 17 | + */ |
|
| 18 | + private $fk; |
|
| 19 | + |
|
| 20 | + private $useAlternateName = false; |
|
| 21 | + /** |
|
| 22 | + * @var Table |
|
| 23 | + */ |
|
| 24 | + private $mainTable; |
|
| 25 | + /** |
|
| 26 | + * @var NamingStrategyInterface |
|
| 27 | + */ |
|
| 28 | + private $namingStrategy; |
|
| 29 | + |
|
| 30 | + /** |
|
| 31 | + * @param ForeignKeyConstraint $fk The foreign key pointing to our bean |
|
| 32 | + * @param Table $mainTable The main table that is pointed to |
|
| 33 | + * @param NamingStrategyInterface $namingStrategy |
|
| 34 | + */ |
|
| 35 | + public function __construct(ForeignKeyConstraint $fk, Table $mainTable, NamingStrategyInterface $namingStrategy) |
|
| 36 | + { |
|
| 37 | + $this->fk = $fk; |
|
| 38 | + $this->mainTable = $mainTable; |
|
| 39 | + $this->namingStrategy = $namingStrategy; |
|
| 40 | + } |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * Returns the name of the method to be generated. |
|
| 44 | + * |
|
| 45 | + * @return string |
|
| 46 | + */ |
|
| 47 | + public function getName() : string |
|
| 48 | + { |
|
| 49 | + if (!$this->useAlternateName) { |
|
| 50 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()); |
|
| 51 | + } else { |
|
| 52 | + $methodName = 'get'.TDBMDaoGenerator::toCamelCase($this->fk->getLocalTableName()).'By'; |
|
| 53 | + |
|
| 54 | + $camelizedColumns = array_map([TDBMDaoGenerator::class, 'toCamelCase'], $this->fk->getLocalColumns()); |
|
| 55 | + |
|
| 56 | + $methodName .= implode('And', $camelizedColumns); |
|
| 57 | + |
|
| 58 | + return $methodName; |
|
| 59 | + } |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + /** |
|
| 63 | + * Requests the use of an alternative name for this method. |
|
| 64 | + */ |
|
| 65 | + public function useAlternativeName() |
|
| 66 | + { |
|
| 67 | + $this->useAlternateName = true; |
|
| 68 | + } |
|
| 69 | + |
|
| 70 | + /** |
|
| 71 | + * Returns the code of the method. |
|
| 72 | + * |
|
| 73 | + * @return string |
|
| 74 | + */ |
|
| 75 | + public function getCode() : string |
|
| 76 | + { |
|
| 77 | + $code = ''; |
|
| 78 | + |
|
| 79 | + $getterCode = ' /** |
|
| 80 | 80 | * Returns the list of %s pointing to this bean via the %s column. |
| 81 | 81 | * |
| 82 | 82 | * @return %s[]|AlterableResultIterator |
@@ -88,55 +88,55 @@ discard block |
||
| 88 | 88 | |
| 89 | 89 | '; |
| 90 | 90 | |
| 91 | - $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
| 92 | - $code .= sprintf($getterCode, |
|
| 93 | - $beanClass, |
|
| 94 | - implode(', ', $this->fk->getColumns()), |
|
| 95 | - $beanClass, |
|
| 96 | - $this->getName(), |
|
| 97 | - var_export($this->fk->getLocalTableName(), true), |
|
| 98 | - var_export($this->fk->getName(), true), |
|
| 99 | - var_export($this->fk->getLocalTableName(), true), |
|
| 100 | - $this->getFilters($this->fk) |
|
| 101 | - ); |
|
| 102 | - |
|
| 103 | - return $code; |
|
| 104 | - } |
|
| 105 | - |
|
| 106 | - private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 107 | - { |
|
| 108 | - $counter = 0; |
|
| 109 | - $parameters = []; |
|
| 110 | - |
|
| 111 | - $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
| 112 | - |
|
| 113 | - foreach ($fk->getLocalColumns() as $columnName) { |
|
| 114 | - $pkColumn = $pkColumns[$counter]; |
|
| 115 | - $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
| 116 | - ++$counter; |
|
| 117 | - } |
|
| 118 | - $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 119 | - |
|
| 120 | - return $parametersCode; |
|
| 121 | - } |
|
| 122 | - |
|
| 123 | - /** |
|
| 124 | - * Returns an array of classes that needs a "use" for this method. |
|
| 125 | - * |
|
| 126 | - * @return string[] |
|
| 127 | - */ |
|
| 128 | - public function getUsedClasses() : array |
|
| 129 | - { |
|
| 130 | - return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
| 131 | - } |
|
| 132 | - |
|
| 133 | - /** |
|
| 134 | - * Returns the code to past in jsonSerialize. |
|
| 135 | - * |
|
| 136 | - * @return string |
|
| 137 | - */ |
|
| 138 | - public function getJsonSerializeCode() : string |
|
| 139 | - { |
|
| 140 | - return ''; |
|
| 141 | - } |
|
| 91 | + $beanClass = $this->namingStrategy->getBeanClassName($this->fk->getLocalTableName()); |
|
| 92 | + $code .= sprintf($getterCode, |
|
| 93 | + $beanClass, |
|
| 94 | + implode(', ', $this->fk->getColumns()), |
|
| 95 | + $beanClass, |
|
| 96 | + $this->getName(), |
|
| 97 | + var_export($this->fk->getLocalTableName(), true), |
|
| 98 | + var_export($this->fk->getName(), true), |
|
| 99 | + var_export($this->fk->getLocalTableName(), true), |
|
| 100 | + $this->getFilters($this->fk) |
|
| 101 | + ); |
|
| 102 | + |
|
| 103 | + return $code; |
|
| 104 | + } |
|
| 105 | + |
|
| 106 | + private function getFilters(ForeignKeyConstraint $fk) : string |
|
| 107 | + { |
|
| 108 | + $counter = 0; |
|
| 109 | + $parameters = []; |
|
| 110 | + |
|
| 111 | + $pkColumns = $this->mainTable->getPrimaryKeyColumns(); |
|
| 112 | + |
|
| 113 | + foreach ($fk->getLocalColumns() as $columnName) { |
|
| 114 | + $pkColumn = $pkColumns[$counter]; |
|
| 115 | + $parameters[] = sprintf('%s => $this->get(%s, %s)', var_export($fk->getLocalTableName().'.'.$columnName, true), var_export($pkColumn, true), var_export($this->fk->getForeignTableName(), true)); |
|
| 116 | + ++$counter; |
|
| 117 | + } |
|
| 118 | + $parametersCode = '['.implode(', ', $parameters).']'; |
|
| 119 | + |
|
| 120 | + return $parametersCode; |
|
| 121 | + } |
|
| 122 | + |
|
| 123 | + /** |
|
| 124 | + * Returns an array of classes that needs a "use" for this method. |
|
| 125 | + * |
|
| 126 | + * @return string[] |
|
| 127 | + */ |
|
| 128 | + public function getUsedClasses() : array |
|
| 129 | + { |
|
| 130 | + return [$this->namingStrategy->getBeanClassName($this->fk->getForeignTableName())]; |
|
| 131 | + } |
|
| 132 | + |
|
| 133 | + /** |
|
| 134 | + * Returns the code to past in jsonSerialize. |
|
| 135 | + * |
|
| 136 | + * @return string |
|
| 137 | + */ |
|
| 138 | + public function getJsonSerializeCode() : string |
|
| 139 | + { |
|
| 140 | + return ''; |
|
| 141 | + } |
|
| 142 | 142 | } |
@@ -8,7 +8,7 @@ |
||
| 8 | 8 | <input type="hidden" id="selfedit" name="selfedit" value="<?php echo plainstring_to_htmlprotected($this->selfedit) ?>" /> |
| 9 | 9 | |
| 10 | 10 | <?php if (!$this->autoloadDetected) { |
| 11 | - ?> |
|
| 11 | + ?> |
|
| 12 | 12 | <div class="alert">Warning! TDBM could not detect the autoload section of your composer.json file. |
| 13 | 13 | Unless you are developing your own autoload system, you should configure <strong>composer.json</strong> to <a href="http://getcomposer.org/doc/01-basic-usage.md#autoloading" target="_blank">define a source directory and a root namespace using PSR-0</a>.</div> |
| 14 | 14 | <?php |
@@ -616,7 +616,7 @@ |
||
| 616 | 616 | * Tries to put string to the singular form (if it is plural). |
| 617 | 617 | * We assume the table names are in english. |
| 618 | 618 | * |
| 619 | - * @param $str string |
|
| 619 | + * @param string $str string |
|
| 620 | 620 | * |
| 621 | 621 | * @return string |
| 622 | 622 | */ |
@@ -89,11 +89,11 @@ discard block |
||
| 89 | 89 | |
| 90 | 90 | // Remove all beans and daos from junction tables |
| 91 | 91 | $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
| 92 | - $junctionTableNames = array_map(function (Table $table) { |
|
| 92 | + $junctionTableNames = array_map(function(Table $table) { |
|
| 93 | 93 | return $table->getName(); |
| 94 | 94 | }, $junctionTables); |
| 95 | 95 | |
| 96 | - $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 96 | + $tableList = array_filter($tableList, function(Table $table) use ($junctionTableNames) { |
|
| 97 | 97 | return !in_array($table->getName(), $junctionTableNames); |
| 98 | 98 | }); |
| 99 | 99 | |
@@ -251,7 +251,7 @@ discard block |
||
| 251 | 251 | $usedBeans[] = $beanClassName; |
| 252 | 252 | // Let's suppress duplicates in used beans (if any) |
| 253 | 253 | $usedBeans = array_flip(array_flip($usedBeans)); |
| 254 | - $useStatements = array_map(function ($usedBean) { |
|
| 254 | + $useStatements = array_map(function($usedBean) { |
|
| 255 | 255 | return "use $usedBean;\n"; |
| 256 | 256 | }, $usedBeans); |
| 257 | 257 | |
@@ -606,7 +606,7 @@ discard block |
||
| 606 | 606 | $pos = strpos($str, ' '); |
| 607 | 607 | } |
| 608 | 608 | $before = substr($str, 0, $pos); |
| 609 | - $after = substr($str, $pos + 1); |
|
| 609 | + $after = substr($str, $pos+1); |
|
| 610 | 610 | $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
| 611 | 611 | } |
| 612 | 612 | |
@@ -682,7 +682,7 @@ discard block |
||
| 682 | 682 | $map = [ |
| 683 | 683 | Type::TARRAY => 'array', |
| 684 | 684 | Type::SIMPLE_ARRAY => 'array', |
| 685 | - 'json' => 'array', // 'json' is supported from Doctrine DBAL 2.6 only. |
|
| 685 | + 'json' => 'array', // 'json' is supported from Doctrine DBAL 2.6 only. |
|
| 686 | 686 | Type::JSON_ARRAY => 'array', |
| 687 | 687 | Type::BIGINT => 'string', |
| 688 | 688 | Type::BOOLEAN => 'bool', |
@@ -9,7 +9,6 @@ |
||
| 9 | 9 | use Mouf\Database\TDBM\ConfigurationInterface; |
| 10 | 10 | use Mouf\Database\TDBM\TDBMException; |
| 11 | 11 | use Mouf\Database\TDBM\TDBMSchemaAnalyzer; |
| 12 | -use Symfony\Component\EventDispatcher\EventDispatcherInterface; |
|
| 13 | 12 | use Symfony\Component\Filesystem\Filesystem; |
| 14 | 13 | |
| 15 | 14 | /** |
@@ -17,132 +17,132 @@ discard block |
||
| 17 | 17 | */ |
| 18 | 18 | class TDBMDaoGenerator |
| 19 | 19 | { |
| 20 | - /** |
|
| 21 | - * @var Schema |
|
| 22 | - */ |
|
| 23 | - private $schema; |
|
| 24 | - |
|
| 25 | - /** |
|
| 26 | - * Name of composer file. |
|
| 27 | - * |
|
| 28 | - * @var string |
|
| 29 | - */ |
|
| 30 | - private $composerFile; |
|
| 31 | - |
|
| 32 | - /** |
|
| 33 | - * @var TDBMSchemaAnalyzer |
|
| 34 | - */ |
|
| 35 | - private $tdbmSchemaAnalyzer; |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * @var GeneratorListenerInterface |
|
| 39 | - */ |
|
| 40 | - private $eventDispatcher; |
|
| 41 | - |
|
| 42 | - /** |
|
| 43 | - * @var NamingStrategyInterface |
|
| 44 | - */ |
|
| 45 | - private $namingStrategy; |
|
| 46 | - /** |
|
| 47 | - * @var ConfigurationInterface |
|
| 48 | - */ |
|
| 49 | - private $configuration; |
|
| 50 | - |
|
| 51 | - /** |
|
| 52 | - * Constructor. |
|
| 53 | - * |
|
| 54 | - * @param ConfigurationInterface $configuration |
|
| 55 | - * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 56 | - */ |
|
| 57 | - public function __construct(ConfigurationInterface $configuration, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 58 | - { |
|
| 59 | - $this->configuration = $configuration; |
|
| 60 | - $this->schema = $tdbmSchemaAnalyzer->getSchema(); |
|
| 61 | - $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 62 | - $this->namingStrategy = $configuration->getNamingStrategy(); |
|
| 63 | - $this->eventDispatcher = $configuration->getGeneratorEventDispatcher(); |
|
| 64 | - } |
|
| 65 | - |
|
| 66 | - /** |
|
| 67 | - * Generates all the daos and beans. |
|
| 68 | - * |
|
| 69 | - * @throws TDBMException |
|
| 70 | - */ |
|
| 71 | - public function generateAllDaosAndBeans(): void |
|
| 72 | - { |
|
| 73 | - // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 74 | - |
|
| 75 | - $tableList = $this->schema->getTables(); |
|
| 76 | - |
|
| 77 | - // Remove all beans and daos from junction tables |
|
| 78 | - $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
|
| 79 | - $junctionTableNames = array_map(function (Table $table) { |
|
| 80 | - return $table->getName(); |
|
| 81 | - }, $junctionTables); |
|
| 82 | - |
|
| 83 | - $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 84 | - return !in_array($table->getName(), $junctionTableNames); |
|
| 85 | - }); |
|
| 86 | - |
|
| 87 | - $beanDescriptors = []; |
|
| 88 | - |
|
| 89 | - foreach ($tableList as $table) { |
|
| 90 | - $beanDescriptors[] = $this->generateDaoAndBean($table); |
|
| 91 | - } |
|
| 92 | - |
|
| 93 | - |
|
| 94 | - $this->generateFactory($tableList); |
|
| 95 | - |
|
| 96 | - // Let's call the list of listeners |
|
| 97 | - $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors); |
|
| 98 | - } |
|
| 99 | - |
|
| 100 | - /** |
|
| 101 | - * Generates in one method call the daos and the beans for one table. |
|
| 102 | - * |
|
| 103 | - * @param Table $table |
|
| 104 | - * |
|
| 105 | - * @return BeanDescriptor |
|
| 106 | - * @throws TDBMException |
|
| 107 | - */ |
|
| 108 | - private function generateDaoAndBean(Table $table) : BeanDescriptor |
|
| 109 | - { |
|
| 110 | - $tableName = $table->getName(); |
|
| 111 | - $daoName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 112 | - $beanName = $this->namingStrategy->getBeanClassName($tableName); |
|
| 113 | - $baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
| 114 | - $baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName); |
|
| 115 | - |
|
| 116 | - $beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy); |
|
| 117 | - $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table); |
|
| 118 | - $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table); |
|
| 119 | - return $beanDescriptor; |
|
| 120 | - } |
|
| 121 | - |
|
| 122 | - /** |
|
| 123 | - * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 124 | - * |
|
| 125 | - * @param BeanDescriptor $beanDescriptor |
|
| 126 | - * @param string $className The name of the class |
|
| 127 | - * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 128 | - * @param Table $table The table |
|
| 129 | - * |
|
| 130 | - * @throws TDBMException |
|
| 131 | - */ |
|
| 132 | - public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table) |
|
| 133 | - { |
|
| 134 | - $beannamespace = $this->configuration->getBeanNamespace(); |
|
| 135 | - $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 136 | - |
|
| 137 | - $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
|
| 138 | - |
|
| 139 | - $this->dumpFile($possibleBaseFileName, $str); |
|
| 140 | - |
|
| 141 | - $possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname(); |
|
| 142 | - |
|
| 143 | - if (!file_exists($possibleFileName)) { |
|
| 144 | - $tableName = $table->getName(); |
|
| 145 | - $str = "<?php |
|
| 20 | + /** |
|
| 21 | + * @var Schema |
|
| 22 | + */ |
|
| 23 | + private $schema; |
|
| 24 | + |
|
| 25 | + /** |
|
| 26 | + * Name of composer file. |
|
| 27 | + * |
|
| 28 | + * @var string |
|
| 29 | + */ |
|
| 30 | + private $composerFile; |
|
| 31 | + |
|
| 32 | + /** |
|
| 33 | + * @var TDBMSchemaAnalyzer |
|
| 34 | + */ |
|
| 35 | + private $tdbmSchemaAnalyzer; |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * @var GeneratorListenerInterface |
|
| 39 | + */ |
|
| 40 | + private $eventDispatcher; |
|
| 41 | + |
|
| 42 | + /** |
|
| 43 | + * @var NamingStrategyInterface |
|
| 44 | + */ |
|
| 45 | + private $namingStrategy; |
|
| 46 | + /** |
|
| 47 | + * @var ConfigurationInterface |
|
| 48 | + */ |
|
| 49 | + private $configuration; |
|
| 50 | + |
|
| 51 | + /** |
|
| 52 | + * Constructor. |
|
| 53 | + * |
|
| 54 | + * @param ConfigurationInterface $configuration |
|
| 55 | + * @param TDBMSchemaAnalyzer $tdbmSchemaAnalyzer |
|
| 56 | + */ |
|
| 57 | + public function __construct(ConfigurationInterface $configuration, TDBMSchemaAnalyzer $tdbmSchemaAnalyzer) |
|
| 58 | + { |
|
| 59 | + $this->configuration = $configuration; |
|
| 60 | + $this->schema = $tdbmSchemaAnalyzer->getSchema(); |
|
| 61 | + $this->tdbmSchemaAnalyzer = $tdbmSchemaAnalyzer; |
|
| 62 | + $this->namingStrategy = $configuration->getNamingStrategy(); |
|
| 63 | + $this->eventDispatcher = $configuration->getGeneratorEventDispatcher(); |
|
| 64 | + } |
|
| 65 | + |
|
| 66 | + /** |
|
| 67 | + * Generates all the daos and beans. |
|
| 68 | + * |
|
| 69 | + * @throws TDBMException |
|
| 70 | + */ |
|
| 71 | + public function generateAllDaosAndBeans(): void |
|
| 72 | + { |
|
| 73 | + // TODO: check that no class name ends with "Base". Otherwise, there will be name clash. |
|
| 74 | + |
|
| 75 | + $tableList = $this->schema->getTables(); |
|
| 76 | + |
|
| 77 | + // Remove all beans and daos from junction tables |
|
| 78 | + $junctionTables = $this->configuration->getSchemaAnalyzer()->detectJunctionTables(true); |
|
| 79 | + $junctionTableNames = array_map(function (Table $table) { |
|
| 80 | + return $table->getName(); |
|
| 81 | + }, $junctionTables); |
|
| 82 | + |
|
| 83 | + $tableList = array_filter($tableList, function (Table $table) use ($junctionTableNames) { |
|
| 84 | + return !in_array($table->getName(), $junctionTableNames); |
|
| 85 | + }); |
|
| 86 | + |
|
| 87 | + $beanDescriptors = []; |
|
| 88 | + |
|
| 89 | + foreach ($tableList as $table) { |
|
| 90 | + $beanDescriptors[] = $this->generateDaoAndBean($table); |
|
| 91 | + } |
|
| 92 | + |
|
| 93 | + |
|
| 94 | + $this->generateFactory($tableList); |
|
| 95 | + |
|
| 96 | + // Let's call the list of listeners |
|
| 97 | + $this->eventDispatcher->onGenerate($this->configuration, $beanDescriptors); |
|
| 98 | + } |
|
| 99 | + |
|
| 100 | + /** |
|
| 101 | + * Generates in one method call the daos and the beans for one table. |
|
| 102 | + * |
|
| 103 | + * @param Table $table |
|
| 104 | + * |
|
| 105 | + * @return BeanDescriptor |
|
| 106 | + * @throws TDBMException |
|
| 107 | + */ |
|
| 108 | + private function generateDaoAndBean(Table $table) : BeanDescriptor |
|
| 109 | + { |
|
| 110 | + $tableName = $table->getName(); |
|
| 111 | + $daoName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 112 | + $beanName = $this->namingStrategy->getBeanClassName($tableName); |
|
| 113 | + $baseBeanName = $this->namingStrategy->getBaseBeanClassName($tableName); |
|
| 114 | + $baseDaoName = $this->namingStrategy->getBaseDaoClassName($tableName); |
|
| 115 | + |
|
| 116 | + $beanDescriptor = new BeanDescriptor($table, $this->configuration->getSchemaAnalyzer(), $this->schema, $this->tdbmSchemaAnalyzer, $this->namingStrategy); |
|
| 117 | + $this->generateBean($beanDescriptor, $beanName, $baseBeanName, $table); |
|
| 118 | + $this->generateDao($beanDescriptor, $daoName, $baseDaoName, $beanName, $table); |
|
| 119 | + return $beanDescriptor; |
|
| 120 | + } |
|
| 121 | + |
|
| 122 | + /** |
|
| 123 | + * Writes the PHP bean file with all getters and setters from the table passed in parameter. |
|
| 124 | + * |
|
| 125 | + * @param BeanDescriptor $beanDescriptor |
|
| 126 | + * @param string $className The name of the class |
|
| 127 | + * @param string $baseClassName The name of the base class which will be extended (name only, no directory) |
|
| 128 | + * @param Table $table The table |
|
| 129 | + * |
|
| 130 | + * @throws TDBMException |
|
| 131 | + */ |
|
| 132 | + public function generateBean(BeanDescriptor $beanDescriptor, $className, $baseClassName, Table $table) |
|
| 133 | + { |
|
| 134 | + $beannamespace = $this->configuration->getBeanNamespace(); |
|
| 135 | + $str = $beanDescriptor->generatePhpCode($beannamespace); |
|
| 136 | + |
|
| 137 | + $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
|
| 138 | + |
|
| 139 | + $this->dumpFile($possibleBaseFileName, $str); |
|
| 140 | + |
|
| 141 | + $possibleFileName = $this->configuration->getPathFinder()->getPath($beannamespace.'\\'.$className)->getPathname(); |
|
| 142 | + |
|
| 143 | + if (!file_exists($possibleFileName)) { |
|
| 144 | + $tableName = $table->getName(); |
|
| 145 | + $str = "<?php |
|
| 146 | 146 | /* |
| 147 | 147 | * This file has been automatically generated by TDBM. |
| 148 | 148 | * You can edit this file as it will not be overwritten. |
@@ -160,73 +160,73 @@ discard block |
||
| 160 | 160 | } |
| 161 | 161 | "; |
| 162 | 162 | |
| 163 | - $this->dumpFile($possibleFileName, $str); |
|
| 164 | - } |
|
| 165 | - } |
|
| 166 | - |
|
| 167 | - /** |
|
| 168 | - * Tries to find a @defaultSort annotation in one of the columns. |
|
| 169 | - * |
|
| 170 | - * @param Table $table |
|
| 171 | - * |
|
| 172 | - * @return array First item: column name, Second item: column order (asc/desc) |
|
| 173 | - */ |
|
| 174 | - private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 175 | - { |
|
| 176 | - $defaultSort = null; |
|
| 177 | - $defaultSortDirection = null; |
|
| 178 | - foreach ($table->getColumns() as $column) { |
|
| 179 | - $comments = $column->getComment(); |
|
| 180 | - $matches = []; |
|
| 181 | - if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 182 | - $defaultSort = $column->getName(); |
|
| 183 | - if (count($matches) === 3) { |
|
| 184 | - $defaultSortDirection = $matches[2]; |
|
| 185 | - } else { |
|
| 186 | - $defaultSortDirection = 'ASC'; |
|
| 187 | - } |
|
| 188 | - } |
|
| 189 | - } |
|
| 190 | - |
|
| 191 | - return [$defaultSort, $defaultSortDirection]; |
|
| 192 | - } |
|
| 193 | - |
|
| 194 | - /** |
|
| 195 | - * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 196 | - * |
|
| 197 | - * @param BeanDescriptor $beanDescriptor |
|
| 198 | - * @param string $className The name of the class |
|
| 199 | - * @param string $baseClassName |
|
| 200 | - * @param string $beanClassName |
|
| 201 | - * @param Table $table |
|
| 202 | - * |
|
| 203 | - * @throws TDBMException |
|
| 204 | - */ |
|
| 205 | - private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table) |
|
| 206 | - { |
|
| 207 | - $daonamespace = $this->configuration->getDaoNamespace(); |
|
| 208 | - $beannamespace = $this->configuration->getBeanNamespace(); |
|
| 209 | - $tableName = $table->getName(); |
|
| 210 | - $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 211 | - |
|
| 212 | - list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 213 | - |
|
| 214 | - // FIXME: lowercase tables with _ in the name should work! |
|
| 215 | - $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 216 | - |
|
| 217 | - $beanClassWithoutNameSpace = $beanClassName; |
|
| 218 | - $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 219 | - |
|
| 220 | - list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
| 221 | - |
|
| 222 | - $usedBeans[] = $beanClassName; |
|
| 223 | - // Let's suppress duplicates in used beans (if any) |
|
| 224 | - $usedBeans = array_flip(array_flip($usedBeans)); |
|
| 225 | - $useStatements = array_map(function ($usedBean) { |
|
| 226 | - return "use $usedBean;\n"; |
|
| 227 | - }, $usedBeans); |
|
| 228 | - |
|
| 229 | - $str = "<?php |
|
| 163 | + $this->dumpFile($possibleFileName, $str); |
|
| 164 | + } |
|
| 165 | + } |
|
| 166 | + |
|
| 167 | + /** |
|
| 168 | + * Tries to find a @defaultSort annotation in one of the columns. |
|
| 169 | + * |
|
| 170 | + * @param Table $table |
|
| 171 | + * |
|
| 172 | + * @return array First item: column name, Second item: column order (asc/desc) |
|
| 173 | + */ |
|
| 174 | + private function getDefaultSortColumnFromAnnotation(Table $table) |
|
| 175 | + { |
|
| 176 | + $defaultSort = null; |
|
| 177 | + $defaultSortDirection = null; |
|
| 178 | + foreach ($table->getColumns() as $column) { |
|
| 179 | + $comments = $column->getComment(); |
|
| 180 | + $matches = []; |
|
| 181 | + if (preg_match('/@defaultSort(\((desc|asc)\))*/', $comments, $matches) != 0) { |
|
| 182 | + $defaultSort = $column->getName(); |
|
| 183 | + if (count($matches) === 3) { |
|
| 184 | + $defaultSortDirection = $matches[2]; |
|
| 185 | + } else { |
|
| 186 | + $defaultSortDirection = 'ASC'; |
|
| 187 | + } |
|
| 188 | + } |
|
| 189 | + } |
|
| 190 | + |
|
| 191 | + return [$defaultSort, $defaultSortDirection]; |
|
| 192 | + } |
|
| 193 | + |
|
| 194 | + /** |
|
| 195 | + * Writes the PHP bean DAO with simple functions to create/get/save objects. |
|
| 196 | + * |
|
| 197 | + * @param BeanDescriptor $beanDescriptor |
|
| 198 | + * @param string $className The name of the class |
|
| 199 | + * @param string $baseClassName |
|
| 200 | + * @param string $beanClassName |
|
| 201 | + * @param Table $table |
|
| 202 | + * |
|
| 203 | + * @throws TDBMException |
|
| 204 | + */ |
|
| 205 | + private function generateDao(BeanDescriptor $beanDescriptor, string $className, string $baseClassName, string $beanClassName, Table $table) |
|
| 206 | + { |
|
| 207 | + $daonamespace = $this->configuration->getDaoNamespace(); |
|
| 208 | + $beannamespace = $this->configuration->getBeanNamespace(); |
|
| 209 | + $tableName = $table->getName(); |
|
| 210 | + $primaryKeyColumns = $table->getPrimaryKeyColumns(); |
|
| 211 | + |
|
| 212 | + list($defaultSort, $defaultSortDirection) = $this->getDefaultSortColumnFromAnnotation($table); |
|
| 213 | + |
|
| 214 | + // FIXME: lowercase tables with _ in the name should work! |
|
| 215 | + $tableCamel = self::toSingular(self::toCamelCase($tableName)); |
|
| 216 | + |
|
| 217 | + $beanClassWithoutNameSpace = $beanClassName; |
|
| 218 | + $beanClassName = $beannamespace.'\\'.$beanClassName; |
|
| 219 | + |
|
| 220 | + list($usedBeans, $findByDaoCode) = $beanDescriptor->generateFindByDaoCode($beannamespace, $beanClassWithoutNameSpace); |
|
| 221 | + |
|
| 222 | + $usedBeans[] = $beanClassName; |
|
| 223 | + // Let's suppress duplicates in used beans (if any) |
|
| 224 | + $usedBeans = array_flip(array_flip($usedBeans)); |
|
| 225 | + $useStatements = array_map(function ($usedBean) { |
|
| 226 | + return "use $usedBean;\n"; |
|
| 227 | + }, $usedBeans); |
|
| 228 | + |
|
| 229 | + $str = "<?php |
|
| 230 | 230 | |
| 231 | 231 | /* |
| 232 | 232 | * This file has been automatically generated by TDBM. |
@@ -302,10 +302,10 @@ discard block |
||
| 302 | 302 | } |
| 303 | 303 | "; |
| 304 | 304 | |
| 305 | - if (count($primaryKeyColumns) === 1) { |
|
| 306 | - $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 307 | - $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType()); |
|
| 308 | - $str .= " |
|
| 305 | + if (count($primaryKeyColumns) === 1) { |
|
| 306 | + $primaryKeyColumn = $primaryKeyColumns[0]; |
|
| 307 | + $primaryKeyPhpType = self::dbalTypeToPhpType($table->getColumn($primaryKeyColumn)->getType()); |
|
| 308 | + $str .= " |
|
| 309 | 309 | /** |
| 310 | 310 | * Get $beanClassWithoutNameSpace specified by its ID (its primary key) |
| 311 | 311 | * If the primary key does not exist, an exception is thrown. |
@@ -320,8 +320,8 @@ discard block |
||
| 320 | 320 | return \$this->tdbmService->findObjectByPk('$tableName', ['$primaryKeyColumn' => \$id], [], \$lazyLoading); |
| 321 | 321 | } |
| 322 | 322 | "; |
| 323 | - } |
|
| 324 | - $str .= " |
|
| 323 | + } |
|
| 324 | + $str .= " |
|
| 325 | 325 | /** |
| 326 | 326 | * Deletes the $beanClassWithoutNameSpace passed in parameter. |
| 327 | 327 | * |
@@ -421,19 +421,19 @@ discard block |
||
| 421 | 421 | } |
| 422 | 422 | "; |
| 423 | 423 | |
| 424 | - $str .= $findByDaoCode; |
|
| 425 | - $str .= '} |
|
| 424 | + $str .= $findByDaoCode; |
|
| 425 | + $str .= '} |
|
| 426 | 426 | '; |
| 427 | 427 | |
| 428 | - $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
|
| 428 | + $possibleBaseFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\Generated\\'.$baseClassName)->getPathname(); |
|
| 429 | 429 | |
| 430 | - $this->dumpFile($possibleBaseFileName, $str); |
|
| 430 | + $this->dumpFile($possibleBaseFileName, $str); |
|
| 431 | 431 | |
| 432 | - $possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname(); |
|
| 432 | + $possibleFileName = $this->configuration->getPathFinder()->getPath($daonamespace.'\\'.$className)->getPathname(); |
|
| 433 | 433 | |
| 434 | - // Now, let's generate the "editable" class |
|
| 435 | - if (!file_exists($possibleFileName)) { |
|
| 436 | - $str = "<?php |
|
| 434 | + // Now, let's generate the "editable" class |
|
| 435 | + if (!file_exists($possibleFileName)) { |
|
| 436 | + $str = "<?php |
|
| 437 | 437 | |
| 438 | 438 | /* |
| 439 | 439 | * This file has been automatically generated by TDBM. |
@@ -451,24 +451,24 @@ discard block |
||
| 451 | 451 | { |
| 452 | 452 | } |
| 453 | 453 | "; |
| 454 | - $this->dumpFile($possibleFileName, $str); |
|
| 455 | - } |
|
| 456 | - } |
|
| 454 | + $this->dumpFile($possibleFileName, $str); |
|
| 455 | + } |
|
| 456 | + } |
|
| 457 | 457 | |
| 458 | - /** |
|
| 459 | - * Generates the factory bean. |
|
| 460 | - * |
|
| 461 | - * @param Table[] $tableList |
|
| 462 | - * @throws TDBMException |
|
| 463 | - */ |
|
| 464 | - private function generateFactory(array $tableList) : void |
|
| 465 | - { |
|
| 466 | - $daoNamespace = $this->configuration->getDaoNamespace(); |
|
| 467 | - $daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName(); |
|
| 458 | + /** |
|
| 459 | + * Generates the factory bean. |
|
| 460 | + * |
|
| 461 | + * @param Table[] $tableList |
|
| 462 | + * @throws TDBMException |
|
| 463 | + */ |
|
| 464 | + private function generateFactory(array $tableList) : void |
|
| 465 | + { |
|
| 466 | + $daoNamespace = $this->configuration->getDaoNamespace(); |
|
| 467 | + $daoFactoryClassName = $this->namingStrategy->getDaoFactoryClassName(); |
|
| 468 | 468 | |
| 469 | - // For each table, let's write a property. |
|
| 469 | + // For each table, let's write a property. |
|
| 470 | 470 | |
| 471 | - $str = "<?php |
|
| 471 | + $str = "<?php |
|
| 472 | 472 | |
| 473 | 473 | /* |
| 474 | 474 | * This file has been automatically generated by TDBM. |
@@ -478,13 +478,13 @@ discard block |
||
| 478 | 478 | namespace {$daoNamespace}\\Generated; |
| 479 | 479 | |
| 480 | 480 | "; |
| 481 | - foreach ($tableList as $table) { |
|
| 482 | - $tableName = $table->getName(); |
|
| 483 | - $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 484 | - $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
| 485 | - } |
|
| 481 | + foreach ($tableList as $table) { |
|
| 482 | + $tableName = $table->getName(); |
|
| 483 | + $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 484 | + $str .= "use {$daoNamespace}\\".$daoClassName.";\n"; |
|
| 485 | + } |
|
| 486 | 486 | |
| 487 | - $str .= " |
|
| 487 | + $str .= " |
|
| 488 | 488 | /** |
| 489 | 489 | * The $daoFactoryClassName provides an easy access to all DAOs generated by TDBM. |
| 490 | 490 | * |
@@ -493,12 +493,12 @@ discard block |
||
| 493 | 493 | { |
| 494 | 494 | "; |
| 495 | 495 | |
| 496 | - foreach ($tableList as $table) { |
|
| 497 | - $tableName = $table->getName(); |
|
| 498 | - $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 499 | - $daoInstanceName = self::toVariableName($daoClassName); |
|
| 496 | + foreach ($tableList as $table) { |
|
| 497 | + $tableName = $table->getName(); |
|
| 498 | + $daoClassName = $this->namingStrategy->getDaoClassName($tableName); |
|
| 499 | + $daoInstanceName = self::toVariableName($daoClassName); |
|
| 500 | 500 | |
| 501 | - $str .= ' /** |
|
| 501 | + $str .= ' /** |
|
| 502 | 502 | * @var '.$daoClassName.' |
| 503 | 503 | */ |
| 504 | 504 | private $'.$daoInstanceName.'; |
@@ -522,131 +522,131 @@ discard block |
||
| 522 | 522 | { |
| 523 | 523 | $this->'.$daoInstanceName.' = $'.$daoInstanceName.'; |
| 524 | 524 | }'; |
| 525 | - } |
|
| 525 | + } |
|
| 526 | 526 | |
| 527 | - $str .= ' |
|
| 527 | + $str .= ' |
|
| 528 | 528 | } |
| 529 | 529 | '; |
| 530 | 530 | |
| 531 | - $possibleFileName = $this->configuration->getPathFinder()->getPath($daoNamespace.'\\Generated\\'.$daoFactoryClassName)->getPathname(); |
|
| 532 | - |
|
| 533 | - $this->dumpFile($possibleFileName, $str); |
|
| 534 | - } |
|
| 535 | - |
|
| 536 | - /** |
|
| 537 | - * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 538 | - * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 539 | - * |
|
| 540 | - * @param $str string |
|
| 541 | - * |
|
| 542 | - * @return string |
|
| 543 | - */ |
|
| 544 | - public static function toCamelCase($str) |
|
| 545 | - { |
|
| 546 | - $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 547 | - while (true) { |
|
| 548 | - if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 549 | - break; |
|
| 550 | - } |
|
| 551 | - |
|
| 552 | - $pos = strpos($str, '_'); |
|
| 553 | - if ($pos === false) { |
|
| 554 | - $pos = strpos($str, ' '); |
|
| 555 | - } |
|
| 556 | - $before = substr($str, 0, $pos); |
|
| 557 | - $after = substr($str, $pos + 1); |
|
| 558 | - $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 559 | - } |
|
| 560 | - |
|
| 561 | - return $str; |
|
| 562 | - } |
|
| 563 | - |
|
| 564 | - /** |
|
| 565 | - * Tries to put string to the singular form (if it is plural). |
|
| 566 | - * We assume the table names are in english. |
|
| 567 | - * |
|
| 568 | - * @param $str string |
|
| 569 | - * |
|
| 570 | - * @return string |
|
| 571 | - */ |
|
| 572 | - public static function toSingular($str) |
|
| 573 | - { |
|
| 574 | - return Inflector::singularize($str); |
|
| 575 | - } |
|
| 576 | - |
|
| 577 | - /** |
|
| 578 | - * Put the first letter of the string in lower case. |
|
| 579 | - * Very useful to transform a class name into a variable name. |
|
| 580 | - * |
|
| 581 | - * @param $str string |
|
| 582 | - * |
|
| 583 | - * @return string |
|
| 584 | - */ |
|
| 585 | - public static function toVariableName($str) |
|
| 586 | - { |
|
| 587 | - return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 588 | - } |
|
| 589 | - |
|
| 590 | - /** |
|
| 591 | - * Ensures the file passed in parameter can be written in its directory. |
|
| 592 | - * |
|
| 593 | - * @param string $fileName |
|
| 594 | - * |
|
| 595 | - * @throws TDBMException |
|
| 596 | - */ |
|
| 597 | - private function ensureDirectoryExist(string $fileName) |
|
| 598 | - { |
|
| 599 | - $dirName = dirname($fileName); |
|
| 600 | - if (!file_exists($dirName)) { |
|
| 601 | - $old = umask(0); |
|
| 602 | - $result = mkdir($dirName, 0775, true); |
|
| 603 | - umask($old); |
|
| 604 | - if ($result === false) { |
|
| 605 | - throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 606 | - } |
|
| 607 | - } |
|
| 608 | - } |
|
| 609 | - |
|
| 610 | - private function dumpFile(string $fileName, string $content) : void |
|
| 611 | - { |
|
| 612 | - $this->ensureDirectoryExist($fileName); |
|
| 613 | - $fileSystem = new Filesystem(); |
|
| 614 | - $fileSystem->dumpFile($fileName, $content); |
|
| 615 | - @chmod($fileName, 0664); |
|
| 616 | - } |
|
| 617 | - |
|
| 618 | - /** |
|
| 619 | - * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 620 | - * |
|
| 621 | - * @param Type $type The DBAL type |
|
| 622 | - * |
|
| 623 | - * @return string The PHP type |
|
| 624 | - */ |
|
| 625 | - public static function dbalTypeToPhpType(Type $type) |
|
| 626 | - { |
|
| 627 | - $map = [ |
|
| 628 | - Type::TARRAY => 'array', |
|
| 629 | - Type::SIMPLE_ARRAY => 'array', |
|
| 630 | - 'json' => 'array', // 'json' is supported from Doctrine DBAL 2.6 only. |
|
| 631 | - Type::JSON_ARRAY => 'array', |
|
| 632 | - Type::BIGINT => 'string', |
|
| 633 | - Type::BOOLEAN => 'bool', |
|
| 634 | - Type::DATETIME => '\DateTimeInterface', |
|
| 635 | - Type::DATETIMETZ => '\DateTimeInterface', |
|
| 636 | - Type::DATE => '\DateTimeInterface', |
|
| 637 | - Type::TIME => '\DateTimeInterface', |
|
| 638 | - Type::DECIMAL => 'float', |
|
| 639 | - Type::INTEGER => 'int', |
|
| 640 | - Type::OBJECT => 'string', |
|
| 641 | - Type::SMALLINT => 'int', |
|
| 642 | - Type::STRING => 'string', |
|
| 643 | - Type::TEXT => 'string', |
|
| 644 | - Type::BINARY => 'string', |
|
| 645 | - Type::BLOB => 'string', |
|
| 646 | - Type::FLOAT => 'float', |
|
| 647 | - Type::GUID => 'string', |
|
| 648 | - ]; |
|
| 649 | - |
|
| 650 | - return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 651 | - } |
|
| 531 | + $possibleFileName = $this->configuration->getPathFinder()->getPath($daoNamespace.'\\Generated\\'.$daoFactoryClassName)->getPathname(); |
|
| 532 | + |
|
| 533 | + $this->dumpFile($possibleFileName, $str); |
|
| 534 | + } |
|
| 535 | + |
|
| 536 | + /** |
|
| 537 | + * Transforms a string to camelCase (except the first letter will be uppercase too). |
|
| 538 | + * Underscores and spaces are removed and the first letter after the underscore is uppercased. |
|
| 539 | + * |
|
| 540 | + * @param $str string |
|
| 541 | + * |
|
| 542 | + * @return string |
|
| 543 | + */ |
|
| 544 | + public static function toCamelCase($str) |
|
| 545 | + { |
|
| 546 | + $str = strtoupper(substr($str, 0, 1)).substr($str, 1); |
|
| 547 | + while (true) { |
|
| 548 | + if (strpos($str, '_') === false && strpos($str, ' ') === false) { |
|
| 549 | + break; |
|
| 550 | + } |
|
| 551 | + |
|
| 552 | + $pos = strpos($str, '_'); |
|
| 553 | + if ($pos === false) { |
|
| 554 | + $pos = strpos($str, ' '); |
|
| 555 | + } |
|
| 556 | + $before = substr($str, 0, $pos); |
|
| 557 | + $after = substr($str, $pos + 1); |
|
| 558 | + $str = $before.strtoupper(substr($after, 0, 1)).substr($after, 1); |
|
| 559 | + } |
|
| 560 | + |
|
| 561 | + return $str; |
|
| 562 | + } |
|
| 563 | + |
|
| 564 | + /** |
|
| 565 | + * Tries to put string to the singular form (if it is plural). |
|
| 566 | + * We assume the table names are in english. |
|
| 567 | + * |
|
| 568 | + * @param $str string |
|
| 569 | + * |
|
| 570 | + * @return string |
|
| 571 | + */ |
|
| 572 | + public static function toSingular($str) |
|
| 573 | + { |
|
| 574 | + return Inflector::singularize($str); |
|
| 575 | + } |
|
| 576 | + |
|
| 577 | + /** |
|
| 578 | + * Put the first letter of the string in lower case. |
|
| 579 | + * Very useful to transform a class name into a variable name. |
|
| 580 | + * |
|
| 581 | + * @param $str string |
|
| 582 | + * |
|
| 583 | + * @return string |
|
| 584 | + */ |
|
| 585 | + public static function toVariableName($str) |
|
| 586 | + { |
|
| 587 | + return strtolower(substr($str, 0, 1)).substr($str, 1); |
|
| 588 | + } |
|
| 589 | + |
|
| 590 | + /** |
|
| 591 | + * Ensures the file passed in parameter can be written in its directory. |
|
| 592 | + * |
|
| 593 | + * @param string $fileName |
|
| 594 | + * |
|
| 595 | + * @throws TDBMException |
|
| 596 | + */ |
|
| 597 | + private function ensureDirectoryExist(string $fileName) |
|
| 598 | + { |
|
| 599 | + $dirName = dirname($fileName); |
|
| 600 | + if (!file_exists($dirName)) { |
|
| 601 | + $old = umask(0); |
|
| 602 | + $result = mkdir($dirName, 0775, true); |
|
| 603 | + umask($old); |
|
| 604 | + if ($result === false) { |
|
| 605 | + throw new TDBMException("Unable to create directory: '".$dirName."'."); |
|
| 606 | + } |
|
| 607 | + } |
|
| 608 | + } |
|
| 609 | + |
|
| 610 | + private function dumpFile(string $fileName, string $content) : void |
|
| 611 | + { |
|
| 612 | + $this->ensureDirectoryExist($fileName); |
|
| 613 | + $fileSystem = new Filesystem(); |
|
| 614 | + $fileSystem->dumpFile($fileName, $content); |
|
| 615 | + @chmod($fileName, 0664); |
|
| 616 | + } |
|
| 617 | + |
|
| 618 | + /** |
|
| 619 | + * Transforms a DBAL type into a PHP type (for PHPDoc purpose). |
|
| 620 | + * |
|
| 621 | + * @param Type $type The DBAL type |
|
| 622 | + * |
|
| 623 | + * @return string The PHP type |
|
| 624 | + */ |
|
| 625 | + public static function dbalTypeToPhpType(Type $type) |
|
| 626 | + { |
|
| 627 | + $map = [ |
|
| 628 | + Type::TARRAY => 'array', |
|
| 629 | + Type::SIMPLE_ARRAY => 'array', |
|
| 630 | + 'json' => 'array', // 'json' is supported from Doctrine DBAL 2.6 only. |
|
| 631 | + Type::JSON_ARRAY => 'array', |
|
| 632 | + Type::BIGINT => 'string', |
|
| 633 | + Type::BOOLEAN => 'bool', |
|
| 634 | + Type::DATETIME => '\DateTimeInterface', |
|
| 635 | + Type::DATETIMETZ => '\DateTimeInterface', |
|
| 636 | + Type::DATE => '\DateTimeInterface', |
|
| 637 | + Type::TIME => '\DateTimeInterface', |
|
| 638 | + Type::DECIMAL => 'float', |
|
| 639 | + Type::INTEGER => 'int', |
|
| 640 | + Type::OBJECT => 'string', |
|
| 641 | + Type::SMALLINT => 'int', |
|
| 642 | + Type::STRING => 'string', |
|
| 643 | + Type::TEXT => 'string', |
|
| 644 | + Type::BINARY => 'string', |
|
| 645 | + Type::BLOB => 'string', |
|
| 646 | + Type::FLOAT => 'float', |
|
| 647 | + Type::GUID => 'string', |
|
| 648 | + ]; |
|
| 649 | + |
|
| 650 | + return isset($map[$type->getName()]) ? $map[$type->getName()] : $type->getName(); |
|
| 651 | + } |
|
| 652 | 652 | } |
@@ -11,136 +11,136 @@ discard block |
||
| 11 | 11 | */ |
| 12 | 12 | class ScalarBeanPropertyDescriptor extends AbstractBeanPropertyDescriptor |
| 13 | 13 | { |
| 14 | - /** |
|
| 15 | - * @var Column |
|
| 16 | - */ |
|
| 17 | - private $column; |
|
| 18 | - /** |
|
| 19 | - * @var NamingStrategyInterface |
|
| 20 | - */ |
|
| 21 | - private $namingStrategy; |
|
| 22 | - |
|
| 23 | - /** |
|
| 24 | - * ScalarBeanPropertyDescriptor constructor. |
|
| 25 | - * @param Table $table |
|
| 26 | - * @param Column $column |
|
| 27 | - * @param NamingStrategyInterface $namingStrategy |
|
| 28 | - */ |
|
| 29 | - public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
| 30 | - { |
|
| 31 | - parent::__construct($table); |
|
| 32 | - $this->table = $table; |
|
| 33 | - $this->column = $column; |
|
| 34 | - $this->namingStrategy = $namingStrategy; |
|
| 35 | - } |
|
| 36 | - |
|
| 37 | - /** |
|
| 38 | - * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 39 | - * |
|
| 40 | - * @return ForeignKeyConstraint|null |
|
| 41 | - */ |
|
| 42 | - public function getForeignKey() |
|
| 43 | - { |
|
| 44 | - return false; |
|
| 45 | - } |
|
| 46 | - |
|
| 47 | - /** |
|
| 48 | - * Returns the param annotation for this property (useful for constructor). |
|
| 49 | - * |
|
| 50 | - * @return string |
|
| 51 | - */ |
|
| 52 | - public function getParamAnnotation() |
|
| 53 | - { |
|
| 54 | - $className = $this->getClassName(); |
|
| 55 | - $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 56 | - |
|
| 57 | - $str = ' * @param %s %s'; |
|
| 58 | - |
|
| 59 | - return sprintf($str, $paramType, $this->getVariableName()); |
|
| 60 | - } |
|
| 61 | - |
|
| 62 | - public function getUpperCamelCaseName() |
|
| 63 | - { |
|
| 64 | - return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 69 | - * |
|
| 70 | - * @return null|string |
|
| 71 | - */ |
|
| 72 | - public function getClassName() |
|
| 73 | - { |
|
| 74 | - return; |
|
| 75 | - } |
|
| 76 | - |
|
| 77 | - /** |
|
| 78 | - * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 79 | - * |
|
| 80 | - * @return bool |
|
| 81 | - */ |
|
| 82 | - public function isCompulsory() |
|
| 83 | - { |
|
| 84 | - return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
| 85 | - } |
|
| 86 | - |
|
| 87 | - /** |
|
| 88 | - * Returns true if the property has a default value. |
|
| 89 | - * |
|
| 90 | - * @return bool |
|
| 91 | - */ |
|
| 92 | - public function hasDefault() |
|
| 93 | - { |
|
| 94 | - return $this->column->getDefault() !== null; |
|
| 95 | - } |
|
| 96 | - |
|
| 97 | - /** |
|
| 98 | - * Returns the code that assigns a value to its default value. |
|
| 99 | - * |
|
| 100 | - * @return string |
|
| 101 | - */ |
|
| 102 | - public function assignToDefaultCode() |
|
| 103 | - { |
|
| 104 | - $str = ' $this->%s(%s);'; |
|
| 105 | - |
|
| 106 | - $default = $this->column->getDefault(); |
|
| 107 | - |
|
| 108 | - if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
| 109 | - $defaultCode = 'new \DateTimeImmutable()'; |
|
| 110 | - } else { |
|
| 111 | - $defaultCode = var_export($this->column->getDefault(), true); |
|
| 112 | - } |
|
| 113 | - |
|
| 114 | - return sprintf($str, $this->getSetterName(), $defaultCode); |
|
| 115 | - } |
|
| 116 | - |
|
| 117 | - /** |
|
| 118 | - * Returns true if the property is the primary key. |
|
| 119 | - * |
|
| 120 | - * @return bool |
|
| 121 | - */ |
|
| 122 | - public function isPrimaryKey() |
|
| 123 | - { |
|
| 124 | - return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 125 | - } |
|
| 126 | - |
|
| 127 | - /** |
|
| 128 | - * Returns the PHP code for getters and setters. |
|
| 129 | - * |
|
| 130 | - * @return string |
|
| 131 | - */ |
|
| 132 | - public function getGetterSetterCode() |
|
| 133 | - { |
|
| 134 | - $type = $this->column->getType(); |
|
| 135 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 136 | - |
|
| 137 | - $columnGetterName = $this->getGetterName(); |
|
| 138 | - $columnSetterName = $this->getSetterName(); |
|
| 139 | - |
|
| 140 | - // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
| 141 | - $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
| 142 | - |
|
| 143 | - $getterAndSetterCode = ' /** |
|
| 14 | + /** |
|
| 15 | + * @var Column |
|
| 16 | + */ |
|
| 17 | + private $column; |
|
| 18 | + /** |
|
| 19 | + * @var NamingStrategyInterface |
|
| 20 | + */ |
|
| 21 | + private $namingStrategy; |
|
| 22 | + |
|
| 23 | + /** |
|
| 24 | + * ScalarBeanPropertyDescriptor constructor. |
|
| 25 | + * @param Table $table |
|
| 26 | + * @param Column $column |
|
| 27 | + * @param NamingStrategyInterface $namingStrategy |
|
| 28 | + */ |
|
| 29 | + public function __construct(Table $table, Column $column, NamingStrategyInterface $namingStrategy) |
|
| 30 | + { |
|
| 31 | + parent::__construct($table); |
|
| 32 | + $this->table = $table; |
|
| 33 | + $this->column = $column; |
|
| 34 | + $this->namingStrategy = $namingStrategy; |
|
| 35 | + } |
|
| 36 | + |
|
| 37 | + /** |
|
| 38 | + * Returns the foreign-key the column is part of, if any. null otherwise. |
|
| 39 | + * |
|
| 40 | + * @return ForeignKeyConstraint|null |
|
| 41 | + */ |
|
| 42 | + public function getForeignKey() |
|
| 43 | + { |
|
| 44 | + return false; |
|
| 45 | + } |
|
| 46 | + |
|
| 47 | + /** |
|
| 48 | + * Returns the param annotation for this property (useful for constructor). |
|
| 49 | + * |
|
| 50 | + * @return string |
|
| 51 | + */ |
|
| 52 | + public function getParamAnnotation() |
|
| 53 | + { |
|
| 54 | + $className = $this->getClassName(); |
|
| 55 | + $paramType = $className ?: TDBMDaoGenerator::dbalTypeToPhpType($this->column->getType()); |
|
| 56 | + |
|
| 57 | + $str = ' * @param %s %s'; |
|
| 58 | + |
|
| 59 | + return sprintf($str, $paramType, $this->getVariableName()); |
|
| 60 | + } |
|
| 61 | + |
|
| 62 | + public function getUpperCamelCaseName() |
|
| 63 | + { |
|
| 64 | + return TDBMDaoGenerator::toCamelCase($this->column->getName()); |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Returns the name of the class linked to this property or null if this is not a foreign key. |
|
| 69 | + * |
|
| 70 | + * @return null|string |
|
| 71 | + */ |
|
| 72 | + public function getClassName() |
|
| 73 | + { |
|
| 74 | + return; |
|
| 75 | + } |
|
| 76 | + |
|
| 77 | + /** |
|
| 78 | + * Returns true if the property is compulsory (and therefore should be fetched in the constructor). |
|
| 79 | + * |
|
| 80 | + * @return bool |
|
| 81 | + */ |
|
| 82 | + public function isCompulsory() |
|
| 83 | + { |
|
| 84 | + return $this->column->getNotnull() && !$this->column->getAutoincrement() && $this->column->getDefault() === null; |
|
| 85 | + } |
|
| 86 | + |
|
| 87 | + /** |
|
| 88 | + * Returns true if the property has a default value. |
|
| 89 | + * |
|
| 90 | + * @return bool |
|
| 91 | + */ |
|
| 92 | + public function hasDefault() |
|
| 93 | + { |
|
| 94 | + return $this->column->getDefault() !== null; |
|
| 95 | + } |
|
| 96 | + |
|
| 97 | + /** |
|
| 98 | + * Returns the code that assigns a value to its default value. |
|
| 99 | + * |
|
| 100 | + * @return string |
|
| 101 | + */ |
|
| 102 | + public function assignToDefaultCode() |
|
| 103 | + { |
|
| 104 | + $str = ' $this->%s(%s);'; |
|
| 105 | + |
|
| 106 | + $default = $this->column->getDefault(); |
|
| 107 | + |
|
| 108 | + if (strtoupper($default) === 'CURRENT_TIMESTAMP') { |
|
| 109 | + $defaultCode = 'new \DateTimeImmutable()'; |
|
| 110 | + } else { |
|
| 111 | + $defaultCode = var_export($this->column->getDefault(), true); |
|
| 112 | + } |
|
| 113 | + |
|
| 114 | + return sprintf($str, $this->getSetterName(), $defaultCode); |
|
| 115 | + } |
|
| 116 | + |
|
| 117 | + /** |
|
| 118 | + * Returns true if the property is the primary key. |
|
| 119 | + * |
|
| 120 | + * @return bool |
|
| 121 | + */ |
|
| 122 | + public function isPrimaryKey() |
|
| 123 | + { |
|
| 124 | + return in_array($this->column->getName(), $this->table->getPrimaryKeyColumns()); |
|
| 125 | + } |
|
| 126 | + |
|
| 127 | + /** |
|
| 128 | + * Returns the PHP code for getters and setters. |
|
| 129 | + * |
|
| 130 | + * @return string |
|
| 131 | + */ |
|
| 132 | + public function getGetterSetterCode() |
|
| 133 | + { |
|
| 134 | + $type = $this->column->getType(); |
|
| 135 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 136 | + |
|
| 137 | + $columnGetterName = $this->getGetterName(); |
|
| 138 | + $columnSetterName = $this->getSetterName(); |
|
| 139 | + |
|
| 140 | + // A column type can be forced if it is not nullable and not auto-incrementable (for auto-increment columns, we can get "null" as long as the bean is not saved). |
|
| 141 | + $isNullable = !$this->column->getNotnull() || $this->column->getAutoincrement(); |
|
| 142 | + |
|
| 143 | + $getterAndSetterCode = ' /** |
|
| 144 | 144 | * The getter for the "%s" column. |
| 145 | 145 | * |
| 146 | 146 | * @return %s |
@@ -162,54 +162,54 @@ discard block |
||
| 162 | 162 | |
| 163 | 163 | '; |
| 164 | 164 | |
| 165 | - return sprintf($getterAndSetterCode, |
|
| 166 | - // Getter |
|
| 167 | - $this->column->getName(), |
|
| 168 | - $normalizedType.($isNullable ? '|null' : ''), |
|
| 169 | - $columnGetterName, |
|
| 170 | - ($isNullable ? '?' : ''), |
|
| 171 | - $normalizedType, |
|
| 172 | - var_export($this->column->getName(), true), |
|
| 173 | - var_export($this->table->getName(), true), |
|
| 174 | - // Setter |
|
| 175 | - $this->column->getName(), |
|
| 176 | - $normalizedType, |
|
| 177 | - $this->column->getName(), |
|
| 178 | - $columnSetterName, |
|
| 179 | - $this->column->getNotnull() ? '' : '?', |
|
| 180 | - $normalizedType, |
|
| 181 | - //$castTo, |
|
| 182 | - $this->column->getName(), |
|
| 183 | - var_export($this->column->getName(), true), |
|
| 184 | - $this->column->getName(), |
|
| 185 | - var_export($this->table->getName(), true) |
|
| 186 | - ); |
|
| 187 | - } |
|
| 188 | - |
|
| 189 | - /** |
|
| 190 | - * Returns the part of code useful when doing json serialization. |
|
| 191 | - * |
|
| 192 | - * @return string |
|
| 193 | - */ |
|
| 194 | - public function getJsonSerializeCode() |
|
| 195 | - { |
|
| 196 | - $type = $this->column->getType(); |
|
| 197 | - $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 198 | - |
|
| 199 | - if ($normalizedType == '\\DateTimeInterface') { |
|
| 200 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 201 | - } else { |
|
| 202 | - return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 203 | - } |
|
| 204 | - } |
|
| 205 | - |
|
| 206 | - /** |
|
| 207 | - * Returns the column name. |
|
| 208 | - * |
|
| 209 | - * @return string |
|
| 210 | - */ |
|
| 211 | - public function getColumnName() |
|
| 212 | - { |
|
| 213 | - return $this->column->getName(); |
|
| 214 | - } |
|
| 165 | + return sprintf($getterAndSetterCode, |
|
| 166 | + // Getter |
|
| 167 | + $this->column->getName(), |
|
| 168 | + $normalizedType.($isNullable ? '|null' : ''), |
|
| 169 | + $columnGetterName, |
|
| 170 | + ($isNullable ? '?' : ''), |
|
| 171 | + $normalizedType, |
|
| 172 | + var_export($this->column->getName(), true), |
|
| 173 | + var_export($this->table->getName(), true), |
|
| 174 | + // Setter |
|
| 175 | + $this->column->getName(), |
|
| 176 | + $normalizedType, |
|
| 177 | + $this->column->getName(), |
|
| 178 | + $columnSetterName, |
|
| 179 | + $this->column->getNotnull() ? '' : '?', |
|
| 180 | + $normalizedType, |
|
| 181 | + //$castTo, |
|
| 182 | + $this->column->getName(), |
|
| 183 | + var_export($this->column->getName(), true), |
|
| 184 | + $this->column->getName(), |
|
| 185 | + var_export($this->table->getName(), true) |
|
| 186 | + ); |
|
| 187 | + } |
|
| 188 | + |
|
| 189 | + /** |
|
| 190 | + * Returns the part of code useful when doing json serialization. |
|
| 191 | + * |
|
| 192 | + * @return string |
|
| 193 | + */ |
|
| 194 | + public function getJsonSerializeCode() |
|
| 195 | + { |
|
| 196 | + $type = $this->column->getType(); |
|
| 197 | + $normalizedType = TDBMDaoGenerator::dbalTypeToPhpType($type); |
|
| 198 | + |
|
| 199 | + if ($normalizedType == '\\DateTimeInterface') { |
|
| 200 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = ($this->'.$this->getGetterName().'() === null) ? null : $this->'.$this->getGetterName()."()->format('c');\n"; |
|
| 201 | + } else { |
|
| 202 | + return ' $array['.var_export($this->getLowerCamelCaseName(), true).'] = $this->'.$this->getGetterName()."();\n"; |
|
| 203 | + } |
|
| 204 | + } |
|
| 205 | + |
|
| 206 | + /** |
|
| 207 | + * Returns the column name. |
|
| 208 | + * |
|
| 209 | + * @return string |
|
| 210 | + */ |
|
| 211 | + public function getColumnName() |
|
| 212 | + { |
|
| 213 | + return $this->column->getName(); |
|
| 214 | + } |
|
| 215 | 215 | } |
@@ -7,105 +7,105 @@ discard block |
||
| 7 | 7 | |
| 8 | 8 | class PivotTableMethodsDescriptor implements MethodDescriptorInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var Table |
|
| 12 | - */ |
|
| 13 | - private $pivotTable; |
|
| 14 | - |
|
| 15 | - private $useAlternateName = false; |
|
| 16 | - |
|
| 17 | - /** |
|
| 18 | - * @var ForeignKeyConstraint |
|
| 19 | - */ |
|
| 20 | - private $localFk; |
|
| 21 | - |
|
| 22 | - /** |
|
| 23 | - * @var ForeignKeyConstraint |
|
| 24 | - */ |
|
| 25 | - private $remoteFk; |
|
| 26 | - /** |
|
| 27 | - * @var NamingStrategyInterface |
|
| 28 | - */ |
|
| 29 | - private $namingStrategy; |
|
| 30 | - |
|
| 31 | - /** |
|
| 32 | - * @param Table $pivotTable The pivot table |
|
| 33 | - * @param ForeignKeyConstraint $localFk |
|
| 34 | - * @param ForeignKeyConstraint $remoteFk |
|
| 35 | - * @param NamingStrategyInterface $namingStrategy |
|
| 36 | - */ |
|
| 37 | - public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
| 38 | - { |
|
| 39 | - $this->pivotTable = $pivotTable; |
|
| 40 | - $this->localFk = $localFk; |
|
| 41 | - $this->remoteFk = $remoteFk; |
|
| 42 | - $this->namingStrategy = $namingStrategy; |
|
| 43 | - } |
|
| 44 | - |
|
| 45 | - /** |
|
| 46 | - * Requests the use of an alternative name for this method. |
|
| 47 | - */ |
|
| 48 | - public function useAlternativeName() |
|
| 49 | - { |
|
| 50 | - $this->useAlternateName = true; |
|
| 51 | - } |
|
| 52 | - |
|
| 53 | - /** |
|
| 54 | - * Returns the name of the method to be generated. |
|
| 55 | - * |
|
| 56 | - * @return string |
|
| 57 | - */ |
|
| 58 | - public function getName() : string |
|
| 59 | - { |
|
| 60 | - if (!$this->useAlternateName) { |
|
| 61 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 62 | - } else { |
|
| 63 | - return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 64 | - } |
|
| 65 | - } |
|
| 66 | - |
|
| 67 | - /** |
|
| 68 | - * Returns the plural name. |
|
| 69 | - * |
|
| 70 | - * @return string |
|
| 71 | - */ |
|
| 72 | - private function getPluralName() : string |
|
| 73 | - { |
|
| 74 | - if (!$this->useAlternateName) { |
|
| 75 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 76 | - } else { |
|
| 77 | - return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 78 | - } |
|
| 79 | - } |
|
| 80 | - |
|
| 81 | - /** |
|
| 82 | - * Returns the singular name. |
|
| 83 | - * |
|
| 84 | - * @return string |
|
| 85 | - */ |
|
| 86 | - private function getSingularName() : string |
|
| 87 | - { |
|
| 88 | - if (!$this->useAlternateName) { |
|
| 89 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
| 90 | - } else { |
|
| 91 | - return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 92 | - } |
|
| 93 | - } |
|
| 94 | - |
|
| 95 | - /** |
|
| 96 | - * Returns the code of the method. |
|
| 97 | - * |
|
| 98 | - * @return string |
|
| 99 | - */ |
|
| 100 | - public function getCode() : string |
|
| 101 | - { |
|
| 102 | - $singularName = $this->getSingularName(); |
|
| 103 | - $pluralName = $this->getPluralName(); |
|
| 104 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 105 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 106 | - $pluralVariableName = $variableName.'s'; |
|
| 107 | - |
|
| 108 | - $str = ' /** |
|
| 10 | + /** |
|
| 11 | + * @var Table |
|
| 12 | + */ |
|
| 13 | + private $pivotTable; |
|
| 14 | + |
|
| 15 | + private $useAlternateName = false; |
|
| 16 | + |
|
| 17 | + /** |
|
| 18 | + * @var ForeignKeyConstraint |
|
| 19 | + */ |
|
| 20 | + private $localFk; |
|
| 21 | + |
|
| 22 | + /** |
|
| 23 | + * @var ForeignKeyConstraint |
|
| 24 | + */ |
|
| 25 | + private $remoteFk; |
|
| 26 | + /** |
|
| 27 | + * @var NamingStrategyInterface |
|
| 28 | + */ |
|
| 29 | + private $namingStrategy; |
|
| 30 | + |
|
| 31 | + /** |
|
| 32 | + * @param Table $pivotTable The pivot table |
|
| 33 | + * @param ForeignKeyConstraint $localFk |
|
| 34 | + * @param ForeignKeyConstraint $remoteFk |
|
| 35 | + * @param NamingStrategyInterface $namingStrategy |
|
| 36 | + */ |
|
| 37 | + public function __construct(Table $pivotTable, ForeignKeyConstraint $localFk, ForeignKeyConstraint $remoteFk, NamingStrategyInterface $namingStrategy) |
|
| 38 | + { |
|
| 39 | + $this->pivotTable = $pivotTable; |
|
| 40 | + $this->localFk = $localFk; |
|
| 41 | + $this->remoteFk = $remoteFk; |
|
| 42 | + $this->namingStrategy = $namingStrategy; |
|
| 43 | + } |
|
| 44 | + |
|
| 45 | + /** |
|
| 46 | + * Requests the use of an alternative name for this method. |
|
| 47 | + */ |
|
| 48 | + public function useAlternativeName() |
|
| 49 | + { |
|
| 50 | + $this->useAlternateName = true; |
|
| 51 | + } |
|
| 52 | + |
|
| 53 | + /** |
|
| 54 | + * Returns the name of the method to be generated. |
|
| 55 | + * |
|
| 56 | + * @return string |
|
| 57 | + */ |
|
| 58 | + public function getName() : string |
|
| 59 | + { |
|
| 60 | + if (!$this->useAlternateName) { |
|
| 61 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 62 | + } else { |
|
| 63 | + return 'get'.TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 64 | + } |
|
| 65 | + } |
|
| 66 | + |
|
| 67 | + /** |
|
| 68 | + * Returns the plural name. |
|
| 69 | + * |
|
| 70 | + * @return string |
|
| 71 | + */ |
|
| 72 | + private function getPluralName() : string |
|
| 73 | + { |
|
| 74 | + if (!$this->useAlternateName) { |
|
| 75 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()); |
|
| 76 | + } else { |
|
| 77 | + return TDBMDaoGenerator::toCamelCase($this->remoteFk->getForeignTableName()).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 78 | + } |
|
| 79 | + } |
|
| 80 | + |
|
| 81 | + /** |
|
| 82 | + * Returns the singular name. |
|
| 83 | + * |
|
| 84 | + * @return string |
|
| 85 | + */ |
|
| 86 | + private function getSingularName() : string |
|
| 87 | + { |
|
| 88 | + if (!$this->useAlternateName) { |
|
| 89 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())); |
|
| 90 | + } else { |
|
| 91 | + return TDBMDaoGenerator::toCamelCase(TDBMDaoGenerator::toSingular($this->remoteFk->getForeignTableName())).'By'.TDBMDaoGenerator::toCamelCase($this->pivotTable->getName()); |
|
| 92 | + } |
|
| 93 | + } |
|
| 94 | + |
|
| 95 | + /** |
|
| 96 | + * Returns the code of the method. |
|
| 97 | + * |
|
| 98 | + * @return string |
|
| 99 | + */ |
|
| 100 | + public function getCode() : string |
|
| 101 | + { |
|
| 102 | + $singularName = $this->getSingularName(); |
|
| 103 | + $pluralName = $this->getPluralName(); |
|
| 104 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 105 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 106 | + $pluralVariableName = $variableName.'s'; |
|
| 107 | + |
|
| 108 | + $str = ' /** |
|
| 109 | 109 | * Returns the list of %s associated to this bean via the %s pivot table. |
| 110 | 110 | * |
| 111 | 111 | * @return %s[] |
@@ -116,9 +116,9 @@ discard block |
||
| 116 | 116 | } |
| 117 | 117 | '; |
| 118 | 118 | |
| 119 | - $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
| 119 | + $getterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralName, var_export($this->remoteFk->getLocalTableName(), true)); |
|
| 120 | 120 | |
| 121 | - $str = ' /** |
|
| 121 | + $str = ' /** |
|
| 122 | 122 | * Adds a relationship with %s associated to this bean via the %s pivot table. |
| 123 | 123 | * |
| 124 | 124 | * @param %s %s |
@@ -129,9 +129,9 @@ discard block |
||
| 129 | 129 | } |
| 130 | 130 | '; |
| 131 | 131 | |
| 132 | - $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 132 | + $adderCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 133 | 133 | |
| 134 | - $str = ' /** |
|
| 134 | + $str = ' /** |
|
| 135 | 135 | * Deletes the relationship with %s associated to this bean via the %s pivot table. |
| 136 | 136 | * |
| 137 | 137 | * @param %s %s |
@@ -142,9 +142,9 @@ discard block |
||
| 142 | 142 | } |
| 143 | 143 | '; |
| 144 | 144 | |
| 145 | - $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 145 | + $removerCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 146 | 146 | |
| 147 | - $str = ' /** |
|
| 147 | + $str = ' /** |
|
| 148 | 148 | * Returns whether this bean is associated with %s via the %s pivot table. |
| 149 | 149 | * |
| 150 | 150 | * @param %s %s |
@@ -156,9 +156,9 @@ discard block |
||
| 156 | 156 | } |
| 157 | 157 | '; |
| 158 | 158 | |
| 159 | - $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 159 | + $hasCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $variableName, $singularName, $remoteBeanName, $variableName, var_export($this->remoteFk->getLocalTableName(), true), $variableName); |
|
| 160 | 160 | |
| 161 | - $str = ' /** |
|
| 161 | + $str = ' /** |
|
| 162 | 162 | * Sets all relationships with %s associated to this bean via the %s pivot table. |
| 163 | 163 | * Exiting relationships will be removed and replaced by the provided relationships. |
| 164 | 164 | * |
@@ -170,38 +170,38 @@ discard block |
||
| 170 | 170 | } |
| 171 | 171 | '; |
| 172 | 172 | |
| 173 | - $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
| 174 | - |
|
| 175 | - $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 176 | - |
|
| 177 | - return $code; |
|
| 178 | - } |
|
| 179 | - |
|
| 180 | - /** |
|
| 181 | - * Returns an array of classes that needs a "use" for this method. |
|
| 182 | - * |
|
| 183 | - * @return string[] |
|
| 184 | - */ |
|
| 185 | - public function getUsedClasses() : array |
|
| 186 | - { |
|
| 187 | - return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
| 188 | - } |
|
| 189 | - |
|
| 190 | - /** |
|
| 191 | - * Returns the code to past in jsonSerialize. |
|
| 192 | - * |
|
| 193 | - * @return string |
|
| 194 | - */ |
|
| 195 | - public function getJsonSerializeCode() : string |
|
| 196 | - { |
|
| 197 | - $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 198 | - $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 199 | - |
|
| 200 | - return ' if (!$stopRecursion) { |
|
| 173 | + $setterCode = sprintf($str, $remoteBeanName, $this->pivotTable->getName(), $remoteBeanName, $pluralVariableName, $pluralName, $pluralVariableName, var_export($this->remoteFk->getLocalTableName(), true), $pluralVariableName); |
|
| 174 | + |
|
| 175 | + $code = $getterCode.$adderCode.$removerCode.$hasCode.$setterCode; |
|
| 176 | + |
|
| 177 | + return $code; |
|
| 178 | + } |
|
| 179 | + |
|
| 180 | + /** |
|
| 181 | + * Returns an array of classes that needs a "use" for this method. |
|
| 182 | + * |
|
| 183 | + * @return string[] |
|
| 184 | + */ |
|
| 185 | + public function getUsedClasses() : array |
|
| 186 | + { |
|
| 187 | + return [$this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName())]; |
|
| 188 | + } |
|
| 189 | + |
|
| 190 | + /** |
|
| 191 | + * Returns the code to past in jsonSerialize. |
|
| 192 | + * |
|
| 193 | + * @return string |
|
| 194 | + */ |
|
| 195 | + public function getJsonSerializeCode() : string |
|
| 196 | + { |
|
| 197 | + $remoteBeanName = $this->namingStrategy->getBeanClassName($this->remoteFk->getForeignTableName()); |
|
| 198 | + $variableName = '$'.TDBMDaoGenerator::toVariableName($remoteBeanName); |
|
| 199 | + |
|
| 200 | + return ' if (!$stopRecursion) { |
|
| 201 | 201 | $array[\''.lcfirst($this->getPluralName()).'\'] = array_map(function ('.$remoteBeanName.' '.$variableName.') { |
| 202 | 202 | return '.$variableName.'->jsonSerialize(true); |
| 203 | 203 | }, $this->'.$this->getName().'()); |
| 204 | 204 | } |
| 205 | 205 | '; |
| 206 | - } |
|
| 206 | + } |
|
| 207 | 207 | } |
@@ -7,28 +7,28 @@ |
||
| 7 | 7 | |
| 8 | 8 | class GeneratorEventDispatcher implements GeneratorListenerInterface |
| 9 | 9 | { |
| 10 | - /** |
|
| 11 | - * @var GeneratorListenerInterface[] |
|
| 12 | - */ |
|
| 13 | - private $listeners; |
|
| 10 | + /** |
|
| 11 | + * @var GeneratorListenerInterface[] |
|
| 12 | + */ |
|
| 13 | + private $listeners; |
|
| 14 | 14 | |
| 15 | - /** |
|
| 16 | - * GeneratorEventDispatcher constructor. |
|
| 17 | - * @param GeneratorListenerInterface[] $listeners |
|
| 18 | - */ |
|
| 19 | - public function __construct(array $listeners) |
|
| 20 | - { |
|
| 21 | - $this->listeners = $listeners; |
|
| 22 | - } |
|
| 15 | + /** |
|
| 16 | + * GeneratorEventDispatcher constructor. |
|
| 17 | + * @param GeneratorListenerInterface[] $listeners |
|
| 18 | + */ |
|
| 19 | + public function __construct(array $listeners) |
|
| 20 | + { |
|
| 21 | + $this->listeners = $listeners; |
|
| 22 | + } |
|
| 23 | 23 | |
| 24 | - /** |
|
| 25 | - * @param ConfigurationInterface $configuration |
|
| 26 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 27 | - */ |
|
| 28 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 29 | - { |
|
| 30 | - foreach ($this->listeners as $listener) { |
|
| 31 | - $listener->onGenerate($configuration, $beanDescriptors); |
|
| 32 | - } |
|
| 33 | - } |
|
| 24 | + /** |
|
| 25 | + * @param ConfigurationInterface $configuration |
|
| 26 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 27 | + */ |
|
| 28 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 29 | + { |
|
| 30 | + foreach ($this->listeners as $listener) { |
|
| 31 | + $listener->onGenerate($configuration, $beanDescriptors); |
|
| 32 | + } |
|
| 33 | + } |
|
| 34 | 34 | } |
@@ -11,12 +11,12 @@ |
||
| 11 | 11 | class VoidListener implements GeneratorListenerInterface |
| 12 | 12 | { |
| 13 | 13 | |
| 14 | - /** |
|
| 15 | - * @param ConfigurationInterface $configuration |
|
| 16 | - * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 17 | - */ |
|
| 18 | - public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 19 | - { |
|
| 20 | - // Let's do nothing. |
|
| 21 | - } |
|
| 14 | + /** |
|
| 15 | + * @param ConfigurationInterface $configuration |
|
| 16 | + * @param BeanDescriptorInterface[] $beanDescriptors |
|
| 17 | + */ |
|
| 18 | + public function onGenerate(ConfigurationInterface $configuration, array $beanDescriptors): void |
|
| 19 | + { |
|
| 20 | + // Let's do nothing. |
|
| 21 | + } |
|
| 22 | 22 | } |