1 <?php
2 /**
3 * Application model for Cake.
4 *
5 * This file is application-wide model file. You can put all
6 * application-wide model-related methods here.
7 *
8 * PHP 5
9 *
10 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
11 * Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
12 *
13 * Licensed under the Creative Commons BY-NC-SA 3.0 License
14 * Redistributions of files must retain the above copyright notice.
15 *
16 * @copyright Copyright 2005-2012, Cake Software Foundation, Inc. (http://cakefoundation.org)
17 * @link http://cakephp.org CakePHP(tm) Project
18 * @package app.Model
19 * @since CakePHP(tm) v 0.2.9
20 * @license Creative Commons BY-NC-SA 3.0 License (http://creativecommons.org/licenses/by-nc-sa/3.0/)
21 */
22
23 App::uses('Model', 'Model');
24
25 /**
26 * Application model for Cake.
27 *
28 * Add your application-wide methods in the class below, your models
29 * will inherit them.
30 *
31 * @package app.Model
32 */
33 class AppModel extends Model {
34
35 /**
36 * @var bool set to true in model if you want to use the b3Database to retrieve statistics data.
37 * Defaults to false to avoid conflicts with external plugins that depend on the default connection to be app-related.
38 */
39 public $b3Database = false;
40
41 //-------------------------------------------------------------------
42
43 /**
44 * Dynamically creates a B3 DataSource object at runtime based on server selection.
45 * Sets DataSource as 'default' if $this->b3Database is set to false.
46 *
47 * @param bool $id
48 * @param null $table
49 * @param null $ds
50 */
51 public function __construct($id = false, $table = null, $ds = null) {
52 if ($this->b3Database) {
53 $serverId = Configure::read('server_id');
54 $host = Configure::read('servers.' . $serverId . '.dbhost');
55 $login = Configure::read('servers.' . $serverId . '.dbuser');
56 $password = Configure::read('servers.' . $serverId . '.dbpass');
57 $database = Configure::read('servers.' . $serverId . '.dbname');
58
59 $config = array (
60 'datasource' => 'Database/Mysql',
61 'persistent' => false,
62 'host' => $host,
63 'login' => $login,
64 'password' => $password,
65 'database' => $database,
66 'prefix' => '',
67 //'encoding' => 'utf8',
68 );
69 try {
70 ConnectionManager::create('b3', $config);
71 $this->useDbConfig = 'b3';
72 } catch(Exception $e) {
73 die ('Cannot Connect to B3 Database');
74 }
75
76 } else {
77 $this->setDataSource('default');
78 }
79 parent::__construct($id, $table, $ds);
80 }
81
82 }
83