public function unrelate(Row ...$rows): self
    {
        $table1 = $this->table;

        foreach ($rows as $row) {
            $table2 = $row->getTable();

            //Has one
            if ($field = $table1->getJoinField($table2)) {
                $this->{$field->getName()} = null;
                continue;
            }

            //Has many
            if ($field = $table2->getJoinField($table1)) {
                $row->{$field->getName()} = null;
                $row->save();
                continue;
            }

            //Has many to many
            if ($joinTable = $table1->getJoinTable($table2)) {
                $joinTable->delete()
                    ->where("{$joinTable->getJoinField($table1)} = ", $this->id)
                    ->where("{$joinTable->getJoinField($table2)} = ", $row->id)
                    ->run();

                continue;
            }

            throw new RuntimeException(
                sprintf('The tables %s and %s are not related', $table1, $table2)
            );
        }

        return $this->save();
    }
public function unrelate(Row ...$rows): self
    {
        $table1 = $this->table;

        foreach ($rows as $row) {
            $table2 = $row->getTable();

            //Has one
            if ($field = $table1->getJoinField($table2)) {
                $this->{$field->getName()} = null;
                continue;
            }

            //Has many
            if ($field = $table2->getJoinField($table1)) {
                $row->{$field->getName()} = null;
                $row->save();
                continue;
            }

            //Has many to many
            if ($joinTable = $table1->getJoinTable($table2)) {
                $joinTable->delete()
                    ->where("{$joinTable->getJoinField($table1)} = ", $this->id)
                    ->where("{$joinTable->getJoinField($table2)} = ", $row->id)
                    ->run();

                continue;
            }

            throw new RuntimeException(
                sprintf('The tables %s and %s are not related', $table1, $table2)
            );
        }

        return $this->save();
    }
public function unrelate ( Row ... $ rows ) : self { $ table1 = $ this -> table ; foreach ( $ rows as $ row ) { $ table2 = $ row -> getTable ( ) ; //Has one if ( $ field = $ table1 -> getJoinField ( $ table2 ) ) { $ this -> { $ field -> getName ( ) } = null ; continue ; } //Has many if ( $ field = $ table2 -> getJoinField ( $ table1 ) ) { $ row -> { $ field -> getName ( ) } = null ; $ row -> save ( ) ; continue ; } //Has many to many if ( $ joinTable = $ table1 -> getJoinTable ( $ table2 ) ) { $ joinTable -> delete ( ) -> where ( "{$joinTable->getJoinField($table1)} = " , $ this -> id ) -> where ( "{$joinTable->getJoinField($table2)} = " , $ row -> id ) -> run ( ) ; continue ; } throw new RuntimeException ( sprintf ( 'The tables %s and %s are not related' , $ table1 , $ table2 ) ) ; } return $ this -> save ( ) ; }
Unrelate this row with other rows
Unrelate this row with other rows