October\Rain\Database\Concerns\HasRelationships

Overview

HasRelationships is a concern used by the \October\Rain\Database\Model class, employing a cleaner declaration of model relationships.

The relation definitions uses an almost identical approach to the relation methods defined by Eloquent, instead using class properties to make the class file less cluttered and keep the logic separated from the definition.

Relations should be declared with keys as the relation name and value as a mixed array. The relation type $morphTo does not include a class name as the first value.

Example:

class Order extends Model
{
    protected $hasMany = [
        'items' => Item::class
    ];
}

Public Properties

public array $hasOne

hasOne related record, inverse of belongsTo.

protected $hasOne = [
    'owner' => [User::class, 'key' => 'user_id']
];

public array $hasMany

hasMany related records, inverse of belongsTo.

protected $hasMany = [
    'items' => Item::class
];

public array $belongsTo

belongsTo another record with a local key attribute

protected $belongsTo = [
    'parent' => [Category::class, 'key' => 'parent_id']
];

public array $belongsToMany

belongsToMany to multiple records using a join table.

protected $belongsToMany = [
    'groups' => [Group::class, 'table'=> 'join_groups_users']
];

public array $morphTo

morphTo another record using local key and type attributes

protected $morphTo = [
    'pictures' => []
];

public array $morphOne

morphOne related record, inverse of morphTo.

protected $morphOne = [
    'log' => [History::class, 'name' => 'user']
];

public array $morphMany

morphMany related records, inverse of morphTo.

protected $morphMany = [
    'log' => [History::class, 'name' => 'user']
];

public array $morphToMany

morphToMany to multiple records using a join table.

protected $morphToMany = [
    'tag' => [Tag::class, 'table' => 'tagables', 'name' => 'tagable']
];

public array $morphedByMany

morphedByMany to a polymorphic, inverse many-to-many relationship.

public $morphedByMany = [
    'tag' => [Tag::class, 'table' => 'tagables', 'name' => 'tagable']
];

public array $attachOne

attachOne file attachment.

protected $attachOne = [
    'picture' => [\October\Rain\Database\Attach\File::class, 'public' => false]
];

public array $attachMany

attachMany file attachments.

protected $attachMany = [
    'pictures' => [\October\Rain\Database\Attach\File::class, 'name'=> 'imageable']
];

public array $hasManyThrough

hasManyThrough is related records through another record.

protected $hasManyThrough = [
    'posts' => [Post::class, 'through' => User::class]
];

public array $hasOneThrough

hasOneThrough is a related record through another record.

protected $hasOneThrough = [
    'post' => [Post::class, 'through' => User::class]
];

Protected Properties

protected static array $relationTypes

relationTypes expected, used to cycle and verify relationships.


Public Methods

public attachMany()

