@@ -99,7 +99,7 @@ public function __construct(?string $tableName = null, array $primaryKeys = [],
9999 $ this ->_setStatus (TDBMObjectStateEnum::STATE_DETACHED );
100100 } else {
101101 $ this ->_attach ($ tdbmService );
102- if (!empty ($ primaryKeys )) {
102+ if (!empty ($ primaryKeys )) { // @TODO (gua) might not be fully loaded
103103 $ this ->_setStatus (TDBMObjectStateEnum::STATE_NOT_LOADED );
104104 } else {
105105 $ this ->_setStatus (TDBMObjectStateEnum::STATE_NEW );
@@ -110,7 +110,7 @@ public function __construct(?string $tableName = null, array $primaryKeys = [],
110110 /**
111111 * Alternative constructor called when data is fetched from database via a SELECT.
112112 *
113- * @param array[] $beanData array<table, array<column, value>>
113+ * @param array<string, array<string, mixed>> $beanData array<table, array<column, value>>
114114 * @param TDBMService $tdbmService
115115 */
116116 public function _constructFromData (array $ beanData , TDBMService $ tdbmService ): void
@@ -121,7 +121,7 @@ public function _constructFromData(array $beanData, TDBMService $tdbmService): v
121121 $ this ->dbRows [$ table ] = new DbRow ($ this , $ table , static ::getForeignKeys ($ table ), $ tdbmService ->_getPrimaryKeysFromObjectData ($ table , $ columns ), $ tdbmService , $ columns );
122122 }
123123
124- $ this ->status = TDBMObjectStateEnum::STATE_LOADED ;
124+ $ this ->status = TDBMObjectStateEnum::STATE_LOADED ; // @TODO might be not fully loaded
125125 }
126126
127127 /**
@@ -251,27 +251,14 @@ protected function set(string $var, $value, ?string $tableName = null): void
251251 }
252252 }
253253
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
259255 {
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. ' ));
270257 if (!isset ($ this ->dbRows [$ tableName ])) {
271258 $ this ->registerTable ($ tableName );
272259 }
273260
274- $ oldLinkedBean = $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName );
261+ $ oldLinkedBean = $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName, $ className , $ resultIteratorClass );
275262 if ($ oldLinkedBean !== null ) {
276263 $ oldLinkedBean ->removeManyToOneRelationship ($ tableName , $ foreignKeyName , $ this );
277264 }
@@ -291,19 +278,19 @@ protected function setRef(string $foreignKeyName, AbstractTDBMObject $bean = nul
291278 *
292279 * @return AbstractTDBMObject|null
293280 */
294- protected function getRef (string $ foreignKeyName , ? string $ tableName = null ) : ?AbstractTDBMObject
281+ protected function getRef (string $ foreignKeyName , string $ tableName, string $ className , string $ resultIteratorClass ) : ?AbstractTDBMObject
295282 {
296283 $ tableName = $ this ->checkTableName ($ tableName );
297284
298285 if (!isset ($ this ->dbRows [$ tableName ])) {
299286 return null ;
300287 }
301288
302- return $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName );
289+ return $ this ->dbRows [$ tableName ]->getRef ($ foreignKeyName, $ className , $ resultIteratorClass );
303290 }
304291
305292 /**
306- * Adds a many to many relationship to this bean.
293+ * Adds a many to many$table relationship to this bean.
307294 *
308295 * @param string $pivotTableName
309296 * @param AbstractTDBMObject $remoteBean
@@ -525,15 +512,16 @@ private function removeManyToOneRelationship(string $tableName, string $foreignK
525512 *
526513 * @return AlterableResultIterator
527514 */
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
529516 {
517+ assert (is_a ($ resultIteratorClass , ResultIterator::class, true ), new TDBMInvalidArgumentException ('$resultIteratorClass should be a ` ' . ResultIterator::class. '`. ` ' . $ resultIteratorClass . '` provided. ' ));
530518 $ key = $ tableName .'___ ' .$ foreignKeyName ;
531519 $ alterableResultIterator = $ this ->getManyToOneAlterableResultIterator ($ tableName , $ foreignKeyName );
532520 if ($ this ->status === TDBMObjectStateEnum::STATE_DETACHED || $ this ->status === TDBMObjectStateEnum::STATE_NEW || (isset ($ this ->manyToOneRelationships [$ key ]) && $ this ->manyToOneRelationships [$ key ]->getUnderlyingResultIterator () !== null )) {
533521 return $ alterableResultIterator ;
534522 }
535523
536- $ unalteredResultIterator = $ this ->tdbmService ->findObjects ($ tableName , $ searchFilter , [], $ orderString );
524+ $ unalteredResultIterator = $ this ->tdbmService ->findObjects ($ tableName , $ searchFilter , [], $ orderString, [], null , null , $ resultIteratorClass );
537525
538526 $ alterableResultIterator ->setResultIterator ($ unalteredResultIterator ->getIterator ());
539527
0 commit comments