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