XF 2.2 Node (Forum) creation via entity system: please_select_valid_parent

Scandal

Active member
I have been building an importer for own use (I want to import a batch of forums via a CSV).

I'm doing something like:
PHP:
$node = \XF::em()->create('XF:Node');
$node->title = 'something';
$node->node_type_id = 'Forum';
// i have the $parent_node_id from a node which created previously on the same script // I'm sure it is was saved and exists on the db
$node->parent_node_id = intval($parent_node_id);
$node->preSave();
                            
if ($node->getErrors())
{
    $returned_errors[] = implode('<br />', $node->getErrors());
}
else
{
    $node->save();
}

But I receive the error: please_select_valid_parent, which is on XF\Behavior\TreeStructured:
PHP:
    public function preSave()
    {
        $entity = $this->entity;

        if ($entity->isInsert() || $entity->isChanged($this->config['parentField']))
        {
            $parentId = $entity->getValue($this->config['parentField']);
            if ($parentId)
            {
                $newParent = $entity->em()->find($entity->structure()->shortName, $parentId);
                if (!$newParent || ($newParent->lft >= $entity->lft && $newParent->rgt <= $entity->rgt))
                {
                    $entity->error(\XF::phrase('please_select_valid_parent'), $this->config['parentField']);
                }
            }
        }
    }

Any idea to fix this error?
 

Jeremy P

XenForo developer
Staff member
Probably because the nested set needs to be rebuilt (which happens at the end of a request). You might be better off writing an importer using the importer framework.
 

Scandal

Active member
Probably because the nested set needs to be rebuilt (which happens at the end of a request). You might be better off writing an importer using the importer framework.
Yes, that was. What I did is to create via a script run all the parent nodes -> do some page refreshes on the admin -> it shows that a jos have to be run -> rebuilt the permissions. Then run the script for second time to create all the child nodes -> again a message appear on the admin to rebuild the permissions. Once all done, I can see also the Forums on the forum list. (y)
 
Top