public function afterSave(Event $event, Entity $entity)
{
$model = $event->subject();
$types = $this->_typeMaps[$this->config('type')];
if (!is_array($types)) {
$types = [$types];
}
foreach ($types as $type) {
$parent = $entity->parentNode();
if (!empty($parent)) {
$parent = $this->node($parent, $type)->first();
}
$data = ['parent_id' => isset($parent->id) ? $parent->id : null, 'model' => $model->alias(), 'foreign_key' => $entity->id];
if (!$entity->isNew()) {
$node = $this->node($entity, $type)->first();
$data['id'] = isset($node->id) ? $node->id : null;
$newData = $model->{$type}->patchEntity($node, $data);
} else {
$newData = $model->{$type}->newEntity($data);
}
$saved = $model->{$type}->target()->save($newData);
}
}