Rev 52 | Rev 54 | Go to most recent revision | Show entire file | Ignore whitespace | Details | Blame | Last modification | View Log | RSS feed
Rev 52 | Rev 53 | ||
---|---|---|---|
Line 33... | Line 33... | ||
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()
|
Line 112... | Line 118... | ||
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.
|
Line 145... | Line 150... | ||
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 | *
|