src/Leevel/Database/Ddd/Entity.php
public function loadRelation(string $prop): Relation
    {
        $prop = $this->normalize($prop);

        $this->validate($prop);

        $defined = static::STRUCT[$prop];

        if (isset($defined[self::BELONGS_TO])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->belongsTo(
               $defined[self::BELONGS_TO],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::HAS_MANY])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->hasMany(
               $defined[self::HAS_MANY],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::HAS_ONE])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->hasOne(
               $defined[self::HAS_ONE],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::MANY_MANY])) {
            $this->validateRelationDefined($defined, [
                'middle_entity', 'source_key', 'target_key',
                'middle_target_key', 'middle_source_key',
            ]);

            $relation = $this->ManyMany(
               $defined[self::MANY_MANY],
               $defined['middle_entity'],
               $defined['target_key'],
               $defined['source_key'],
               $defined['middle_target_key'],
               $defined['middle_source_key']
           );
        }

        if (isset($defined[self::SCOPE])) {
            call_user_func([$this, 'scope'.ucfirst($defined[self::SCOPE])], $relation);
        }

        return $relation;
    }
public function loadRelation(string $prop): Relation
    {
        $prop = $this->normalize($prop);

        $this->validate($prop);

        $defined = static::STRUCT[$prop];

        if (isset($defined[self::BELONGS_TO])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->belongsTo(
               $defined[self::BELONGS_TO],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::HAS_MANY])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->hasMany(
               $defined[self::HAS_MANY],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::HAS_ONE])) {
            $this->validateRelationDefined($defined, ['source_key', 'target_key']);

            $relation = $this->hasOne(
               $defined[self::HAS_ONE],
               $defined['target_key'],
               $defined['source_key']
           );
        } elseif (isset($defined[self::MANY_MANY])) {
            $this->validateRelationDefined($defined, [
                'middle_entity', 'source_key', 'target_key',
                'middle_target_key', 'middle_source_key',
            ]);

            $relation = $this->ManyMany(
               $defined[self::MANY_MANY],
               $defined['middle_entity'],
               $defined['target_key'],
               $defined['source_key'],
               $defined['middle_target_key'],
               $defined['middle_source_key']
           );
        }

        if (isset($defined[self::SCOPE])) {
            call_user_func([$this, 'scope'.ucfirst($defined[self::SCOPE])], $relation);
        }

        return $relation;
    }
读取关联.

@param string $prop

@return \Leevel\Database\Ddd\Relation\Relation