Neuron-PHP

BelongsToManyRelation extends Relation
in package

Implements belongs-to-many relationship logic (many-to-many with pivot table).

Table of Contents

Properties

$_parent  : Model
$_pdo  : PDO
$_relatedModel  : string
$_foreignPivotKey  : string
$_parentKey  : string
$_pivotTable  : string
$_relatedKey  : string
$_relatedPivotKey  : string

Methods

__construct()  : mixed
Constructor
attach()  : void
Attach related models to the pivot table.
detach()  : void
Detach related models from the pivot table.
eagerLoad()  : void
Eager load relations for multiple models.
getRelatedModel()  : string
Get the related model class name.
handleDependent()  : void
Handle dependent cascade when parent is destroyed.
load()  : Model|array<string|int, mixed>|null
Load the related models.
sync()  : void
Sync the pivot table with the given IDs.

Properties

$_relatedModel

protected string $_relatedModel

Methods

__construct()

Constructor

public __construct(PDO $pdo, Model $parent, string $relatedModel, string $pivotTable, string $foreignPivotKey, string $relatedPivotKey[, string $parentKey = 'id' ][, string $relatedKey = 'id' ]) : mixed
Parameters
$pdo : PDO

Database connection

$parent : Model

Parent model instance

$relatedModel : string

Related model class name

$pivotTable : string

Pivot table name

$foreignPivotKey : string

Foreign key in pivot table for parent model

$relatedPivotKey : string

Foreign key in pivot table for related model

$parentKey : string = 'id'

Parent key column name

$relatedKey : string = 'id'

Related key column name

attach()

Attach related models to the pivot table.

public attach(array<string|int, mixed>|int $ids) : void
Parameters
$ids : array<string|int, mixed>|int

Single ID or array of IDs

detach()

Detach related models from the pivot table.

public detach([array<string|int, mixed>|int|null $ids = null ]) : void
Parameters
$ids : array<string|int, mixed>|int|null = null

Single ID, array of IDs, or null to detach all

eagerLoad()

Eager load relations for multiple models.

public eagerLoad(array<string|int, mixed> $models) : void
Parameters
$models : array<string|int, mixed>

Array of parent models

getRelatedModel()

Get the related model class name.

public getRelatedModel() : string
Return values
string

handleDependent()

Handle dependent cascade when parent is destroyed.

public handleDependent(DependentStrategy $strategy) : void

For BelongsToMany relationships:

  • Destroy: Deletes related records AND pivot entries
  • DeleteAll: Deletes only pivot entries (default behavior)
  • Nullify: Same as DeleteAll (deletes pivot entries)
  • Restrict: Prevents deletion if pivot entries exist
Parameters
$strategy : DependentStrategy
Tags
throws
RelationException

sync()

Sync the pivot table with the given IDs.

public sync(array<string|int, mixed> $ids) : void

Removes all existing pivot entries and creates new ones.

Parameters
$ids : array<string|int, mixed>

Array of related model IDs


        
On this page

Search results