Skip to content

Fatal error when creating subpages on legacy databases #336

@hightowe

Description

@hightowe

Background

I am in the process of upgrading a quite old WonderCMS installation (3.0.4) and hit this problem.

Fixed here

main...hightowe:wondercms-subpage-bugfix:main

Description

A PHP Fatal error occurs when attempting to create or save a subpage in WonderCMS (v3.6.0) if the parent page was created in an older version of WonderCMS that did not explicitly initialize the subpages property in database.js.

When the parent page lacks the subpages key, the code attempts to descend into it, resulting in a null value. Subsequent attempts to assign properties to this null object trigger a Fatal Error.

Error Trace

PHP Fatal error:  Uncaught Error: Attempt to assign property "testsubpage" on null in index.php:798
Stack trace:
#0 index.php(2656): Wcms->createPage()
#1 index.php(164): Wcms->saveAction()
#2 index.php(15): Wcms->init()

Additionally, several PHP warnings are triggered in findAndUpdateMenuKey due to null values being passed to explode() and foreach().

Root Cause

The core issue is that many existing WonderCMS installations have a database.js where pages do not have a subpages key. The current code assumes this key always exists after a depth of 1, which fails on legacy data or after certain upgrade paths.

Proposed Fix

The fix involves adding surgical property_exists checks and ensuring that the subpages property is initialized as a stdClass before assignment. This ensures compatibility with older database structures without requiring a full database migration.

Key Changes:

  • createPage(): Add property_exists checks before descending into child pages.
  • findAndUpdateMenuKey(): Ensure the menu key is cast to a string and return an empty array if null to prevent foreach warnings.
  • Initialization: Use the internal self::DB_PAGES_SUBPAGE_KEY constant consistently.

Impact

This affects users who have upgraded their WonderCMS installation over time and are now trying to utilize subpage functionality on older parent pages.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions