Matiss
Matiss

Reputation: 5349

Silverstripe 4, How to solve database table conflict that maps to same database table

So I made a mistake and accidentally defined two classes that lead to the same database table

<?php

namespace Sca {
  use SilverStripe\ORM\DataObject;

  class Gallery extends DataObject {

      private static $table_name = 'Gallery';
      // ...
  }
}

And the other class

<?php

namespace Sca {
  use SilverStripe\ORM\DataObject;

  class GalleryHolder extends DataObject {

      private static $table_name = 'Gallery';
      // ...
  }
}

Then I ran /dev/build and the site crashed. Now the only thin I see is the apache error on apache log under /var/log/apache/..

[2019-08-05 23:36:32] error-log.ERROR: Uncaught Exception LogicException: "Multiple classes ("Sca\GalleryHolder", "Sca\Gallery") map to the same table: "Gallery"" at /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php line 299 {"exception":"[object] (LogicException(code: 0): Multiple classes (\"Sca\GalleryHolder\", \"Sca\Gallery\") map to the same table: \"Gallery\" at /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299)"} []

I dumped the database and $ grep'ed the dirs with no success to find the place where there is defined GalleryHolder that leads to creating Gallery table for that class. No results. I also deleted the class file for GalleryHolder and ran again /dev/build but still gas that error and "Server error" screen when visiting from the web. Is there any suggestions where to clear the cache or schema to resolve the conflict?

Upvotes: 0

Views: 299

Answers (1)

Matiss
Matiss

Reputation: 5349

So I found a quick fix. I went to /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299
and commented out the lines that was throwing an error and added new line that modifies the table name if there is conflict. I reset changed these lines

if ($conflict) {
//    throw new LogicException(
//       "Multiple classes (\"{$class}\", \"{$conflict}\") map to the same table: \"{$table}\""
//    );
      $table = $table . '_dupl';
}

Then I was able to load the page again and it rebuilt the cache by it self and I uncomented previousley commented out lines at /var/www/demo/sc2/vendor/silverstripe/framework/src/ORM/DataObjectSchema.php:299 .

Upvotes: 0

Related Questions