protected function _merge($data, $exists)
    {
        if (!$data) {
            $this->_parent->get($this->_through)->clear();
            return;
        }

        $pivot = $this->_parent->{$this->_through};

        $relThrough = $this->_parent->schema()->relation($this->_through);
        $through = $relThrough->to();
        $schema = $through::definition();
        $rel = $schema->relation($this->_using);
        $fromKey = $rel->keys('from');
        $toKey = $rel->keys('to');

        $i = 0;

        while ($i < $pivot->count()) {
            $found = false;
            $entity = $pivot->get($i);
            $id1 = $entity->get($fromKey);
            if ($id1 === null) {
                $pivot->splice($i, 1);
                continue;
            }
            foreach ($data as $key => $item) {
                $isDocument = $item instanceof Document;
                $id2 = $isDocument ? $item->get($toKey) : (isset($item[$toKey]) ?$item[$toKey] : null);

                if ((string) $id1 === (string) $id2) {
                    if ($isDocument) {
                        $entity->set($this->_using, $item);
                    } else {
                        $entity->get($this->_using)->amend($item);
                    }
                    unset($data[$key]);
                    $i++;
                    $found = true;
                    break;
                }
            }
            if (!$found) {
                $pivot->splice($i, 1);
            }
        }

        foreach ($data as $entity) {
            $this[] = $entity;
        }
    }