public attachMany(
    $related,
    $isPublic = null,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\MorphMany 

attachMany defines an attachment one-to-many relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public attachOne()

public attachOne(
    $related,
    $isPublic = true,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\MorphOne 

attachOne defines an attachment one-to-one relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public belongsTo()

public belongsTo(
    $related,
    $foreignKey = null,
    $parentKey = null,
    $relationName = null
): October\Rain\Database\Relations\BelongsTo 

belongsTo defines an inverse one-to-one or many relationship. Overridden from \Eloquent\Model to allow the usage of the intermediary methods to handle the relationsData array.

public belongsToMany()

public belongsToMany(
    $related,
    $table = null,
    $primaryKey = null,
    $foreignKey = null,
    $parentKey = null,
    $relatedKey = null,
    $relationName = null
): October\Rain\Database\Relations\BelongsToMany 

belongsToMany defines a many-to-many relationship. This code is almost a duplicate of Eloquent but uses a Rain relation class.

public getRelationDefinition()

public getRelationDefinition($name): array 

getRelationDefinition returns relationship details from a supplied name

public getRelationDefinitions()

public getRelationDefinitions(): array 

getRelationDefinitions returns relationship details for all relations defined on this model

public getRelationSimpleValue()

public getRelationSimpleValue($relationName): void

getRelationSimpleValue returns a relation key value(s), not as an object.

public getRelationType()

public getRelationType(string $name): October\Rain\Database\Relation 

getRelationType returns a relationship type based on a supplied name

public hasMany()

public hasMany(
    $related,
    $primaryKey = null,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\HasMany 

hasMany defines a one-to-many relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public hasManyThrough()

public hasManyThrough(
    $related,
    $through,
    $primaryKey = null,
    $throughKey = null,
    $localKey = null,
    $secondLocalKey = null,
    $relationName = null
): October\Rain\Database\Relations\HasManyThrough 

hasManyThrough defines a has-many-through relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public hasOne()

public hasOne(
    $related,
    $primaryKey = null,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\HasOne 

hasOne defines a one-to-one relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public hasOneThrough()

public hasOneThrough(
    $related,
    $through,
    $primaryKey = null,
    $throughKey = null,
    $localKey = null,
    $secondLocalKey = null,
    $relationName = null
): October\Rain\Database\Relations\HasOneThrough 

hasOneThrough define a has-one-through relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public hasRelation()

public hasRelation($name): bool 

hasRelation checks if model has a relationship by supplied name

public isRelationPushable()

public isRelationPushable($name): bool 

isRelationPushable determines whether the specified relation should be saved when push() is called instead of save() on the model. Defaults to true.

public isRelationTypeSingular()

public isRelationTypeSingular($name): bool 

isRelationTypeSingular returns true if the relation is expected to return a single record versus a collection of records.

public makeRelation()

public makeRelation(string $name): Model|null 

makeRelation returns a relation class object, supporting nested relations with dot notation

public morphMany()

public morphMany(
    $related,
    $name,
    $type = null,
    $id = null,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\MorphMany 

morphMany defines a polymorphic one-to-many relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public morphOne()

public morphOne(
    $related,
    $name,
    $type = null,
    $id = null,
    $localKey = null,
    $relationName = null
): October\Rain\Database\Relations\MorphOne 

morphOne defines a polymorphic one-to-one relationship. This code is a duplicate of Eloquent but uses a Rain relation class.

public morphTo()

public morphTo(
    $name = null,
    $type = null,
    $id = null,
    $ownerKey = null
): October\Rain\Database\Relations\BelongsTo 

morphTo defines a polymorphic, inverse one-to-one or many relationship. Overridden from \Eloquent\Model to allow the usage of the intermediary methods to handle the relation.

public morphToMany()

public morphToMany(
    $related,
    $name,
    $table = null,
    $primaryKey = null,
    $foreignKey = null,
    $parentKey = null,
    $relatedKey = null,
    $inverse = false,
    $relationName = null
): October\Rain\Database\Relations\MorphToMany 

morphToMany defines a polymorphic many-to-many relationship. This code is almost a duplicate of Eloquent but uses a Rain relation class.

public morphedByMany()

public morphedByMany(
    $related,
    $name,
    $table = null,
    $primaryKey = null,
    $foreignKey = null,
    $parentKey = null,
    $relatedKey = null,
    $relationName = null
): October\Rain\Database\Relations\MorphToMany 

morphedByMany defines a polymorphic many-to-many inverse relationship. This code is almost a duplicate of Eloquent but uses a Rain relation class.

Protected Methods

protected getRelationCaller()

protected getRelationCaller(): void

getRelationCaller finds the calling function name from the stack trace.

protected getRelationCustomClass()

protected getRelationCustomClass($name): string|null 

getRelationCustomClass returns a custom relation class name for the relation or null if none is found.

protected getRelationDefaults()

protected getRelationDefaults(string $type): array 

getRelationDefaults returns default relation arguments for a given type.

protected handleRelation()

protected handleRelation(
    string $relationName
): Illuminate\Database\Eloquent\Relations\Relation 

handleRelation looks for the relation and does the correct magic as Eloquent would require inside relation methods. For more information, read the documentation of the mentioned property.

protected makeRelationInternal()

protected makeRelationInternal($relationName, $relationClass): void

makeRelationInternal is used internally to create a new related instance. It also fires the afterRelation to extend the created instance.

protected morphEagerTo()

protected morphEagerTo(
    string $name,
    string $type,
    string $id,
    string $ownerKey
): Illuminate\Database\Eloquent\Relations\MorphTo 

morphEagerTo defines a polymorphic, inverse one-to-one or many relationship.

protected morphInstanceTo()

protected morphInstanceTo(
    string $target,
    string $name,
    string $type,
    string $id,
    string $ownerKey
): Illuminate\Database\Eloquent\Relations\MorphTo 

morphInstanceTo defines a polymorphic, inverse one-to-one or many relationship

protected performDeleteOnRelations()

protected performDeleteOnRelations(): void

performDeleteOnRelations locates relations with delete flag and cascades the delete event. This is called before the parent model is deleted. This method checks in with the Multisite trait to preserve shared relations.

See Also

protected setRelationSimpleValue()

protected setRelationSimpleValue($relationName, $value): void

setRelationSimpleValue sets a relation value directly from its attribute.

protected validateRelationArgs()

protected validateRelationArgs($relationName, $optional, $required = []): void

validateRelationArgs supplied relation arguments