|
1
|
|
|
<?php namespace Backpack\CRUD; |
|
2
|
|
|
|
|
3
|
|
|
use Illuminate\Database\Eloquent\Collection; |
|
4
|
|
|
use Illuminate\Database\Eloquent\Model; |
|
5
|
|
|
use DB; |
|
6
|
|
|
use Lang; |
|
7
|
|
|
|
|
8
|
|
|
trait CrudTrait { |
|
9
|
|
|
|
|
10
|
|
|
/* |
|
11
|
|
|
|-------------------------------------------------------------------------- |
|
12
|
|
|
| Methods for ENUM and SELECT crud fields. |
|
13
|
|
|
|-------------------------------------------------------------------------- |
|
14
|
|
|
*/ |
|
15
|
|
|
|
|
16
|
|
|
public static function getPossibleEnumValues($field_name){ |
|
17
|
|
|
$instance = new static; // create an instance of the model to be able to get the table name |
|
18
|
|
|
$type = DB::select( DB::raw('SHOW COLUMNS FROM '.$instance->getTable().' WHERE Field = "'.$field_name.'"') )[0]->Type; |
|
|
|
|
|
|
19
|
|
|
preg_match('/^enum\((.*)\)$/', $type, $matches); |
|
20
|
|
|
$enum = array(); |
|
21
|
|
|
$exploded = explode(',', $matches[1]); |
|
22
|
|
|
foreach($exploded as $value){ |
|
23
|
|
|
$v = trim( $value, "'" ); |
|
24
|
|
|
$enum[] = $v; |
|
25
|
|
|
} |
|
26
|
|
|
return $enum; |
|
27
|
|
|
} |
|
28
|
|
|
|
|
29
|
|
|
public static function isColumnNullable($column_name) { |
|
30
|
|
|
$instance = new static; // create an instance of the model to be able to get the table name |
|
31
|
|
|
$answer = DB::select(DB::raw("SELECT IS_NULLABLE FROM INFORMATION_SCHEMA.COLUMNS WHERE TABLE_NAME='".$instance->getTable()."' AND COLUMN_NAME='".$column_name."' AND table_schema='".env('DB_DATABASE')."'"))[0]; |
|
|
|
|
|
|
32
|
|
|
|
|
33
|
|
|
return ($answer->IS_NULLABLE == 'YES' ? true : false); |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
|
|
37
|
|
|
/* |
|
38
|
|
|
|-------------------------------------------------------------------------- |
|
39
|
|
|
| Methods for Fake Fields functionality (used in PageManager). |
|
40
|
|
|
|-------------------------------------------------------------------------- |
|
41
|
|
|
*/ |
|
42
|
|
|
|
|
43
|
|
|
/** |
|
44
|
|
|
* Add fake fields as regular attributes, even though they are stored as JSON. |
|
45
|
|
|
* |
|
46
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
47
|
|
|
*/ |
|
48
|
|
|
public function addFakes($columns = ['extras']) { |
|
49
|
|
|
foreach ($columns as $key => $column) { |
|
50
|
|
|
|
|
51
|
|
|
$column_contents = $this->{$column}; |
|
52
|
|
|
|
|
53
|
|
|
if (!is_object($this->{$column})) |
|
54
|
|
|
{ |
|
55
|
|
|
$column_contents = json_decode($this->{$column}); |
|
56
|
|
|
} |
|
57
|
|
|
|
|
58
|
|
|
if (count($column_contents)) |
|
59
|
|
|
{ |
|
60
|
|
|
foreach ($column_contents as $fake_field_name => $fake_field_value) { |
|
61
|
|
|
$this->setAttribute($fake_field_name, $fake_field_value); |
|
|
|
|
|
|
62
|
|
|
} |
|
63
|
|
|
} |
|
64
|
|
|
} |
|
65
|
|
|
} |
|
66
|
|
|
|
|
67
|
|
|
/** |
|
68
|
|
|
* Return the entity with fake fields as attributes. |
|
69
|
|
|
* |
|
70
|
|
|
* @param array $columns - the database columns that contain the JSONs |
|
71
|
|
|
* @return CrudTrait |
|
|
|
|
|
|
72
|
|
|
*/ |
|
73
|
|
|
public function withFakes($columns = []) |
|
74
|
|
|
{ |
|
75
|
|
|
$model = '\\'.get_class($this); |
|
76
|
|
|
|
|
77
|
|
|
if (!count($columns)) { |
|
78
|
|
|
if (property_exists($model, 'fakeColumns')) { |
|
79
|
|
|
$columns = $this->fakeColumns; |
|
|
|
|
|
|
80
|
|
|
} else |
|
81
|
|
|
{ |
|
82
|
|
|
$columns = ['extras']; |
|
83
|
|
|
} |
|
84
|
|
|
} |
|
85
|
|
|
|
|
86
|
|
|
$this->addFakes($columns); |
|
87
|
|
|
|
|
88
|
|
|
return $this; |
|
89
|
|
|
} |
|
90
|
|
|
|
|
91
|
|
|
} |
|
92
|
|
|
|
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
Idableprovides a methodequalsIdthat 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.