1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace AntonyThorpe\SilverShopProductModel; |
4
|
|
|
|
5
|
|
|
use SilverShop\Page\ProductCategory; |
|
|
|
|
6
|
|
|
use SilverStripe\Forms\TextField; |
7
|
|
|
use SilverStripe\Forms\FieldList; |
8
|
|
|
use SilverStripe\ORM\DataObject; |
9
|
|
|
|
10
|
|
|
/** |
11
|
|
|
* Create Product Models in the Product Category and use these for the dropdown when editing a product |
12
|
|
|
* (instead of a textfield). |
13
|
|
|
* In addition, loop over the models on the Product Category Page |
14
|
|
|
*/ |
15
|
|
|
class ProductModel extends DataObject |
16
|
|
|
{ |
17
|
|
|
private static $db = array( |
|
|
|
|
18
|
|
|
'Title' => 'Varchar(100)', |
19
|
|
|
'Description' => 'Varchar(255)', |
20
|
|
|
'Sort' => 'Int' |
21
|
|
|
); |
22
|
|
|
|
23
|
|
|
private static $has_one = array( |
|
|
|
|
24
|
|
|
'ProductCategory' => ProductCategory::class |
25
|
|
|
); |
26
|
|
|
|
27
|
|
|
private static $required_fields = array( |
|
|
|
|
28
|
|
|
'Title' |
29
|
|
|
); |
30
|
|
|
|
31
|
|
|
private static $table_name = 'AntonyThorpe_SilverShopProductModel_ProductModel'; |
|
|
|
|
32
|
|
|
|
33
|
|
|
public function getCMSFields() |
34
|
|
|
{ |
35
|
|
|
return FieldList::create( |
36
|
|
|
TextField::create('Title', _t(self::class . 'Title', 'Model Title')) |
37
|
|
|
->setMaxLength(100), |
38
|
|
|
TextField::create('Description', _t(self::class . 'Description', 'Model Description')) |
39
|
|
|
->setRightTitle(_t(self::class . 'DescriptionRightTitle', 'An additional description if needed by the website')) |
40
|
|
|
->setMaxLength(255) |
41
|
|
|
); |
42
|
|
|
} |
43
|
|
|
|
44
|
|
|
public function validate() |
45
|
|
|
{ |
46
|
|
|
$result = parent::validate(); |
47
|
|
|
|
48
|
|
|
if (empty($this->Title)) { |
49
|
|
|
$result->addError( |
50
|
|
|
_t( |
51
|
|
|
self::class . 'ValidationMessageTitle', |
52
|
|
|
'ProductModel Class validation - missing the Model Title field' |
53
|
|
|
) |
54
|
|
|
); |
55
|
|
|
} |
56
|
|
|
|
57
|
|
|
if (empty($this->ProductCategoryID)) { |
|
|
|
|
58
|
|
|
$result->addError( |
59
|
|
|
_t( |
60
|
|
|
self::class . 'ValidationMessageProductCategoryID', |
61
|
|
|
'ProductModel Class validation - missing ProductCategoryID field' |
62
|
|
|
) |
63
|
|
|
); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
return $result; |
67
|
|
|
} |
68
|
|
|
} |
69
|
|
|
|
Let?s assume that you have a directory layout like this:
and let?s assume the following content of
Bar.php
:If both files
OtherDir/Foo.php
andSomeDir/Foo.php
are loaded in the same runtime, you will see a PHP error such as the following:PHP Fatal error: Cannot use SomeDir\Foo as Foo because the name is already in use in OtherDir/Foo.php
However, as
OtherDir/Foo.php
does not necessarily have to be loaded and the error is only triggered if it is loaded beforeOtherDir/Bar.php
, this problem might go unnoticed for a while. In order to prevent this error from surfacing, you must import the namespace with a different alias: