@@ -99,7 +99,7 @@ public function __construct(?string $tableName = null, array $primaryKeys = [],
99
99
$ this ->_setStatus (TDBMObjectStateEnum::STATE_DETACHED );
100
100
} else {
101
101
$ this ->_attach ($ tdbmService );
102
- if (!empty ($ primaryKeys )) {
102
+ if (!empty ($ primaryKeys )) { // @TODO (gua) might not be fully loaded
103
103
$ this ->_setStatus (TDBMObjectStateEnum::STATE_NOT_LOADED );
104
104
} else {
105
105
$ this ->_setStatus (TDBMObjectStateEnum::STATE_NEW );
@@ -110,7 +110,7 @@ public function __construct(?string $tableName = null, array $primaryKeys = [],
110
110
/**
111
111
* Alternative constructor called when data is fetched from database via a SELECT.
112
112
*
113
- * @param array[] $beanData array<table, array<column, value>>
113
+ * @param array<string, array<string, mixed>> $beanData array<table, array<column, value>>
114
114
* @param TDBMService $tdbmService
115
115
*/
116
116
public function _constructFromData (array $ beanData , TDBMService $ tdbmService ): void
@@ -121,7 +121,7 @@ public function _constructFromData(array $beanData, TDBMService $tdbmService): v
121
121
$ this ->dbRows [$ table ] = new DbRow ($ this , $ table , static ::getForeignKeys ($ table ), $ tdbmService ->_getPrimaryKeysFromObjectData ($ table , $ columns ), $ tdbmService , $ columns );
122
122
}
123
123
124
- $ this ->status = TDBMObjectStateEnum::STATE_LOADED ;
124
+ $ this ->status = TDBMObjectStateEnum::STATE_LOADED ; // @TODO might be not fully loaded
125
125
}
126
126
127
127
/**
@@ -251,27 +251,14 @@ protected function set(string $var, $value, ?string $tableName = null): void
251
251
}
252
252
}
253
253
254
- /**
255
- * @param string $foreignKeyName
256
- * @param AbstractTDBMObject $bean
257
- */
258
- protected function setRef (string $ foreignKeyName , AbstractTDBMObject $ bean = null , string $ tableName = null ): void
254
+ protected function setRef (string $ foreignKeyName , ?AbstractTDBMObject $ bean , string $ tableName , string $ className , string $ resultIteratorClass ): void
259
255
{
260
- if ($ tableName === null ) {
261
- if (count ($ this ->dbRows ) > 1 ) {
262
- throw new TDBMException ('This object is based on several tables. You must specify which table you are retrieving data from. ' );
263
- } elseif (count ($ this ->dbRows ) === 1 ) {
264
- $ tableName = (string ) array_keys ($ this ->dbRows )[0 ];
265
- } else {
266
- throw new TDBMException ('Please specify a table for this object. ' );
267
- }
268
- }
269
-
256
+ assert ($ bean === null || is_a ($ bean , $ className ), new TDBMInvalidArgumentException ('$bean should be `null` or ` ' . $ className . '`. ` ' . ($ bean === null ? 'null ' : get_class ($ bean )) . '` provided. ' ));
270
257
if (!isset ($ this ->dbRows [$ tableName ])) {
271
258
$ this ->registerTable ($ tableName );
272
259
}
273
260
274
- $ oldLinkedBean = $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName );
261
+ $ oldLinkedBean = $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName, $ className , $ resultIteratorClass );
275
262
if ($ oldLinkedBean !== null ) {
276
263
$ oldLinkedBean ->removeManyToOneRelationship ($ tableName , $ foreignKeyName , $ this );
277
264
}
@@ -291,19 +278,19 @@ protected function setRef(string $foreignKeyName, AbstractTDBMObject $bean = nul
291
278
*
292
279
* @return AbstractTDBMObject|null
293
280
*/
294
- protected function getRef (string $ foreignKeyName , ? string $ tableName = null ) : ?AbstractTDBMObject
281
+ protected function getRef (string $ foreignKeyName , string $ tableName, string $ className , string $ resultIteratorClass ) : ?AbstractTDBMObject
295
282
{
296
283
$ tableName = $ this ->checkTableName ($ tableName );
297
284
298
285
if (!isset ($ this ->dbRows [$ tableName ])) {
299
286
return null ;
300
287
}
301
288
302
- return $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName );
289
+ return $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName, $ className , $ resultIteratorClass );
303
290
}
304
291
305
292
/**
306
- * Adds a many to many relationship to this bean.
293
+ * Adds a many to many$table relationship to this bean.
307
294
*
308
295
* @param string $pivotTableName
309
296
* @param AbstractTDBMObject $remoteBean
@@ -525,15 +512,16 @@ private function removeManyToOneRelationship(string $tableName, string $foreignK
525
512
*
526
513
* @return AlterableResultIterator
527
514
*/
528
- protected function retrieveManyToOneRelationshipsStorage (string $ tableName , string $ foreignKeyName , array $ searchFilter , string $ orderString = null ) : AlterableResultIterator
515
+ protected function retrieveManyToOneRelationshipsStorage (string $ tableName , string $ foreignKeyName , array $ searchFilter , ? string $ orderString, string $ resultIteratorClass ) : AlterableResultIterator
529
516
{
517
+ assert (is_a ($ resultIteratorClass , ResultIterator::class, true ), new TDBMInvalidArgumentException ('$resultIteratorClass should be a ` ' . ResultIterator::class. '`. ` ' . $ resultIteratorClass . '` provided. ' ));
530
518
$ key = $ tableName .'___ ' .$ foreignKeyName ;
531
519
$ alterableResultIterator = $ this ->getManyToOneAlterableResultIterator ($ tableName , $ foreignKeyName );
532
520
if ($ this ->status === TDBMObjectStateEnum::STATE_DETACHED || $ this ->status === TDBMObjectStateEnum::STATE_NEW || (isset ($ this ->manyToOneRelationships [$ key ]) && $ this ->manyToOneRelationships [$ key ]->getUnderlyingResultIterator () !== null )) {
533
521
return $ alterableResultIterator ;
534
522
}
535
523
536
- $ unalteredResultIterator = $ this ->tdbmService ->findObjects ($ tableName , $ searchFilter , [], $ orderString );
524
+ $ unalteredResultIterator = $ this ->tdbmService ->findObjects ($ tableName , $ searchFilter , [], $ orderString, [], null , null , $ resultIteratorClass );
537
525
538
526
$ alterableResultIterator ->setResultIterator ($ unalteredResultIterator ->getIterator ());
539
527
0 commit comments