diff --git a/src/Model/ProductCategory.php b/src/Model/ProductCategory.php index 0a4131a..76463fc 100755 --- a/src/Model/ProductCategory.php +++ b/src/Model/ProductCategory.php @@ -86,7 +86,10 @@ class ProductCategory extends DataObject * @var array */ private static $indexes = [ - 'Code' => true, + 'Code' => [ + 'type' => 'unique', + 'columns' => ['Code'], + ], ]; /** @@ -183,37 +186,21 @@ public function getCMSFields() ->setDescription('Enter a dollar amount here for the declared customs value for international shipments. If you leave this blank, the sale price of the item will be used.'); - /* - $fields = FieldList::create( - LiteralField::create( - 'PCIntro', - _t( - 'ProductCategory.PCIntro', - '

Categories must be created in your - - FoxyCart Product Categories - , and also manually created in FoxyStripe. -

' - ) - ), - TextField::create('Code') - ->setTitle(_t('ProductCategory.Code', 'Category Code')) - ->setDescription(_t('ProductCategory.CodeDescription', 'copy/paste from FoxyCart')), - TextField::create('Title') - ->setTitle(_t('ProductCategory.Title', 'Category Title')) - ->setDescription(_t('ProductCategory.TitleDescription', 'copy/paste from FoxyCart')), - DropdownField::create( - 'DeliveryType', - 'Delivery Type', - singleton('ProductCategory')->dbObject('DeliveryType')->enumValues() - )->setEmptyString('') - ); + return $fields; + } - $this->extend('updateCMSFields', $fields); - */ + /** + * @return \SilverStripe\ORM\ValidationResult + */ + public function validate() + { + $result = parent::validate(); - return $fields; + if (ProductCategory::get()->filter('Code', $this->Code)->exclude('ID', $this->ID)->first()) { + $result->addError('Code must be unique for each category.'); + } + + return $result; } /** diff --git a/tests/ProductPageTest.php b/tests/ProductPageTest.php index cba5dab..b884856 100644 --- a/tests/ProductPageTest.php +++ b/tests/ProductPageTest.php @@ -107,7 +107,6 @@ public function testProductCategoryCreation() { $this->logInWithPermission('Product_CANCRUD'); $category = $this->objFromFixture(ProductCategory::class, 'apparel'); - $category->write(); $categoryID = $category->ID; $productCategory = ProductCategory::get()->filter(array('Code' => 'APPAREL'))->first(); @@ -120,12 +119,10 @@ public function testProductCategoryDeletion() $this->logInWithPermission('Product_CANCRUD'); $category = $this->objFromFixture(ProductCategory::class, 'default'); - $category->write(); $this->assertFalse($category->canDelete()); $category2 = $this->objFromFixture(ProductCategory::class, 'apparel'); - $category2->write(); $category2ID = $category2->ID; $this->assertTrue($category2->canDelete());