source/Spiral/ORM/Entities/Relations/ManyToManyRelation.php
public function link(RecordInterface $record, array $pivotData = []): self
    {
        $this->loadData(true);
        $this->assertValid($record);
        $this->assertPivot($pivotData);

        //Ensure reference
        $record = $this->matchOne($record) ?? $record;

        if (in_array($record, $this->instances, true)) {
            //Merging pivot data
            $this->pivotData->offsetSet($record, $pivotData + $this->getPivot($record));

            if (!in_array($record, $this->updated, true) && !in_array($record, $this->scheduled, true)) {
                //Indicating that record pivot data has been changed
                $this->updated[] = $record;
            }

            return $this;
        }

        //New association
        $this->instances[] = $record;
        $this->scheduled[] = $record;
        $this->pivotData->offsetSet($record, $pivotData);

        return $this;
    }
public function link(RecordInterface $record, array $pivotData = []): self
    {
        $this->loadData(true);
        $this->assertValid($record);
        $this->assertPivot($pivotData);

        //Ensure reference
        $record = $this->matchOne($record) ?? $record;

        if (in_array($record, $this->instances, true)) {
            //Merging pivot data
            $this->pivotData->offsetSet($record, $pivotData + $this->getPivot($record));

            if (!in_array($record, $this->updated, true) && !in_array($record, $this->scheduled, true)) {
                //Indicating that record pivot data has been changed
                $this->updated[] = $record;
            }

            return $this;
        }

        //New association
        $this->instances[] = $record;
        $this->scheduled[] = $record;
        $this->pivotData->offsetSet($record, $pivotData);

        return $this;
    }
public function link ( RecordInterface $ record , array $ pivotData = [ ] ) : self { $ this -> loadData ( true ) ; $ this -> assertValid ( $ record ) ; $ this -> assertPivot ( $ pivotData ) ; //Ensure reference $ record = $ this -> matchOne ( $ record ) ?? $ record ; if ( in_array ( $ record , $ this -> instances , true ) ) { //Merging pivot data $ this -> pivotData -> offsetSet ( $ record , $ pivotData + $ this -> getPivot ( $ record ) ) ; if ( ! in_array ( $ record , $ this -> updated , true ) && ! in_array ( $ record , $ this -> scheduled , true ) ) { //Indicating that record pivot data has been changed $ this -> updated [ ] = $ record ; } return $ this ; } //New association $ this -> instances [ ] = $ record ; $ this -> scheduled [ ] = $ record ; $ this -> pivotData -> offsetSet ( $ record , $ pivotData ) ; return $ this ; }
Link record with parent entity. Only record instances is accepted.

Attention, attached instances MIGHT be de-referenced IF parent object was reselected in a
different scope.

@param RecordInterface $record
@param array           $pivotData

@return self

@throws RelationException
Link record with parent entity . Only record instances is accepted .