Rev 35 | Rev 51 | Go to most recent revision | Show entire file | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 35 | Rev 50 | ||
---|---|---|---|
Line 5... | Line 5... | ||
5 | 5 | ||
6 | /**
|
6 | /**
|
7 | * Generic database table model class
|
7 | * Generic database table model class
|
8 | *
|
8 | *
|
9 | * @author Thomas Lahn
|
9 | * @author Thomas Lahn
|
- | 10 | * @property Database $database
|
|
10 | * @property-read int $lastInsertId
|
11 | * @property-read int $lastInsertId
|
11 | * ID of the last inserted row, or the last value from
|
12 | * ID of the last inserted row, or the last value from
|
12 | a sequence object, depending on the underlying driver.
|
13 | a sequence object, depending on the underlying driver.
|
13 | */
|
14 | */
|
14 | class Table extends AbstractModel |
15 | class Table extends AbstractModel |
Line 18... | Line 19... | ||
18 | */
|
19 | */
|
19 | protected $_name = ''; |
20 | protected $_name = ''; |
20 | 21 | ||
21 | /**
|
22 | /**
|
22 | * Database of the table
|
23 | * Database of the table
|
23 | * @var Database
|
24 | * @var Database|string
|
24 | */
|
25 | */
|
25 | protected $_database; |
26 | protected $_database; |
26 | 27 | ||
27 | protected $_id = 'id'; |
28 | protected $_id = 'id'; |
28 | 29 | ||
Line 43... | Line 44... | ||
43 | * Table name
|
44 | * Table name
|
44 | * @param string $id
|
45 | * @param string $id
|
45 | * Name of the primary key column
|
46 | * Name of the primary key column
|
46 | * @throws InvalidArgumentException
|
47 | * @throws InvalidArgumentException
|
47 | */
|
48 | */
|
48 | public function __construct(Database $database = null, $name = '', $id = '') |
49 | public function __construct($database = null, $name = '', $id = '') |
49 | {
|
50 | {
|
50 | if ($database === null) |
51 | if ($database === null) |
51 | {
|
52 | {
|
- | 53 | /* Call getter to convert to Database if possible */
|
|
- | 54 | if ($this->database === null) |
|
- | 55 | {
|
|
52 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
56 | $this->_database = Application::getInstance()->getDefaultDatabase(); |
53 | }
|
57 | }
|
- | 58 | }
|
|
54 | else
|
59 | else
|
55 | {
|
60 | {
|
56 | $this->_database = $database; |
61 | $this->_database = $database; |
57 | }
|
62 | }
|
58 | 63 | ||
59 | if ($name !== '') |
64 | if ($name !== '') |
60 | {
|
65 | {
|
61 | $this->_name = $name; |
66 | $this->_name = $name; |
62 | }
|
67 | }
|
63 | 68 | ||
64 | if (!$this->_name) |
69 | if (!\is_string($this->_name)) |
65 | {
|
70 | {
|
66 | throw new InvalidArgumentException('a table name is required'); |
71 | throw new \InvalidArgumentException( |
- | 72 | 'Expected string for table name, saw '
|
|
- | 73 | . \get_class($this->_name) || \gettype($this->_name)); |
|
67 | }
|
74 | }
|
68 | 75 | ||
69 | if ($id !== '') |
76 | if ($id !== '') |
70 | {
|
77 | {
|
71 | $this->_id = $id; |
78 | $this->_id = $id; |
Line 76... | Line 83... | ||
76 | * Returns the database for the table
|
83 | * Returns the database for the table
|
77 | * @return Database
|
84 | * @return Database
|
78 | */
|
85 | */
|
79 | public function getDatabase() |
86 | public function getDatabase() |
80 | {
|
87 | {
|
- | 88 | if (\is_string($this->_database)) |
|
- | 89 | {
|
|
- | 90 | /* Call setter to convert to Database */
|
|
- | 91 | $this->database = $this->_database; |
|
- | 92 | }
|
|
- | 93 | ||
81 | return $this->_database; |
94 | return $this->_database; |
82 | }
|
95 | }
|
83 | 96 | ||
84 | /**
|
97 | /**
|
- | 98 | * @param Database|string $value
|
|
- | 99 | * @throws InvalidArgumentException
|
|
- | 100 | */
|
|
- | 101 | public function setDatabase ($value) |
|
- | 102 | {
|
|
- | 103 | if ($value instanceof Database) |
|
- | 104 | {
|
|
- | 105 | $this->_database = $value; |
|
- | 106 | }
|
|
- | 107 | else if ($value !== null) |
|
- | 108 | {
|
|
- | 109 | $database = new $value(); |
|
- | 110 | if (!($database instanceof Database)) |
|
- | 111 | {
|
|
- | 112 | throw new \InvalidArgumentException( |
|
- | 113 | 'Expected Database instance or string for class name, saw '
|
|
- | 114 | . (\get_class($value) || \gettype($value)) |
|
- | 115 | ); |
|
- | 116 | }
|
|
- | 117 | ||
- | 118 | $this->_database = $database; |
|
- | 119 | }
|
|
- | 120 | }
|
|
- | 121 | ||
- | 122 | /**
|
|
85 | * Initiates a transaction
|
123 | * Initiates a transaction
|
86 | *
|
124 | *
|
87 | * @return bool
|
125 | * @return bool
|
88 | * @see Database::beginTransaction()
|
126 | * @see Database::beginTransaction()
|
89 | */
|
127 | */
|
Line 185... | Line 223... | ||
185 | * <var>$condition</var> are <code>null</code>.
|
223 | * <var>$condition</var> are <code>null</code>.
|
186 | * @see Database::delete()
|
224 | * @see Database::delete()
|
187 | */
|
225 | */
|
188 | public function delete($id, array $condition = null) |
226 | public function delete ($id, array $condition = null) |
189 | {
|
227 | {
|
190 | if (!is_null($id)) |
228 | if ($id !== null) |
191 | {
|
229 | {
|
192 | $condition = array($this->_id => $id); |
230 | $condition = array($this->_id => $id); |
193 | }
|
231 | }
|
194 | else if (is_null($condition)) |
232 | else if ($condition === null) |
195 | {
|
233 | {
|
196 | throw new InvalidArgumentException( |
234 | throw new InvalidArgumentException( |
197 | '$id and $condition cannot both be null'); |
235 | '$id and $condition cannot both be null'); |
198 | }
|
236 | }
|
199 | 237 |