PHP Fatal error: Cannot declare class... When Following Nextcloud app Tutorial

Hi there,

I was following the Nextcloud App development Tutorial using the latest documentation and got to the Database migration section and tried to update the app using php occ upgrade and it gave me the following Error:

 PHP Fatal error:  Cannot declare class OCA\NotesTutorial\Migration\Version000000Date20200616180432, because the name is already in use in /media/webroot/nextcloud-dev/apps/nctut/lib/Migration/Version000000Date20200616180432.php on line 46

I’ve Created a VersionXXYYZZDateYYYYMMDDHHSSAA.php in lib/Migration added the php, Increased the version number then tried to update and it gave me the error above.

I’ve got Nextcloud 19 and the contents of the Version000000Date20200616180432.php file is:

<?php

  namespace OCA\NotesTutorial\Migration;

  use Closure;
  use OCP\DB\ISchemaWrapper;
  use OCP\Migration\SimpleMigrationStep;
  use OCP\Migration\IOutput;

  class Version000000Date20200616180432 extends SimpleMigrationStep {

    /**
    * @param IOutput $output
    * @param Closure $schemaClosure The `\Closure` returns a `ISchemaWrapper`
    * @param array $options
    * @return null|ISchemaWrapper
    */
    public function changeSchema(IOutput $output, Closure $schemaClosure, array $options) {
        /** @var ISchemaWrapper $schema */
        $schema = $schemaClosure();

        if (!$schema->hasTable('notestutorial')) {
            $table = $schema->createTable('notestutorial');
            $table->addColumn('id', 'integer', [
                'autoincrement' => true,
                'notnull' => true,
            ]);
            $table->addColumn('title', 'string', [
                'notnull' => true,
                'length' => 200
            ]);
            $table->addColumn('user_id', 'string', [
                'notnull' => true,
                'length' => 200,
            ]);
            $table->addColumn('content', 'text', [
                'notnull' => true,
                'default' => ''
            ]);

            $table->setPrimaryKey(['id']);
            $table->addIndex(['user_id'], 'notestutorial_user_id_index');
        }
        return $schema;
    }
}

Any help appriciated.

I assume you have both apps enabled? You cannot have multiple PHP classes from different apps that have the same OCA\NotesTutorial\... namespace. You either need to disable the demo app when using your own or change your apps namespace all over the place where it is used.

1 Like

Thanks no I didn’t have both apps enabled but I found my problem now, I didn’t change the namespace from the files I copied over from the tutorial to the one my app was using.

Thanks for your help