Rev 52 | Rev 54 | Go to most recent revision | Only display areas with differences | Regard whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 52 | Rev 53 | ||
---|---|---|---|
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 $_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
|
|
- | 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 | *
|
|
38 | * @type array
|
44 | * @type array
|
39 | */
|
45 | */
|
40 | protected $_persistentProperties = array('id'); |
46 | protected $_persistentProperties = array(); |
41 | 47 | ||
42 | /**
|
48 | /**
|
43 | * Creates a new model object
|
49 | * Creates a new model object
|
44 | *
|
50 | *
|
45 | * @see AbstractModel::__construct()
|
51 | * @see AbstractModel::__construct()
|
46 | */
|
52 | */
|
47 | public function __construct (array $data = null, array $mapping = null) |
53 | public function __construct (array $data = null, array $mapping = null) |
48 | {
|
54 | {
|
49 | parent::__construct($data, $mapping); |
55 | parent::__construct($data, $mapping); |
50 | }
|
56 | }
|
51 | 57 | ||
52 | public function getPersistentTable () |
58 | public function getPersistentTable () |
53 | {
|
59 | {
|
54 | if (is_string($this->_persistentTable)) |
60 | if (is_string($this->_persistentTable)) |
55 | {
|
61 | {
|
56 | /* Call setter to convert to Table */
|
62 | /* Call setter to convert to Table */
|
57 | $this->persistentTable = $this->_persistentTable; |
63 | $this->persistentTable = $this->_persistentTable; |
58 | }
|
64 | }
|
59 | 65 | ||
60 | return $this->_persistentTable; |
66 | return $this->_persistentTable; |
61 | }
|
67 | }
|
62 | 68 | ||
63 | public function setPersistentTable ($value) |
69 | public function setPersistentTable ($value) |
64 | {
|
70 | {
|
65 | if ($value instanceof Table) |
71 | if ($value instanceof Table) |
66 | {
|
72 | {
|
67 | $this->_persistentTable = $value; |
73 | $this->_persistentTable = $value; |
68 | }
|
74 | }
|
69 | else
|
75 | else
|
70 | {
|
76 | {
|
71 | $table = new $value(); |
77 | $table = new $value(); |
72 | if (!($table instanceof Db\Table)) |
78 | if (!($table instanceof Db\Table)) |
73 | {
|
79 | {
|
74 | throw new \InvalidArgumentException( |
80 | throw new \InvalidArgumentException( |
75 | 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
|
81 | 'Parameter does not specify a subclass of \\PointedEars\\PHPX\\Table: '
|
76 | . $value |
82 | . $value |
77 | ); |
83 | ); |
78 | }
|
84 | }
|
79 | 85 | ||
80 | $this->_persistentTable = $table; |
86 | $this->_persistentTable = $table; |
81 | }
|
87 | }
|
82 | }
|
88 | }
|
83 | 89 | ||
84 | /**
|
90 | /**
|
85 | * Returns an array for database queries containing the
|
91 | * Returns an array for database queries containing the
|
86 | * property values of this object, using the specified
|
92 | * property values of this object, using the specified
|
87 | * property-to-column mapping.
|
93 | * property-to-column mapping.
|
88 | *
|
94 | *
|
89 | * @param array $propertyNames = null
|
95 | * @param array $propertyNames = null
|
90 | * Names of the properties that should be included.
|
96 | * Names of the properties that should be included.
|
91 | * The default is to include all persistent properties.
|
97 | * The default is to include all persistent properties.
|
92 | * @return array
|
98 | * @return array
|
93 | */
|
99 | */
|
94 | public function getPropertyArray (array $propertyNames = null) |
100 | public function getPropertyArray (array $propertyNames = null) |
95 | {
|
101 | {
|
96 | $a = array(); |
102 | $a = array(); |
97 | 103 | ||
98 | if ($propertyNames === null) |
104 | if ($propertyNames === null) |
99 | {
|
105 | {
|
100 | $propertyNames = $this->_persistentProperties; |
106 | $propertyNames = $this->_persistentProperties; |
101 | }
|
107 | }
|
102 | 108 | ||
103 | foreach ($propertyNames as $propertyName => $columnName) |
109 | foreach ($propertyNames as $propertyName => $columnName) |
104 | {
|
110 | {
|
105 | if (is_numeric($propertyName)) |
111 | if (is_numeric($propertyName)) |
106 | {
|
112 | {
|
107 | $propertyName = $columnName; |
113 | $propertyName = $columnName; |
108 | }
|
114 | }
|
109 | 115 | ||
110 | $a[$columnName] = $this->$propertyName; |
116 | $a[$columnName] = $this->$propertyName; |
111 | }
|
117 | }
|
112 | 118 | ||
113 | return $a; |
119 | return $a; |
114 | }
|
120 | }
|
115 | 121 | ||
116 | /**
|
122 | /**
|
117 | * Finds the record for the model object in a database, fills
|
123 | * Finds the record for the model object in the table, fills
|
118 | * the object with missing data, and returns the result.
|
124 | * the object with missing data, and returns the result.
|
119 | *
|
125 | *
|
120 | * @see Table::find(Model)
|
126 | * @see Table::find(Model)
|
121 | * @return Model|null
|
127 | * @return Model|null
|
122 | * This object filled with missing data, or <code>null</code>
|
128 | * This object filled with missing data, or <code>null</code>
|
123 | * if there is no data for this object
|
129 | * if there is no data for this object
|
124 | */
|
130 | */
|
125 | public function find () |
131 | public function find () |
126 | {
|
132 | {
|
127 | $result = $this->persistentTable->find( |
133 | $result = $this->persistentTable->find($this->{$this->_persistentId}); |
128 | $this->{$this->_persistentId}); |
- | |
129 | if ($result) |
134 | if ($result) |
130 | {
|
135 | {
|
131 | return $this->map($result); |
136 | return $this->map($result); |
132 | }
|
137 | }
|
133 | 138 | ||
134 | return null; |
139 | return null; |
135 | }
|
140 | }
|
136 | 141 | ||
137 | /**
|
142 | /**
|
138 | * Saves a model object in the <code>Table</code>
|
143 | * Saves the model object in the table
|
139 | *
|
144 | *
|
140 | * @param array $propertyNames = null
|
145 | * @param array $propertyNames = null
|
141 | * Names of the properties whose values should be saved
|
146 | * Names of the properties whose values should be saved
|
142 | * in the database. The default is to save the values
|
147 | * in the database. The default is to save the values
|
143 | * of all persistent properties.
|
148 | * of all persistent properties.
|
144 | * @return boolean
|
149 | * @return boolean
|
145 | * @see Model::getPropertyArray()
|
150 | * @see Model::getPropertyArray()
|
146 | * @see Table::updateOrInsert()
|
151 | * @see Table::updateOrInsert()
|
147 | */
|
152 | */
|
148 | public function save (array $propertyNames = null) |
153 | public function save (array $propertyNames = null) |
149 | {
|
154 | {
|
- | 155 | $table = $this->persistentTable; |
|
- | 156 | $idPropertyName = $this->_persistentId; |
|
- | 157 | ||
150 | return $this->persistentTable->updateOrInsert( |
158 | $result = $table->updateOrInsert( |
151 | $this->getPropertyArray($propertyNames), array( |
159 | $this->getPropertyArray($propertyNames), |
- | 160 | array( |
|
152 | $this->persistentTable->id => $this->{$this->_persistentId} |
161 | $table->id => $this->$idPropertyName |
153 | )
|
162 | )
|
154 | ); |
163 | ); |
- | 164 | ||
- | 165 | if ($result && ($lastInsertId = $table->lastInsertId)) |
|
- | 166 | {
|
|
- | 167 | $this->$idPropertyName = $lastInsertId; |
|
- | 168 | }
|
|
- | 169 | ||
- | 170 | return $result; |
|
- | 171 | }
|
|
- | 172 | ||
- | 173 | /**
|
|
- | 174 | * Inserts the model object into the table
|
|
- | 175 | *
|
|
- | 176 | * @param array $propertyNames = null
|
|
- | 177 | * Names of the properties whose values should be insert
|
|
- | 178 | * in the database. The default is to insert the values
|
|
- | 179 | * of all persistent properties.
|
|
- | 180 | * @return boolean
|
|
- | 181 | * @see Model::getPropertyArray()
|
|
- | 182 | * @see Table::insert()
|
|
- | 183 | */
|
|
- | 184 | public function insert (array $propertyNames = null) |
|
- | 185 | {
|
|
- | 186 | $table = $this->persistentTable; |
|
- | 187 | $idPropertyName = $this->_persistentId; |
|
- | 188 | ||
- | 189 | $result = $table->insert($this->getPropertyArray($propertyNames)); |
|
- | 190 | ||
- | 191 | if ($result && ($lastInsertId = $table->lastInsertId)) |
|
- | 192 | {
|
|
- | 193 | $this->$idPropertyName = $lastInsertId; |
|
- | 194 | }
|
|
- | 195 | ||
- | 196 | return $result; |
|
155 | }
|
197 | }
|
156 | 198 | ||
157 | /**
|
199 | /**
|
158 | * Deletes a model object from the <code>Table</code>
|
200 | * Deletes a model object from the <code>Table</code>
|
159 | *
|
201 | *
|
160 | * @return bool
|
202 | * @return bool
|
161 | * @see Table::delete()
|
203 | * @see Table::delete()
|
162 | */
|
204 | */
|
163 | public function delete () |
205 | public function delete () |
164 | {
|
206 | {
|
165 | return $this->persistentTable->delete($this->{$this->_persistentId}); |
207 | return $this->persistentTable->delete($this->{$this->_persistentId}); |
166 | }
|
208 | }
|
167 | }
|
209 | }
|
168 | 210 |