1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
|
4
|
|
|
class TitleDataObject extends DataObject |
|
|
|
|
5
|
|
|
{ |
6
|
|
|
private static $indexes = array( |
|
|
|
|
7
|
|
|
'Title' => 'unique("Title, ClassName")' |
8
|
|
|
); |
9
|
|
|
|
10
|
|
|
private static $searchable_fields = array( |
|
|
|
|
11
|
|
|
'Title' => 'PartialMatchFilter' |
12
|
|
|
); |
13
|
|
|
|
14
|
|
|
private static $default_sort = array( |
|
|
|
|
15
|
|
|
'Title' => "ASC" |
16
|
|
|
); |
17
|
|
|
|
18
|
|
|
private static $db = array( |
|
|
|
|
19
|
|
|
'Title' => 'Varchar(255)', |
20
|
|
|
); |
21
|
|
|
|
22
|
|
|
/** |
23
|
|
|
* to prevent racing conditions ... |
24
|
|
|
* |
25
|
|
|
* @var array |
26
|
|
|
*/ |
27
|
|
|
private static $_cache = array(); |
28
|
|
|
|
29
|
|
|
/** |
30
|
|
|
* see README.md for usage ... |
31
|
|
|
* |
32
|
|
|
* @param string $title |
33
|
|
|
* @param bool $showDBAlterationMessage |
34
|
|
|
* @return DataObject |
35
|
|
|
*/ |
36
|
|
|
public static function find_or_create($title, $showDBAlterationMessage = false) |
37
|
|
|
{ |
38
|
|
|
$title = trim($title); |
39
|
|
|
$titleToLower = strtolower(($title)); |
40
|
|
|
$className = get_called_class(); |
41
|
|
|
$key = $className.'_'.$titleToLower; |
42
|
|
|
if (isset(self::$_cache[$key])) { |
43
|
|
|
if ($showDBAlterationMessage) { |
44
|
|
|
DB::alteration_message('Found '.$className.' with Title = <strong>'.$title.'</strong>'); |
45
|
|
|
} |
46
|
|
|
return self::$_cache[$key]; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
if (! $title) { |
50
|
|
|
return $className::create(); |
51
|
|
|
} |
52
|
|
|
$obj = $className::get()->where('LOWER("Title") =\''.Convert::raw2sql($titleToLower).'\''); |
53
|
|
|
|
54
|
|
|
if ($obj->count() == 0) { |
55
|
|
|
if ($showDBAlterationMessage) { |
56
|
|
|
DB::alteration_message('Creating new '.$className.' with Title = <strong>'.$title.'</strong>', 'created'); |
57
|
|
|
} |
58
|
|
|
$obj = $className::create(); |
59
|
|
|
} else { |
60
|
|
|
if ($showDBAlterationMessage) { |
61
|
|
|
DB::alteration_message('Found '.$className.' with Title = <strong>'.$title.'</strong>'); |
62
|
|
|
} |
63
|
|
|
$obj = $obj->first(); |
64
|
|
|
} |
65
|
|
|
$obj->Title = $title; |
66
|
|
|
self::$_cache[$key] = $obj; |
67
|
|
|
$obj->write(); |
68
|
|
|
|
69
|
|
|
return $obj; |
70
|
|
|
} |
71
|
|
|
} |
72
|
|
|
|
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.