Completed
Branch newinternal (104de7)
by Simon
10:16
created

DataObject::delete()   A

Complexity

Conditions 3
Paths 3

Size

Total Lines 23
Code Lines 13

Duplication

Lines 0
Ratio 0 %

Importance

Changes 3
Bugs 1 Features 0
Metric Value
c 3
b 1
f 0
dl 0
loc 23
rs 9.0856
cc 3
eloc 13
nc 3
nop 0
1
<?php
2
3
namespace Waca;
4
5
use Waca\Exceptions\OptimisticLockFailedException;
6
7
/**
8
 * DataObject is the base class for all the database access classes. Each
9
 * "DataObject" holds one record from the database, and provides functions to
10
 * allow loading from and saving to the database.
11
 *
12
 * Note: This requires the database tables to be named the same as the classes,
13
 * and the database tables must have an "id" column. Simple views can be used
14
 * as a way of aliasing to allow for a transition period.
15
 *
16
 * @author Simon Walker
17
 */
18
abstract class DataObject
19
{
20
	/** @var int ID of the object */
21
	protected $id = null;
22
	/** @var int update version for optimistic locking */
23
	protected $updateversion = 0;
24
	/**
25
	 * @var PdoDatabase
26
	 */
27
	protected $dbObject;
28
29
	/**
30
	 * Retrieves a data object by it's row ID.
31
	 *
32
	 * @param int         $id
33
	 * @param PdoDatabase $database
34
	 *
35
	 * @return DataObject|false
36
	 */
37
	public static function getById($id, PdoDatabase $database)
38
	{
39
		$array = explode('\\', get_called_class());
40
		$realClassName = strtolower(end($array));
41
42
		$statement = $database->prepare("SELECT * FROM {$realClassName} WHERE id = :id LIMIT 1;");
43
		$statement->bindValue(":id", $id);
44
45
		$statement->execute();
46
47
		$resultObject = $statement->fetchObject(get_called_class());
48
49
		if ($resultObject != false) {
50
			$resultObject->setDatabase($database);
51
		}
52
53
		return $resultObject;
54
	}
55
56
	public function setDatabase(PdoDatabase $db)
57
	{
58
		$this->dbObject = $db;
59
	}
60
61
	/**
62
	 * Gets the database associated with this data object.
63
	 * @return PdoDatabase
64
	 */
65
	public function getDatabase()
66
	{
67
		return $this->dbObject;
68
	}
69
70
	/**
71
	 * Saves a data object to the database, either updating or inserting a record.
72
	 */
73
	abstract public function save();
74
75
	/**
76
	 * Retrieves the ID attribute
77
	 */
78
	public function getId()
79
	{
80
		return $this->id;
81
	}
82
83
	/**
84
	 * Deletes the object from the database
85
	 */
86
	public function delete()
87
	{
88
		if ($this->id === null) {
89
			// wtf?
90
			return;
91
		}
92
93
		$array = explode('\\', get_called_class());
94
		$realClassName = strtolower(end($array));
95
96
		$deleteQuery = "DELETE FROM {$realClassName} WHERE id = :id AND updateversion = :updateversion LIMIT 1;";
97
		$statement = $this->dbObject->prepare($deleteQuery);
98
99
		$statement->bindValue(":id", $this->id);
100
		$statement->bindValue(":updateversion", $this->updateversion);
101
		$statement->execute();
102
103
		if ($statement->rowCount() !== 1) {
104
			throw new OptimisticLockFailedException();
105
		}
106
107
		$this->id = null;
108
	}
109
110
	/**
111
	 * @return int
112
	 */
113
	public function getUpdateVersion()
114
	{
115
		return $this->updateversion;
116
	}
117
118
	/**
119
	 * Sets the update version.
120
	 *
121
	 * You should never call this to change the value of the update version. You should only call it when passing user
122
	 * input through.
123
	 *
124
	 * @param int $updateVersion
125
	 */
126
	public function setUpdateVersion($updateVersion)
127
	{
128
		$this->updateversion = $updateVersion;
129
	}
130
131
	/**
132
	 * @return bool
133
	 */
134
	public function isNew(){
135
		return $this->id === null;
136
	}
137
}
138