Skip to content

Commit a6c6359

Browse files
committed
Add a DataModel as a central way to hold DAOs
1 parent a9cb10e commit a6c6359

File tree

1 file changed

+61
-0
lines changed

1 file changed

+61
-0
lines changed

src/TgDatabase/DataModel.php

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
<?php
2+
3+
namespace TgDatabase;
4+
5+
/**
6+
* A simple class to keep all DAOs in one place
7+
*
8+
* @author ralph
9+
*
10+
*/
11+
class DataModel {
12+
13+
private $database;
14+
15+
private $models;
16+
17+
/**
18+
* Constructor.
19+
* @param Database $database - the database instance
20+
*/
21+
public function __construct($database) {
22+
$this->database = $database;
23+
$this->models = array();
24+
$this->init($database);
25+
}
26+
27+
/**
28+
* Initializes all DAOs.
29+
* <p>This method does nothing. Descendants shall override here and create and register their DAOs.</p>
30+
* @param Database $database - the database object
31+
*/
32+
protected function init($database) {
33+
}
34+
35+
/**
36+
* Returns a DAO registered under a certain name.
37+
* @param String name - name of model
38+
* return DAO the DAO registered or NULL
39+
*/
40+
public function get($name) {
41+
return $this->models[$name];
42+
}
43+
44+
/**
45+
* Returns the database object.
46+
* @return Database the database object.
47+
*/
48+
public function getDatabase() {
49+
return $this->database;
50+
}
51+
52+
/**
53+
* Registers a DAO under a name.
54+
* @param string $name - the name of the model
55+
* @param DAO $dao - the DAO to be registered
56+
*/
57+
public function register($name, $dao) {
58+
$this->models[$name] = $dao;
59+
}
60+
}
61+

0 commit comments

Comments
 (0)