Saves models from the given attributes array.

@param array $tree
@param \Franzose\ClosureTable\Contracts\EntityInterface $parent
@return \Franzose\ClosureTable\Extensions\Collection
15943
public static function createFromArray(array $tree, EntityInterface $parent = null)
    {
        $childrenRelationIndex = with(new static)->getChildrenRelationIndex();
        $entities = [];

        foreach ($tree as $item) {
            $children = array_pull($item, $childrenRelationIndex);

            /**
             * @var Entity $entity
             */
            $entity = new static($item);
            $entity->parent_id = $parent ? $parent->getKey() : null;
            $entity->save();

            if (!is_null($children)) {
                $children = static::createFromArray($children, $entity);
                $entity->setRelation($childrenRelationIndex, $children);
                $entity->addChildren($children->all());
            }

            $entities[] = $entity;
        }

        return new Collection($entities);
    }
1068454
public function setChildOf(Tree $parent)
    {
        if ($parent->type->name !== 'category') {
            throw new DomainException("Content can be assigned only to category parent");
        }
        return parent::setChildOf($parent);
    }
1369367
private function descendantsFamilies(Tree $tree, Collection $families): ResponseInterface
    {
        $this->layout = 'layouts/ajax';

        return $this->viewResponse('lists/families-table', [
            'families' => $families,
            'tree'     => $tree,
        ]);
    }
1305308
public function getFlatCollection(array $tree) : ObjectCollection
    {
        $data = [];

        array_walk_recursive($tree, function ($item) use (&$data)
        {
            if ($item !== NULL)
            {
                $data[] = $item;
            }
        });

        return new ObjectCollection($data);
    }
1125326
public function setParent(Node $parent)
    {
        $this->relations['parent'] = $parent;
        $parent->addChild($this);
        return $this;
    }
1038373
public static function tree_to($tree, $fchildrens = 'children')
    {
        $arr = [];
        if (isset($tree[$fchildrens]) && is_array($tree[$fchildrens])) {
            foreach ($tree[$fchildrens] as $child) {
                $arr = array_merge($arr, self::tree_to($child, $fchildrens));
            }
            unset($tree[$fchildrens]);
            $arr[] = $tree;
        } else {
            $arr[] = $tree;
        }
        return $arr;
    }
245354
protected function parseRecursive( $root, $tree )
	{
		$nodes = array();
		if( $child = $root->firstChild )
		{
			while( $child )
			{
				$attributes	= $child->hasAttributes()? $child->attributes : array();
				switch( $child->nodeType )
				{
					case XML_ELEMENT_NODE:
						$node = new XML_DOM_Node( $child->nodeName );
						if( !$this->parseRecursive( $child, $node ) )
						{
	#						$node->setContent( utf8_decode( $child->textContent ) );
							$node->setContent( $child->textContent );
						}
						foreach( $attributes as $attribute)
							$node->setAttribute( $attribute->nodeName, stripslashes( $attribute->nodeValue ) );
						$tree->addChild( $node );
						break;
					case XML_TEXT_NODE:
						if( strlen( trim( $content = $child->textContent ) ) )
						{
							return false;
						}
						else if( isset( $attributes['type'] ) && preg_match( "/.*ml/i", $attributes['type'] ) )
						{
							return false;
						}
						break;
					case XML_CDATA_SECTION_NODE:
						$tree->setContent( stripslashes( $child->textContent ) );
						break;
					default:
						break;
				}
				$child = $child->nextSibling;
			}
		}
		return true;
	}
197627
public function setParent($parent){

            $this->parent = $parent;

            if($this->parentCallback){
                call_user_func($this->parentCallback);
            }

            return $this;

        }
1242558
private function makeNode(&$row, &$tree)
    {
        $hasChildren = $this->propertyAccessor->getValue($row, "[{$this->options['children_column']}]");
        $weight      = $this->propertyAccessor->getValue($row, "[{$this->options['hierarchy_column']}]");

        $row['hasChildren'] = (bool)($hasChildren > 0);
        $row['weight']      = $weight;
        $tree[$row['id']]   = $row;
    }
52769
public function fields($parent, array $fields)
    {
        foreach ($fields as $id => $field) {
            if (!isset($field['id'])) {
                $field['id'] = $id;
            }
            if (!isset($field['name'])) {
                $field['name'] = $id;
            }
            $this->field($parent, $field);
        }
    }
827045
public function getAdminAction(Tree $tree): ResponseInterface
    {
        $this->layout = 'layouts/administration';

        $stories = DB::table('block')
            ->where('module_name', '=', $this->name())
            ->where('gedcom_id', '=', $tree->id())
            ->orderBy('xref')
            ->get();

        foreach ($stories as $story) {
            $block_id = (int) $story->block_id;

            $story->individual = Individual::getInstance($story->xref, $tree);
            $story->title      = $this->getBlockSetting($block_id, 'title');
            $story->languages  = $this->getBlockSetting($block_id, 'languages');
        }

        return $this->viewResponse('modules/stories/config', [
            'stories'    => $stories,
            'title'      => $this->title() . ' — ' . $tree->title(),
            'tree'       => $tree,
            'tree_names' => Tree::getNameList(),
        ]);
    }
