Skip to content

Commit c31d829

Browse files
committed
#10 - Add checkTable()
1 parent e3fd3b3 commit c31d829

File tree

2 files changed

+47
-4
lines changed

2 files changed

+47
-4
lines changed

src/TgDatabase/DAO.php

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,36 @@ class DAO {
2424
/**
2525
* Constructor.
2626
* @param Database $database - the database object
27-
* @param string $tableName - the table name this object will handle (can include #__ as prefix)
28-
* @param string $modelClass - the name of the class that rows will be converted to (optional, default is \stdClass).
29-
* @param string $idColumn - the name of the integer, auto-incremental primary key column (optional, default is uid)
27+
* @param string $tableName - the table name this object will handle (can include #__ as prefix)
28+
* @param string $modelClass - the name of the class that rows will be converted to (optional, default is \stdClass).
29+
* @param string $idColumn - the name of the integer, auto-incremental primary key column (optional, default is uid)
30+
* @param boolean $checkTable - whether to check existance of table and create if required (optional, default is FALSE)
3031
*/
31-
public function __construct($database, $tableName, $modelClass = 'stdClass', $idColumn = 'uid') {
32+
public function __construct($database, $tableName, $modelClass = 'stdClass', $idColumn = 'uid', $checkTable = FALSE) {
3233
$this->database = $database;
3334
$this->tableName = $tableName;
3435
$this->modelClass = class_exists($modelClass) ? $modelClass : 'stdClass';
3536
$this->idColumn = $idColumn;
37+
if ($checkTable) $this->checkTable();
38+
}
39+
40+
/**
41+
* Checks the underlying table for existance and calls #createTable() if it does not exist.
42+
* @return boolean TRUE when table exists or was created successfully.
43+
*/
44+
public function checkTable() {
45+
if (!$this->tableExists()) {
46+
return $this->createTable();
47+
}
48+
return TRUE;
49+
}
50+
51+
/**
52+
* Creates the table. Default implementation does nothing and returns FALSE.
53+
* @return TRUE when table was created successfully.
54+
*/
55+
public function createTable() {
56+
return FALSE;
3657
}
3758

3859
/**

src/TgDatabase/DataModel.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,5 +57,27 @@ public function getDatabase() {
5757
public function register($name, $dao) {
5858
$this->models[$name] = $dao;
5959
}
60+
61+
/**
62+
* Performs a check of each DAO whether underlying tables exists and creates them if required.
63+
* @return object with the result(s):
64+
* {
65+
* "tableChecks" : {
66+
* "<dao-registered-name>" : TRUE|FALSE,
67+
* ...
68+
* },
69+
* "success" : TRUE|FALSE
70+
* }
71+
*/
72+
public function checkTables() {
73+
$rc = new \stdClass;
74+
$rc->tableChecks = new \stdClass;
75+
$rc->success = TRUE;
76+
foreach ($this->models AS $name => $dao) {
77+
$rc->tableChecks->$name = $dao->checkTable();
78+
if (!$rc->tableChecks->$name) $rc->success = FALSE;
79+
}
80+
return $rc;
81+
}
6082
}
6183

0 commit comments

Comments
 (0)