@@ -86,8 +86,7 @@ public function __clone()
8686 * ```
8787 *
8888 * @param string $relationName the relation name. This refers to a relation declared in {@see primaryModel}.
89- * @param callable|null $callable $callable a PHP callback for customizing the relation associated with the junction
90- * table.
89+ * @param callable|null $callable a PHP callback for customizing the relation associated with the junction table.
9190 * Its signature should be `function($query)`, where `$query` is the query to be customized.
9291 *
9392 * @return static the relation object itself.
@@ -98,7 +97,7 @@ public function via(string $relationName, callable $callable = null): static
9897 $ callableUsed = $ callable !== null ;
9998 $ this ->via = [$ relationName , $ relation , $ callableUsed ];
10099
101- if ($ callable !== null ) {
100+ if ($ callableUsed ) {
102101 $ callable ($ relation );
103102 }
104103
@@ -164,13 +163,10 @@ public function inverseOf(string $relationName): static
164163 }
165164
166165 /**
167- * Finds the related records for the specified primary record .
166+ * Returns query records depends on {@see $multiple} .
168167 *
169168 * This method is invoked when a relation of an ActiveRecord is being accessed in a lazy fashion.
170169 *
171- * @param string $name the relation name.
172- * @param ActiveRecordInterface $model the primary model.
173- *
174170 * @throws Exception
175171 * @throws InvalidArgumentException
176172 * @throws InvalidConfigException
@@ -612,9 +608,19 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
612608 {
613609 $ key = [];
614610
615- foreach ($ attributes as $ attribute ) {
616- if (isset ($ activeRecord [$ attribute ]) || (is_object ($ activeRecord ) && property_exists ($ activeRecord , $ attribute ))) {
617- $ key [] = $ this ->normalizeModelKey ($ activeRecord [$ attribute ]);
611+ if (is_array ($ activeRecord )) {
612+ foreach ($ attributes as $ attribute ) {
613+ if (isset ($ activeRecord [$ attribute ])) {
614+ $ key [] = $ this ->normalizeModelKey ($ activeRecord [$ attribute ]);
615+ }
616+ }
617+ } else {
618+ foreach ($ attributes as $ attribute ) {
619+ $ value = $ activeRecord ->getAttribute ($ attribute );
620+
621+ if ($ value !== null ) {
622+ $ key [] = $ this ->normalizeModelKey ($ value );
623+ }
618624 }
619625 }
620626
@@ -628,9 +634,11 @@ private function getModelKey(ActiveRecordInterface|array $activeRecord, array $a
628634 }
629635
630636 /**
637+ * @param int|string|Stringable|null $value raw key value.
638+ *
631639 * @return int|string|null normalized key value.
632640 */
633- private function normalizeModelKey (mixed $ value ): int |string |null
641+ private function normalizeModelKey (int | string | Stringable | null $ value ): int |string |null
634642 {
635643 if ($ value instanceof Stringable) {
636644 /**
0 commit comments