|
1
|
|
|
<?php |
|
2
|
|
|
|
|
3
|
|
|
namespace UniMan\Drivers\Mysql\Forms; |
|
4
|
|
|
|
|
5
|
|
|
use Nette\Application\UI\Form; |
|
6
|
|
|
use Nette\Utils\ArrayHash; |
|
7
|
|
|
use PDO; |
|
8
|
|
|
use UniMan\Core\Forms\DatabaseForm\DatabaseFormInterface; |
|
9
|
|
|
|
|
10
|
|
|
class MySqlDatabaseForm implements DatabaseFormInterface |
|
11
|
|
|
{ |
|
12
|
|
|
private $pdo; |
|
13
|
|
|
|
|
14
|
|
|
private $database; |
|
15
|
|
|
|
|
16
|
|
|
public function __construct(PDO $pdo, $database) |
|
17
|
|
|
{ |
|
18
|
|
|
$this->pdo = $pdo; |
|
19
|
|
|
$this->database = $database; |
|
20
|
|
|
} |
|
21
|
|
|
|
|
22
|
|
|
public function addFieldsToForm(Form $form) |
|
23
|
|
|
{ |
|
24
|
|
|
$form->addText('name', 'Name') |
|
25
|
|
|
->setRequired(); |
|
26
|
|
|
|
|
27
|
|
|
$characterSets = $this->pdo->query('SELECT CHARACTER_SET_NAME, CONCAT(CHARACTER_SET_NAME, " (", DESCRIPTION, ")") FROM information_schema.CHARACTER_SETS ORDER BY CHARACTER_SET_NAME')->fetchAll(PDO::FETCH_KEY_PAIR); |
|
28
|
|
|
$form->addSelect('charset', 'Character set', $characterSets) |
|
29
|
|
|
->setAttribute('class', 'js-select2') |
|
|
|
|
|
|
30
|
|
|
->setPrompt('Default character set'); |
|
31
|
|
|
|
|
32
|
|
|
$collations = []; |
|
33
|
|
|
foreach ($this->pdo->query('SELECT CHARACTER_SET_NAME, COLLATION_NAME FROM information_schema.COLLATIONS ORDER BY COLLATION_NAME')->fetchAll(PDO::FETCH_ASSOC) as $collation) { |
|
34
|
|
|
$collations[$collation['CHARACTER_SET_NAME']][$collation['COLLATION_NAME']] = $collation['COLLATION_NAME']; |
|
35
|
|
|
} |
|
36
|
|
|
ksort($collations); |
|
37
|
|
|
|
|
38
|
|
|
$form->addSelect('collation', 'Collation', $collations) |
|
39
|
|
|
->setAttribute('class', 'js-select2') |
|
|
|
|
|
|
40
|
|
|
->setPrompt('Default collation'); |
|
41
|
|
|
|
|
42
|
|
|
if ($this->database) { |
|
43
|
|
|
$form['name']->setDisabled(); |
|
44
|
|
|
$defaults = $this->pdo->query("SELECT `SCHEMA_NAME` AS `name`, `DEFAULT_CHARACTER_SET_NAME` AS `charset`, `DEFAULT_COLLATION_NAME` AS `collation` FROM `information_schema`.`SCHEMATA` WHERE `SCHEMA_NAME` = '{$this->database}'")->fetch(PDO::FETCH_ASSOC); |
|
45
|
|
|
$form->setDefaults($defaults); |
|
46
|
|
|
} |
|
47
|
|
|
} |
|
48
|
|
|
|
|
49
|
|
|
public function submit(Form $form, ArrayHash $values) |
|
50
|
|
|
{ |
|
51
|
|
|
if ($this->database) { |
|
52
|
|
|
$query = 'ALTER DATABASE ' . $this->database; |
|
53
|
|
|
} else { |
|
54
|
|
|
$query = 'CREATE DATABASE ' . $values['name']; |
|
55
|
|
|
} |
|
56
|
|
|
if ($values['charset']) { |
|
57
|
|
|
$query .= ' CHARACTER SET ' . $values['charset']; |
|
58
|
|
|
if ($values['collation']) { |
|
59
|
|
|
$query .= ' COLLATE ' . $values['collation']; |
|
60
|
|
|
} |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
$statement = $this->pdo->prepare($query); |
|
64
|
|
|
$res = $statement->execute(); |
|
65
|
|
|
if ($res === false) { |
|
66
|
|
|
$form->addError($statement->errorInfo()[2]); |
|
67
|
|
|
return; |
|
68
|
|
|
} |
|
69
|
|
|
return $res; |
|
70
|
|
|
} |
|
71
|
|
|
} |
|
72
|
|
|
|
It seems like the type of the argument is not accepted by the function/method which you are calling.
In some cases, in particular if PHP’s automatic type-juggling kicks in this might be fine. In other cases, however this might be a bug.
We suggest to add an explicit type cast like in the following example: