1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Models\Traits; |
4
|
|
|
|
5
|
|
|
trait HasIdentifiableAttribute |
6
|
|
|
{ |
7
|
|
|
/** |
8
|
|
|
* Get the name of the attribute that best defines the entry, from the user perspective. |
9
|
|
|
* |
10
|
|
|
* Rephrased: In most cases a user will NOT identify an Article because its ID is "4", but |
11
|
|
|
* because its name is "10 Ways to Burn Fat". This method returns the column in the database |
12
|
|
|
* that represents that is better to show to the user as an identifier rather than the ID. |
13
|
|
|
* Ex: name, title, label, description etc. |
14
|
|
|
* |
15
|
|
|
* @return string The name of the column that best defines this entry from the user perspective. |
16
|
|
|
*/ |
17
|
|
|
public static function getIdentifiableName() |
18
|
|
|
{ |
19
|
|
|
$model = (new self); |
20
|
|
|
if (method_exists($model, 'identifiableName')) { |
21
|
|
|
return $model->identifiableName(); |
22
|
|
|
} |
23
|
|
|
|
24
|
|
|
return static::guessIdentifiableColumnName(); |
25
|
|
|
} |
26
|
|
|
|
27
|
|
|
/** |
28
|
|
|
* Get the most likely column in the db table that could be used as an identifiable attribute. |
29
|
|
|
* |
30
|
|
|
* @return string The name of the column in the database that is most likely to be a good indentifying attribute. |
31
|
|
|
*/ |
32
|
|
|
public static function guessIdentifiableColumnName() |
33
|
|
|
{ |
34
|
|
|
$instance = new static(); |
35
|
|
|
$conn = $instance->getConnectionWithExtraTypeMappings(); |
|
|
|
|
36
|
|
|
$table = $instance->getTableWithPrefix(); |
|
|
|
|
37
|
|
|
$columns = $conn->getDoctrineSchemaManager()->listTableColumns($table); |
38
|
|
|
$indexes = $conn->getDoctrineSchemaManager()->listTableIndexes($table); |
39
|
|
|
$columnsNames = array_keys($columns); |
40
|
|
|
|
41
|
|
|
// these column names are sensible defaults for lots of use cases |
42
|
|
|
$sensibleDefaultNames = ['name', 'title', 'description', 'label']; |
43
|
|
|
|
44
|
|
|
// if any of the sensibleDefaultNames column exists |
45
|
|
|
// that's probably a good choice |
46
|
|
|
foreach ($sensibleDefaultNames as $defaultName) { |
47
|
|
|
if (in_array($defaultName, $columnsNames)) { |
48
|
|
|
return $defaultName; |
49
|
|
|
} |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
// get indexed columns in database table |
53
|
|
|
$indexedColumns = []; |
54
|
|
|
foreach ($indexes as $index) { |
55
|
|
|
$indexColumns = $index->getColumns(); |
56
|
|
|
foreach ($indexColumns as $ic) { |
57
|
|
|
array_push($indexedColumns, $ic); |
58
|
|
|
} |
59
|
|
|
} |
60
|
|
|
|
61
|
|
|
// if none of the sensible defaults exists |
62
|
|
|
// we get the first column from database |
63
|
|
|
// that is NOT indexed (usually primary, foreign keys) |
64
|
|
|
foreach ($columns as $columnName => $columnProperties) { |
65
|
|
|
if (! in_array($columnName, $indexedColumns)) { |
66
|
|
|
|
67
|
|
|
//check for convention "field<_id>" in case developer didn't add foreign key constraints. |
68
|
|
|
if (strpos($columnName, '_id') !== false) { |
69
|
|
|
continue; |
70
|
|
|
} |
71
|
|
|
|
72
|
|
|
return $columnName; |
73
|
|
|
} |
74
|
|
|
} |
75
|
|
|
|
76
|
|
|
// in case everything fails we just return the first column in database |
77
|
|
|
return \Arr::first($columnsNames); |
78
|
|
|
} |
79
|
|
|
} |
80
|
|
|
|
This check looks for methods that are used by a trait but not required by it.
To illustrate, let’s look at the following code example
The trait
Idable
provides a methodequalsId
that in turn relies on the methodgetId()
. If this method does not exist on a class mixing in this trait, the method will fail.Adding the
getId()
as an abstract method to the trait will make sure it is available.