Search code examples
exceptionnamespacessilverstripe

SilverStripe: Getting "you need to change the ClassName before you can write it" after update files to use namespaces


I'm getting this exception when trying to ->write() a DataObject called 'ModelSheet', it says the name should be Models\ModelSheet instead of ModelSheet only (i am under the same namespace (Models) and even try with an use statement)


Solution

  • It seems that the ClassName saved to your database record does not match your PHP classname.

    When changing classnames (adding or changing a namespace is changing the classname), you need to update the database to reflect this changes, as the classname is saved in the DB, so Silverstripe knows which PHP-Object is related to the data record.

    If you used Silverstripe's upgrader tool, you should have an .upgrade.yml in your module's directory (e.g. in app or mysite). If not, you can add it manually (see e.g. https://github.com/wernerkrauss/silverstripe-onepage/blob/master/.upgrade.yml as a random example). The structure is like

    mappings:
      OldClassName: My\Namespace\NewClassname
    

    After that all you need is to run dev/build/?flush and your database should be updated.