forked from NetCommons3/NetCommons
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathNetCommonsAppModel.php
More file actions
108 lines (97 loc) · 2.78 KB
/
NetCommonsAppModel.php
File metadata and controls
108 lines (97 loc) · 2.78 KB
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
<?php
/**
* NetCommonsApp Model
*
* @author Jun Nishikawa <topaz2@m0n0m0n0.com>
* @author Takako Miyagawa <nekoget@gmail.com>
* @link http://www.netcommons.org NetCommons Project
* @license http://www.netcommons.org/license.txt NetCommons License
* @copyright Copyright 2014, NetCommons Project
*/
App::uses('Model', 'Model');
/**
* NetCommonsApp Model
*
* @author Jun Nishikawa <topaz2@m0n0m0n0.com>
* @author Takako Miyagawa <nekoget@gmail.com>
* @package NetCommons\NetCommons\Model
* @SuppressWarnings(PHPMD.NumberOfChildren)
* @codeCoverageIgnore
*/
class NetCommonsAppModel extends Model {
/**
* use behaviors
*
* @var array
*/
public $actsAs = array(
'NetCommons.Trackable',
);
/**
* Validation rules
*
* @var array
*/
public $validate = array();
/**
* Constructor. Binds the model's database table to the object.
*
* @param bool|int|string|array $id Set this ID for this model on startup,
* can also be an array of options, see above.
* @param string $table Name of database table to use.
* @param string $ds DataSource connection name.
* @return void
* @SuppressWarnings(PHPMD.BooleanArgumentFlag)
*/
public function __construct($id = false, $table = null, $ds = null) {
// If a datasource is set via params, use it and return
if ((is_array($id) && isset($id['ds'])) || $ds) {
parent::__construct($id, $table, $ds);
return;
}
// Use a static variable, to only use one connection per page-call (otherwise we would get a new handle every time a Model is created)
static $_useDbConfig;
if (!isset($_useDbConfig)) {
// Get all available database-configs
$sources = ConnectionManager::enumConnectionObjects();
// Find the slaves we have
$slaves = array();
foreach ($sources as $name => $values) {
unset($values);
// Slaves have to be named "slave1", "slave2", etc...
if (preg_match('/^slave[0-9]+$/i', $name) == 1) {
$slaves[] = $name;
}
}
// Randomly use a slave
$_useDbConfig = $slaves[rand(0, count($slaves) - 1)];
}
$this->useDbConfig = $_useDbConfig;
parent::__construct($id, $table, $ds);
}
/**
* Sets the DataSource to which this model is bound.
*
* @param string $dataSource The name of the DataSource, as defined in app/Config/database.php
* @return void
* @throws MissingConnectionException
*/
public function setDataSource($dataSource = null) {
$this->useDbConfig !== 'test' && parent::setDataSource($dataSource);
}
/**
* Load models
*
* @param array $models models to load
* @param string $source data source
* @return void
*/
public function loadModels(array $models = [], $source = 'master') {
foreach ($models as $model => $class) {
$this->$model = ClassRegistry::init($class, true);
if ($this->$model->useDbConfig !== 'test') {
$this->$model->setDataSource($source);
}
}
}
}