[CakePHP] Database waardes in custom config file bepalen
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<?php
$config = array(
'Company' => array(
'name' => 'Optiek Cardoen',
'email' => '[email protected]'
)
);
switch ($_SERVER['SERVER_NAME']) {
case 'local.optiekcardoen.be':
$config = array_merge($config, array(
'debug' => 2,
'Database' => array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'xxx'
)
));
break;
default:
$config = array_merge($config, array(
'debug' => 0,
'Database' => array(
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx'
)
));
break;
}
?>
$config = array(
'Company' => array(
'name' => 'Optiek Cardoen',
'email' => '[email protected]'
)
);
switch ($_SERVER['SERVER_NAME']) {
case 'local.optiekcardoen.be':
$config = array_merge($config, array(
'debug' => 2,
'Database' => array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'xxx'
)
));
break;
default:
$config = array_merge($config, array(
'debug' => 0,
'Database' => array(
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx'
)
));
break;
}
?>
Ik vraag me nu af of het vanuit deze config file mogelijk is om te bepalen welke database er in moet worden geladen (de lokale database of de productie database). Iemand die hier ervaring mee heeft? ;)
Dit doe je hier toch al op basis van servername? Of heb je dev en productie op dezelfde server?
Code (php)
1
2
3
4
5
6
7
8
9
10
11
2
3
4
5
6
7
8
9
10
11
<?php
$config = array_merge($config, array(
'debug' => 0,
'Database' => array(
'host' => 'localhost', // <-- Dit is je databaseserver.
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx' // <-- En dit de database op die server.
)
));
?>
$config = array_merge($config, array(
'debug' => 0,
'Database' => array(
'host' => 'localhost', // <-- Dit is je databaseserver.
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx' // <-- En dit de database op die server.
)
));
?>
Code (php)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
<?php
class DATABASE_CONFIG {
/* Staging */
public $local = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => '',
'encoding' => 'utf8',
);
/* Production */
/*public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => '',
'encoding' => 'utf8',
);*/
/* Testing */
/*public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);*/
}
?>
class DATABASE_CONFIG {
/* Staging */
public $local = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => '',
'encoding' => 'utf8',
);
/* Production */
/*public $default = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'xxx',
'password' => 'xxx',
'database' => 'xxx',
'prefix' => '',
'encoding' => 'utf8',
);*/
/* Testing */
/*public $test = array(
'datasource' => 'Database/Mysql',
'persistent' => false,
'host' => 'localhost',
'login' => 'user',
'password' => 'password',
'database' => 'test_database_name',
'prefix' => '',
//'encoding' => 'utf8',
);*/
}
?>
Dat is dus iets wat ik anders wil en vanuit m'n "customConfig.php" wil laten gebeuren. Hopelijk heb ik m'n probleem hiermee wat duidelijker uitgelegd? ;)
Is dit in cakephp2 of 3?
Want volgens mij kan het in 2 niet op deze manier, het volgende zou moeten werken
Code (php)
1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
<?php
App::uses('ConnectionManager', 'Model');
ConnectionManager::loadDataSource(array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'xxx'
));
?>
App::uses('ConnectionManager', 'Model');
ConnectionManager::loadDataSource(array(
'host' => 'localhost',
'login' => 'root',
'password' => '',
'database' => 'xxx'
));
?>
Gewijzigd op 09/07/2015 10:56:13 door Ricardo Dijken