23
23
24
24
use JsonSerializable ;
25
25
use TheCodingMachine \TDBM \Schema \ForeignKeys ;
26
+ use TheCodingMachine \TDBM \Utils \ManyToManyRelationshipPathDescriptor ;
26
27
27
28
/**
28
29
* Instances of this class represent a "bean". Usually, a bean is mapped to a row of one table.
@@ -315,14 +316,12 @@ protected function addRelationship(string $pivotTableName, AbstractTDBMObject $r
315
316
/**
316
317
* Returns true if there is a relationship to this bean.
317
318
*
318
- * @param string $pivotTableName
319
- * @param AbstractTDBMObject $remoteBean
320
- *
321
319
* @return bool
322
320
*/
323
- protected function hasRelationship (string $ pivotTableName , AbstractTDBMObject $ remoteBean ): bool
321
+ protected function hasRelationship (string $ pathKey , AbstractTDBMObject $ remoteBean ): bool
324
322
{
325
- $ storage = $ this ->retrieveRelationshipsStorage ($ pivotTableName );
323
+ $ pathModel = $ this ->_getManyToManyRelationshipDescriptor ($ pathKey );
324
+ $ storage = $ this ->retrieveRelationshipsStorage ($ pathModel );
326
325
327
326
if ($ storage ->contains ($ remoteBean )) {
328
327
if ($ storage [$ remoteBean ]['status ' ] !== 'delete ' ) {
@@ -353,12 +352,13 @@ public function _removeRelationship(string $pivotTableName, AbstractTDBMObject $
353
352
* Sets many to many relationships for this bean.
354
353
* Adds new relationships and removes unused ones.
355
354
*
356
- * @param string $pivotTableName
357
355
* @param AbstractTDBMObject[] $remoteBeans
358
356
*/
359
- protected function setRelationships (string $ pivotTableName , array $ remoteBeans ): void
357
+ protected function setRelationships (string $ pathKey , array $ remoteBeans ): void
360
358
{
361
- $ storage = $ this ->retrieveRelationshipsStorage ($ pivotTableName );
359
+ $ pathModel = $ this ->_getManyToManyRelationshipDescriptor ($ pathKey );
360
+ $ pivotTableName = $ pathModel ->getPivotName ();
361
+ $ storage = $ this ->retrieveRelationshipsStorage ($ pathModel );
362
362
363
363
foreach ($ storage as $ oldRemoteBean ) {
364
364
/* @var $oldRemoteBean AbstractTDBMObject */
@@ -379,18 +379,18 @@ protected function setRelationships(string $pivotTableName, array $remoteBeans):
379
379
/**
380
380
* Returns the list of objects linked to this bean via $pivotTableName.
381
381
*
382
- * @param string $pivotTableName
383
- *
384
382
* @return \SplObjectStorage
385
383
*/
386
- private function retrieveRelationshipsStorage (string $ pivotTableName ): \SplObjectStorage
384
+ private function retrieveRelationshipsStorage (ManyToManyRelationshipPathDescriptor $ pathModel ): \SplObjectStorage
387
385
{
388
- $ storage = $ this ->getRelationshipStorage ($ pivotTableName );
386
+ $ pivotTableName = $ pathModel ->getPivotName ();
387
+
388
+ $ storage = $ this ->getRelationshipStorage ($ pathModel ->getPivotName ());
389
389
if ($ this ->status === TDBMObjectStateEnum::STATE_DETACHED || $ this ->status === TDBMObjectStateEnum::STATE_NEW || (isset ($ this ->loadedRelationships [$ pivotTableName ]) && $ this ->loadedRelationships [$ pivotTableName ])) {
390
390
return $ storage ;
391
391
}
392
392
393
- $ beans = $ this ->tdbmService ->_getRelatedBeans ($ pivotTableName , $ this );
393
+ $ beans = $ this ->tdbmService ->_getRelatedBeans ($ pathModel , $ this );
394
394
$ this ->loadedRelationships [$ pivotTableName ] = true ;
395
395
396
396
foreach ($ beans as $ bean ) {
@@ -410,13 +410,20 @@ private function retrieveRelationshipsStorage(string $pivotTableName): \SplObjec
410
410
/**
411
411
* Internal TDBM method. Returns the list of objects linked to this bean via $pivotTableName.
412
412
*
413
- * @param string $pivotTableName
414
- *
415
413
* @return AbstractTDBMObject[]
416
414
*/
417
- public function _getRelationships (string $ pivotTableName ): array
415
+ public function _getRelationships (string $ pathKey ): array
416
+ {
417
+ $ pathModel = $ this ->_getManyToManyRelationshipDescriptor ($ pathKey );
418
+ return $ this ->_getRelationshipsFromModel ($ pathModel );
419
+ }
420
+
421
+ /**
422
+ * @return AbstractTDBMObject[]
423
+ */
424
+ public function _getRelationshipsFromModel (ManyToManyRelationshipPathDescriptor $ pathModel ): array
418
425
{
419
- return $ this ->relationshipStorageToArray ($ this ->retrieveRelationshipsStorage ($ pivotTableName ));
426
+ return $ this ->relationshipStorageToArray ($ this ->retrieveRelationshipsStorage ($ pathModel ));
420
427
}
421
428
422
429
/**
@@ -577,25 +584,6 @@ public function _getStatus() : string
577
584
*/
578
585
public function __clone ()
579
586
{
580
- // Let's clone the many to many relationships
581
- if ($ this ->status === TDBMObjectStateEnum::STATE_DETACHED ) {
582
- $ pivotTableList = array_keys ($ this ->relationships );
583
- } else {
584
- $ pivotTableList = $ this ->tdbmService ->_getPivotTablesLinkedToBean ($ this );
585
- }
586
-
587
- foreach ($ pivotTableList as $ pivotTable ) {
588
- $ storage = $ this ->retrieveRelationshipsStorage ($ pivotTable );
589
-
590
- // Let's duplicate the reverse side of the relationship // This is useless: already done by "retrieveRelationshipsStorage"!!!
591
- /*foreach ($storage as $remoteBean) {
592
- $metadata = $storage[$remoteBean];
593
-
594
- $remoteStorage = $remoteBean->getRelationshipStorage($pivotTable);
595
- $remoteStorage->attach($this, ['status' => $metadata['status'], 'reverse' => !$metadata['reverse']]);
596
- }*/
597
- }
598
-
599
587
// Let's clone each row
600
588
foreach ($ this ->dbRows as $ key => &$ dbRow ) {
601
589
$ dbRow = clone $ dbRow ;
@@ -672,4 +660,17 @@ protected static function getForeignKeys(string $tableName): ForeignKeys
672
660
{
673
661
return new ForeignKeys ([]);
674
662
}
663
+
664
+ public function _getManyToManyRelationshipDescriptor (string $ pathKey ): ManyToManyRelationshipPathDescriptor
665
+ {
666
+ throw new TDBMException ('Could not find many to many relationship descriptor key for " ' .$ pathKey .'" ' );
667
+ }
668
+
669
+ /**
670
+ * @return string[]
671
+ */
672
+ public function _getManyToManyRelationshipDescriptorKeys (): array
673
+ {
674
+ return [];
675
+ }
675
676
}
0 commit comments