Rev 53 | Rev 55 | Go to most recent revision | Only display areas with differences | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 53 | Rev 54 | ||
---|---|---|---|
1 | <?php
|
1 | <?php
|
2 | 2 | ||
3 | namespace PointedEars\PHPX; |
3 | namespace PointedEars\PHPX; |
4 | 4 | ||
5 | /**
|
5 | /**
|
6 | * Abstract model class for Object-Relational Mapping
|
6 | * Abstract model class for Object-Relational Mapping
|
7 | *
|
7 | *
|
8 | * Provides simple mapping of a model object to records of
|
8 | * Provides simple mapping of a model object to records of
|
9 | * a table of a relational database.
|
9 | * a table of a relational database.
|
10 | *
|
10 | *
|
11 | * @author Thomas Lahn
|
11 | * @author Thomas Lahn
|
12 | */
|
12 | */
|
13 | abstract class Model extends \PointedEars\PHPX\AbstractModel |
13 | abstract class Model extends \PointedEars\PHPX\AbstractModel |
14 | {
|
14 | {
|
15 | /**
|
15 | /**
|
16 | * The <code>Table</code> for instances of this model
|
16 | * The <code>Table</code> for instances of this model
|
17 | *
|
17 | *
|
18 | * @type Table|string
|
18 | * @type Table|string
|
19 | */
|
19 | */
|
20 | protected $_persistentTable; |
20 | protected static $_persistentTable; |
21 | 21 | ||
22 | /**
|
22 | /**
|
23 | * The name(s) of the property or properties whose value(s)
|
23 | * The name(s) of the property or properties whose value(s)
|
24 | * identify this object in the <code>Table</code>. They are
|
24 | * identify this object in the <code>Table</code>. They are
|
25 | * used for comparing against the primary key column(s) of
|
25 | * used for comparing against the primary key column(s) of
|
26 | * the <code>Table</code>.
|
26 | * the <code>Table</code>.
|
27 | *
|
27 | *
|
28 | * @type string|array[string]
|
28 | * @type string|array[string]
|
29 | */
|
29 | */
|
30 | protected $_persistentId = 'id'; |
30 | protected $_persistentId = 'id'; |
31 | 31 | ||
32 | /**
|
32 | /**
|
33 | * The names of the properties that should be used in database
|
33 | * The names of the properties that should be used in database
|
34 | * queries, and their mapping to the columns of
|
34 | * queries, and their mapping to the columns of
|
35 | * the <code>Table</code>, if specified (keys are property names,
|
35 | * the <code>Table</code>, if specified (keys are property names,
|
36 | * values are column names, or both if the key is numeric).
|
36 | * values are column names, or both if the key is numeric).
|
37 | *
|
37 | *
|
38 | * NOTE: It should not be necessary to include the
|
38 | * NOTE: It should not be necessary to include the
|
39 | * <code>persistentId</code> property value here. If an object
|
39 | * <code>persistentId</code> property value here. If an object
|
40 | * is not in the database, it should be assigned an ID
|
40 | * is not in the database, it should be assigned an ID
|
41 | * automatically when saved; if it is in the database,
|
41 | * automatically when saved; if it is in the database,
|
42 | * you already have its ID as you searched by it.
|
42 | * you already have its ID as you searched by it.
|
43 | *
|
43 | *
|
44 | * @type array
|
44 | * @type array
|
45 | */
|
45 | */
|
46 | protected $_persistentProperties = array(); |
46 | protected $_persistentProperties = array(); |
47 | 47 | ||
48 | /**
|
48 | /**
|
49 | * Creates a new model object
|
49 | * Creates a new model object
|
50 | *
|
50 | *
|
51 | * @see AbstractModel::__construct()
|
51 | * @see AbstractModel::__construct()
|
52 | */
|
52 | */
|
53 | public function __construct (array $data = null, array $mapping = null) |
53 | public function __construct (array $data = null, array $mapping = null) |
54 | {
|
54 | {
|
55 | parent::__construct($data, $mapping); |
55 | parent::__construct($data, $mapping); |
56 | }
|
56 | }
|
57 | 57 | ||
58 | public function getPersistentTable () |
58 | public function getPersistentTable () |
59 | {
|
59 | {
|
- | 60 | $class = \get_class($this); |
|
60 | if (is_string($this->_persistentTable)) |
61 | if (\is_string($class::$_persistentTable)) |
61 | {
|
62 | {
|
62 | /* Call setter to convert to Table */
|
63 | /* Call setter to convert to Table */
|
63 | $this->persistentTable = $this->_persistentTable; |
64 | $this->setPersistentTable($class::$_persistentTable); |
64 | }
|
65 | }
|
65 | 66 | ||
66 | return $this->_persistentTable; |
67 | return $class::$_persistentTable; |
67 | }
|
68 | }
|
68 | 69 | ||
69 | public function setPersistentTable ($value) |
70 | public function setPersistentTable ($value) |
70 | {
|
71 | {
|
- | 72 | $class = \get_class($this); |
|
71 | if ($value instanceof Table) |
73 | if ($value instanceof Table) |
72 | {
|
74 | {
|
73 | $this->_persistentTable = $value; |
75 | $class::$_persistentTable = $value; |
74 | }
|
76 | }
|
75 | else
|
77 | else
|
76 | {
|
78 | {
|
77 | $table = new $value(); |
79 | $table = new $value(); |
78 | if (!($table instanceof Db\Table)) |
80 | if (!($table instanceof Db\Table)) |
79 | {
|
81 | {
|
80 | throw new \InvalidArgumentException( |
82 | throw new \InvalidArgumentException( |
81 | 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
|
83 | 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
|
82 | . $value |
84 | . $value |
83 | ); |
85 | ); |
84 | }
|
86 | }
|
85 | 87 | ||
86 | $this->_persistentTable = $table; |
88 | $class::$_persistentTable = $table; |
87 | }
|
89 | }
|
88 | }
|
90 | }
|
89 | 91 | ||
90 | /**
|
92 | /**
|
91 | * Returns an array for database queries containing the
|
93 | * Returns an array for database queries containing the
|
92 | * property values of this object, using the specified
|
94 | * property values of this object, using the specified
|
93 | * property-to-column mapping.
|
95 | * property-to-column mapping.
|
94 | *
|
96 | *
|
95 | * @param array $propertyNames = null
|
97 | * @param array $propertyNames = null
|
96 | * Names of the properties that should be included.
|
98 | * Names of the properties that should be included.
|
97 | * The default is to include all persistent properties.
|
99 | * The default is to include all persistent properties.
|
98 | * @return array
|
100 | * @return array
|
99 | */
|
101 | */
|
100 | public function getPropertyArray (array $propertyNames = null) |
102 | public function getPropertyArray (array $propertyNames = null) |
101 | {
|
103 | {
|
102 | $a = array(); |
104 | $a = array(); |
103 | 105 | ||
104 | if ($propertyNames === null) |
106 | if ($propertyNames === null) |
105 | {
|
107 | {
|
106 | $propertyNames = $this->_persistentProperties; |
108 | $propertyNames = $this->_persistentProperties; |
107 | }
|
109 | }
|
108 | 110 | ||
109 | foreach ($propertyNames as $propertyName => $columnName) |
111 | foreach ($propertyNames as $propertyName => $columnName) |
110 | {
|
112 | {
|
111 | if (is_numeric($propertyName)) |
113 | if (is_numeric($propertyName)) |
112 | {
|
114 | {
|
113 | $propertyName = $columnName; |
115 | $propertyName = $columnName; |
114 | }
|
116 | }
|
115 | 117 | ||
116 | $a[$columnName] = $this->$propertyName; |
118 | $a[$columnName] = $this->$propertyName; |
117 | }
|
119 | }
|
118 | 120 | ||
119 | return $a; |
121 | return $a; |
120 | }
|
122 | }
|
121 | 123 | ||
122 | /**
|
124 | /**
|
123 | * Finds the record for the model object in the table, fills
|
125 | * Finds the record for the model object in the table, fills
|
124 | * the object with missing data, and returns the result.
|
126 | * the object with missing data, and returns the result.
|
125 | *
|
127 | *
|
126 | * @see Table::find(Model)
|
128 | * @see Table::find(Model)
|
127 | * @return Model|null
|
129 | * @return Model|null
|
128 | * This object filled with missing data, or <code>null</code>
|
130 | * This object filled with missing data, or <code>null</code>
|
129 | * if there is no data for this object
|
131 | * if there is no data for this object
|
130 | */
|
132 | */
|
131 | public function find () |
133 | public function find () |
132 | {
|
134 | {
|
- | 135 | $class = \get_class($this); |
|
133 | $result = $this->persistentTable->find($this->{$this->_persistentId}); |
136 | $result = $this->persistentTable->find($this->{$this->_persistentId}); |
134 | if ($result) |
137 | if ($result) |
135 | {
|
138 | {
|
136 | return $this->map($result); |
139 | return $this->map($result); |
137 | }
|
140 | }
|
138 | 141 | ||
139 | return null; |
142 | return null; |
140 | }
|
143 | }
|
141 | 144 | ||
142 | /**
|
145 | /**
|
143 | * Saves the model object in the table
|
146 | * Saves the model object in the table
|
144 | *
|
147 | *
|
145 | * @param array $propertyNames = null
|
148 | * @param array $propertyNames = null
|
146 | * Names of the properties whose values should be saved
|
149 | * Names of the properties whose values should be saved
|
147 | * in the database. The default is to save the values
|
150 | * in the database. The default is to save the values
|
148 | * of all persistent properties.
|
151 | * of all persistent properties.
|
149 | * @return boolean
|
152 | * @return boolean
|
150 | * @see Model::getPropertyArray()
|
153 | * @see Model::getPropertyArray()
|
151 | * @see Table::updateOrInsert()
|
154 | * @see Table::updateOrInsert()
|
152 | */
|
155 | */
|
153 | public function save (array $propertyNames = null) |
156 | public function save (array $propertyNames = null) |
154 | {
|
157 | {
|
155 | $table = $this->persistentTable; |
158 | $table = $this->persistentTable; |
156 | $idPropertyName = $this->_persistentId; |
159 | $idPropertyName = $this->_persistentId; |
157 | 160 | ||
158 | $result = $table->updateOrInsert( |
161 | $result = $table->updateOrInsert( |
159 | $this->getPropertyArray($propertyNames), |
162 | $this->getPropertyArray($propertyNames), |
160 | array( |
163 | array( |
161 | $table->id => $this->$idPropertyName |
164 | $table->id => $this->$idPropertyName |
162 | )
|
165 | )
|
163 | ); |
166 | ); |
164 | 167 | ||
165 | if ($result && ($lastInsertId = $table->lastInsertId)) |
168 | if ($result && ($lastInsertId = $table->lastInsertId)) |
166 | {
|
169 | {
|
167 | $this->$idPropertyName = $lastInsertId; |
170 | $this->$idPropertyName = $lastInsertId; |
168 | }
|
171 | }
|
169 | 172 | ||
170 | return $result; |
173 | return $result; |
171 | }
|
174 | }
|
172 | 175 | ||
173 | /**
|
176 | /**
|
174 | * Inserts the model object into the table
|
177 | * Inserts the model object into the table
|
175 | *
|
178 | *
|
176 | * @param array $propertyNames = null
|
179 | * @param array $propertyNames = null
|
177 | * Names of the properties whose values should be insert
|
180 | * Names of the properties whose values should be insert
|
178 | * in the database. The default is to insert the values
|
181 | * in the database. The default is to insert the values
|
179 | * of all persistent properties.
|
182 | * of all persistent properties.
|
180 | * @return boolean
|
183 | * @return boolean
|
181 | * @see Model::getPropertyArray()
|
184 | * @see Model::getPropertyArray()
|
182 | * @see Table::insert()
|
185 | * @see Table::insert()
|
183 | */
|
186 | */
|
184 | public function insert (array $propertyNames = null) |
187 | public function insert (array $propertyNames = null) |
185 | {
|
188 | {
|
186 | $table = $this->persistentTable; |
189 | $table = $this->persistentTable; |
187 | $idPropertyName = $this->_persistentId; |
190 | $idPropertyName = $this->_persistentId; |
188 | 191 | ||
189 | $result = $table->insert($this->getPropertyArray($propertyNames)); |
192 | $result = $table->insert($this->getPropertyArray($propertyNames)); |
190 | 193 | ||
191 | if ($result && ($lastInsertId = $table->lastInsertId)) |
194 | if ($result && ($lastInsertId = $table->lastInsertId)) |
192 | {
|
195 | {
|
193 | $this->$idPropertyName = $lastInsertId; |
196 | $this->$idPropertyName = $lastInsertId; |
194 | }
|
197 | }
|
195 | 198 | ||
196 | return $result; |
199 | return $result; |
197 | }
|
200 | }
|
198 | 201 | ||
199 | /**
|
202 | /**
|
200 | * Deletes a model object from the <code>Table</code>
|
203 | * Deletes a model object from the <code>Table</code>
|
201 | *
|
204 | *
|
202 | * @return bool
|
205 | * @return bool
|
203 | * @see Table::delete()
|
206 | * @see Table::delete()
|
204 | */
|
207 | */
|
205 | public function delete () |
208 | public function delete () |
206 | {
|
209 | {
|
207 | return $this->persistentTable->delete($this->{$this->_persistentId}); |
210 | return $this->persistentTable->delete($this->{$this->_persistentId}); |
208 | }
|
211 | }
|
209 | }
|
212 | }
|
210 | 213 |