lib/components/types/Relationship.php
public function getPrimaryObject($primaryObject, $relatedObject, $role)
    {
        if (!$this->handlePrimary) {
            return false;
        }
        if ($role === 'child') {
            $primaryField = 'primary_child';
            if (!$relatedObject->objectType->getPrimaryAsChild($this->parent)) {
                // \d(['bad', $this->systemId, get_class($primaryObject), get_class($relatedObject), $role]);
                return false;
            }
            $primaryParent = $primaryObject;
        } else {
            $primaryField = 'primary_parent';
            if (!$relatedObject->objectType->getPrimaryAsParent($this->child)) {
                // \d(['bad', $this->systemId, get_class($primaryObject), get_class($relatedObject), $role]);
                return false;
            }
            $primaryParent = $relatedObject;
        }

        $key = json_encode([__FUNCTION__, $this->systemId, $primaryObject->primaryKey]);
        if (!isset(self::$_cache[$key])) {
            self::$_cache[$key] = null;
            $relationClass = Yii::$app->classes['Relation'];
            $childClass = $this->child->primaryModel;
            $relation = $relationClass::find();
            $alias = $relationClass::tableName();
            $relation->andWhere(['`' . $alias . '`.`parent_object_id`' => $primaryParent->primaryKey, '`' . $alias . '`.`' . $primaryField . '`' => 1]);
            $relation->andWhere(['or', '`' . $alias . '`.`child_object_id` LIKE :prefix']); //, '`'. $alias.'`.`child_object_id` LIKE \''.$childClass.'\''
            $relation->params[':prefix'] = $childClass::modelPrefix() . '-%';
            $primaryObject->addActiveConditions($relation, $alias);
            // \d([$this->systemId, $relation->createCommand()->rawSql, $primaryField, $role]);
            $relation = $relation->one();
            if (!empty($relation)) {
                self::$_cache[$key] = $relation;
            }
        }

        return self::$_cache[$key];
    }
public function getPrimaryObject($primaryObject, $relatedObject, $role)
    {
        if (!$this->handlePrimary) {
            return false;
        }
        if ($role === 'child') {
            $primaryField = 'primary_child';
            if (!$relatedObject->objectType->getPrimaryAsChild($this->parent)) {
                // \d(['bad', $this->systemId, get_class($primaryObject), get_class($relatedObject), $role]);
                return false;
            }
            $primaryParent = $primaryObject;
        } else {
            $primaryField = 'primary_parent';
            if (!$relatedObject->objectType->getPrimaryAsParent($this->child)) {
                // \d(['bad', $this->systemId, get_class($primaryObject), get_class($relatedObject), $role]);
                return false;
            }
            $primaryParent = $relatedObject;
        }

        $key = json_encode([__FUNCTION__, $this->systemId, $primaryObject->primaryKey]);
        if (!isset(self::$_cache[$key])) {
            self::$_cache[$key] = null;
            $relationClass = Yii::$app->classes['Relation'];
            $childClass = $this->child->primaryModel;
            $relation = $relationClass::find();
            $alias = $relationClass::tableName();
            $relation->andWhere(['`' . $alias . '`.`parent_object_id`' => $primaryParent->primaryKey, '`' . $alias . '`.`' . $primaryField . '`' => 1]);
            $relation->andWhere(['or', '`' . $alias . '`.`child_object_id` LIKE :prefix']); //, '`'. $alias.'`.`child_object_id` LIKE \''.$childClass.'\''
            $relation->params[':prefix'] = $childClass::modelPrefix() . '-%';
            $primaryObject->addActiveConditions($relation, $alias);
            // \d([$this->systemId, $relation->createCommand()->rawSql, $primaryField, $role]);
            $relation = $relation->one();
            if (!empty($relation)) {
                self::$_cache[$key] = $relation;
            }
        }

        return self::$_cache[$key];
    }
Get primary object.

@param [[@doctodo param_type:primaryObject]] $primaryObject [[@doctodo param_description:primaryObject]]
@param [[@doctodo param_type:relatedObject]] $relatedObject [[@doctodo param_description:relatedObject]]
@param [[@doctodo param_type:role]]          $role          [[@doctodo param_description:role]]

@return [[@doctodo return_type:getPrimaryObject]] [[@doctodo return_description:getPrimaryObject]]