205727
public function changeFamilyMembersAction(ServerRequestInterface $request, Tree $tree): ResponseInterface
    {
        $xref   = $request->get('xref', '');
        $family = Family::getInstance($xref, $tree);
        Auth::checkFamilyAccess($family, true);

        $HUSB = $request->get('HUSB', '');
        $WIFE = $request->get('WIFE', '');
        $CHIL = $request->get('CHIL', []);

        // Current family members
        $old_father   = $family->husband();
        $old_mother   = $family->wife();
        $old_children = $family->children();

        // New family members
        $new_father   = Individual::getInstance($HUSB, $tree);
        $new_mother   = Individual::getInstance($WIFE, $tree);
        $new_children = [];
        foreach ($CHIL as $child) {
            $new_children[] = Individual::getInstance($child, $tree);
        }

        if ($old_father !== $new_father) {
            if ($old_father) {
                // Remove old FAMS link
                foreach ($old_father->facts(['FAMS']) as $fact) {
                    if ($fact->target() === $family) {
                        $old_father->deleteFact($fact->id(), true);
                    }
                }
                // Remove old HUSB link
                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
                    if ($fact->target() === $old_father) {
                        $family->deleteFact($fact->id(), true);
                    }
                }
            }
            if ($new_father) {
                // Add new FAMS link
                $new_father->createFact('1 FAMS @' . $family->xref() . '@', true);
                // Add new HUSB link
                $family->createFact('1 HUSB @' . $new_father->xref() . '@', true);
            }
        }

        if ($old_mother !== $new_mother) {
            if ($old_mother) {
                // Remove old FAMS link
                foreach ($old_mother->facts(['FAMS']) as $fact) {
                    if ($fact->target() === $family) {
                        $old_mother->deleteFact($fact->id(), true);
                    }
                }
                // Remove old WIFE link
                foreach ($family->facts(['HUSB', 'WIFE']) as $fact) {
                    if ($fact->target() === $old_mother) {
                        $family->deleteFact($fact->id(), true);
                    }
                }
            }
            if ($new_mother) {
                // Add new FAMS link
                $new_mother->createFact('1 FAMS @' . $family->xref() . '@', true);
                // Add new WIFE link
                $family->createFact('1 WIFE @' . $new_mother->xref() . '@', true);
            }
        }

        foreach ($old_children as $old_child) {
            if ($old_child && !in_array($old_child, $new_children, true)) {
                // Remove old FAMC link
                foreach ($old_child->facts(['FAMC']) as $fact) {
                    if ($fact->target() === $family) {
                        $old_child->deleteFact($fact->id(), true);
                    }
                }
                // Remove old CHIL link
                foreach ($family->facts(['CHIL']) as $fact) {
                    if ($fact->target() === $old_child) {
                        $family->deleteFact($fact->id(), true);
                    }
                }
            }
        }

        foreach ($new_children as $new_child) {
            if ($new_child && !$old_children->contains($new_child)) {
                // Add new FAMC link
                $new_child->createFact('1 FAMC @' . $family->xref() . '@', true);
                // Add new CHIL link
                $family->createFact('1 CHIL @' . $new_child->xref() . '@', true);
            }
        }

        return redirect($family->url());
    }
1074539
public function createRoot(array $attributes)
    {
        $attributes[$this->getParentIdName()] = null;
        $root = parent::create($attributes);

        if (!$root[$this->getRootIdName()]) {
            $root[$this->getRootIdName()] = $root->getId();
            $root->save();
        }

        return $root;

    }
812059
protected function performCreateChildOf(array $attributes, Node $parent)
    {

        $parentIdName = $this->getParentIdName();
        $rootIdName = $this->getRootIdName();

        $child = $this->make($attributes);
        $child[$parentIdName] = $parent->getId();
        $child[$rootIdName] = $parent[$rootIdName];

        $child->save();

        // Set in-memory relations
        $child->setParent($parent);
        $parent->addChild($child);

        return $child;

    }
67109
public function saved(Tree $model)
    {
        if ($model->{$model->getTreeColumn('path')} === '') { // If we just save() new node
            $model->{$model->getTreeColumn('path')} = $model->getKey() . '/';
            DB::connection($model->getConnectionName())->table($model->getTable())
                ->where($model->getKeyName(), '=', $model->getKey())
                ->update(
                    array(
                        $model->getTreeColumn('path') => $model->{$model->getTreeColumn('path')}
                    )
                );
        }
    }
1063405
public function storeAsChildOf(array $attributes, $parentModel, $position=null)
    {

        $this->validate($attributes, 'store');

        $filtered = $this->toModelAttributes($this->make([]), $attributes);

        $this->callBeforeListeners('store', [&$attributes, $parentModel]);

        if (!$position) {
            $model = $this->model()->createChildOf($filtered, $parentModel);
        } else {
            $model = $this->model()->createAt($filtered, $parentModel, $position);
        }

        $this->callAfterListeners('store', [$model]);

        return $model;
    }
128585
public function store(array $attributes)
    {
        $model = $this->parent->store($attributes);
        $this->cache->put($model);
        return $model;
    }