1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
namespace Backpack\CRUD\app\Models\Traits; |
4
|
|
|
|
5
|
|
|
use DB; |
6
|
|
|
use Illuminate\Database\Eloquent\Model; |
7
|
|
|
use \Backpack\CRUD\app\Library\CrudPanel\CrudPanelFacade as CRUD; |
8
|
|
|
|
9
|
|
|
|
10
|
|
|
|
11
|
|
|
/* |
12
|
|
|
|-------------------------------------------------------------------------- |
13
|
|
|
| Methods for working with relationships inside select/relationship fields. |
14
|
|
|
|-------------------------------------------------------------------------- |
15
|
|
|
*/ |
16
|
|
|
trait HasRelationshipFields |
17
|
|
|
{ |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Register aditional types in doctrine schema manager for the current connection. |
21
|
|
|
* |
22
|
|
|
* @return DB |
23
|
|
|
*/ |
24
|
|
|
public function getConnectionWithExtraTypeMappings() |
25
|
|
|
{ |
26
|
|
|
$conn = DB::connection($this->getConnectionName()); |
27
|
|
|
|
28
|
|
|
// register the enum, json and jsonb column type, because Doctrine doesn't support it |
29
|
|
|
$conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('enum', 'string'); |
30
|
|
|
$conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('json', 'json_array'); |
31
|
|
|
$conn->getDoctrineSchemaManager()->getDatabasePlatform()->registerDoctrineTypeMapping('jsonb', 'json_array'); |
32
|
|
|
|
33
|
|
|
return $conn; |
34
|
|
|
} |
35
|
|
|
|
36
|
|
|
/** |
37
|
|
|
* Get the model's table name, with the prefix added from the configuration file. |
38
|
|
|
* |
39
|
|
|
* @return string Table name with prefix |
40
|
|
|
*/ |
41
|
|
|
public function getTableWithPrefix() |
42
|
|
|
{ |
43
|
|
|
$prefix = $this->getConnection()->getTablePrefix(); |
44
|
|
|
$tableName = $this->getTable(); |
45
|
|
|
|
46
|
|
|
return $prefix.$tableName; |
47
|
|
|
} |
48
|
|
|
|
49
|
|
|
/** |
50
|
|
|
* Get the column type for a certain db column. |
51
|
|
|
* |
52
|
|
|
* @param string $columnName Name of the column in the db table. |
53
|
|
|
* @return string Db column type. |
54
|
|
|
*/ |
55
|
|
|
public function getColumnType($columnName) |
56
|
|
|
{ |
57
|
|
|
$conn = $this->getConnectionWithExtraTypeMappings(); |
58
|
|
|
$table = $this->getTableWithPrefix(); |
59
|
|
|
|
60
|
|
|
return $conn->getSchemaBuilder()->getColumnType($table, $columnName); |
61
|
|
|
} |
62
|
|
|
|
63
|
|
|
/** |
64
|
|
|
* Checks if the given column name is nullable. |
65
|
|
|
* |
66
|
|
|
* @param string $column_name The name of the db column. |
67
|
|
|
* @return bool |
68
|
|
|
*/ |
69
|
|
|
public static function isColumnNullable($column_name) |
70
|
|
|
{ |
71
|
|
|
// create an instance of the model to be able to get the table name |
72
|
|
|
$instance = new static(); |
73
|
|
|
|
74
|
|
|
$conn = $instance->getConnectionWithExtraTypeMappings(); |
75
|
|
|
$table = $instance->getTableWithPrefix(); |
76
|
|
|
|
77
|
|
|
// MongoDB columns are alway nullable |
78
|
|
|
if (!in_array($conn->getConfig()['driver'], CRUD::getSqlDriverList()) { |
79
|
|
|
return true; |
|
|
|
|
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
try { |
83
|
|
|
// check if the column exists in the database |
84
|
|
|
$column = $conn->getDoctrineColumn($table, $column_name); |
85
|
|
|
// check for NOT NULL |
86
|
|
|
$notNull = $column->getNotnull(); |
87
|
|
|
// return the value of nullable (aka the inverse of NOT NULL) |
88
|
|
|
return ! $notNull; |
89
|
|
|
} catch (\Exception $e) { |
90
|
|
|
return true; |
91
|
|
|
} |
92
|
|
|
} |
93
|
|
|
} |
94
|
|
|
|