| 176 | | /** |
| 177 | | * Collection of models to be persisted |
| 178 | | * |
| 179 | | * @var array Doctrine_Record |
| 180 | | */ |
| 181 | | protected $_createOrUpdateCollection = array(); |
| 182 | | |
| 183 | | /** |
| 184 | | * Collection of models to be persisted |
| 185 | | * |
| 186 | | * @var array Doctrine_Record |
| 187 | | */ |
| 188 | | protected $_deleteCollection = array(); |
| 189 | | |
| 190 | | /** |
| 191 | | * Add a model object to the create collection |
| 192 | | * |
| 193 | | * @param Doctrine_Record $model |
| 194 | | */ |
| 195 | | public function registerModelForCreateOrUpdate($model) |
| 196 | | { |
| 197 | | // code to check to see if the model exists already |
| 198 | | if ($this->_existsInCollections($model)) { |
| 199 | | throw new Exception('model already in another collection for this transaction'); |
| 200 | | } |
| 201 | | |
| 202 | | // no? add it |
| 203 | | $this->_createOrUpdateCollection[] = $model; |
| 204 | | } |
| 205 | | |
| 206 | | /** |
| 207 | | * Add a model object to the delete collection |
| 208 | | * |
| 209 | | * @param Doctrine_Record $model |
| 210 | | */ |
| 211 | | public function registerModelForDelete($model) |
| 212 | | { |
| | 176 | /** |
| | 177 | * Collection of models to be persisted |
| | 178 | * |
| | 179 | * @var array Doctrine_Record |
| | 180 | */ |
| | 181 | protected $_createOrUpdateCollection = array(); |
| | 182 | |
| | 183 | /** |
| | 184 | * Collection of models to be persisted |
| | 185 | * |
| | 186 | * @var array Doctrine_Record |
| | 187 | */ |
| | 188 | protected $_deleteCollection = array(); |
| | 189 | |
| | 190 | /** |
| | 191 | * Add a model object to the create collection |
| | 192 | * |
| | 193 | * @param Doctrine_Record $model |
| | 194 | */ |
| | 195 | public function registerModelForCreateOrUpdate($model) |
| | 196 | { |
| 219 | | $this->_deleteCollection[] = $model; |
| 220 | | } |
| 221 | | |
| 222 | | /** |
| 223 | | * Clear the Unit of Work |
| 224 | | */ |
| 225 | | public function ClearAll() |
| 226 | | { |
| 227 | | $this->_deleteCollection = array(); |
| 228 | | $this->_createOrUpdateCollection = array(); |
| 229 | | } |
| 230 | | |
| 231 | | /** |
| 232 | | * Perform a Commit and clear the Unit Of Work. Throw an Exception if it fails and roll back. |
| 233 | | */ |
| 234 | | public function commitAll() |
| 235 | | { |
| 236 | | $manager = Doctrine_Manager::getInstance(); |
| 237 | | $conn = array_pop($manager->getConnections()); |
| 238 | | |
| 239 | | try { |
| 240 | | $conn->beginTransaction(); |
| 241 | | |
| 242 | | $this->performCreatesOrUpdates($conn); |
| 243 | | $this->performDeletes($conn); |
| 244 | | |
| 245 | | $conn->commit(); |
| 246 | | } catch(Doctrine_Exception $e) { |
| 247 | | $conn->rollback(); |
| 248 | | } |
| 249 | | |
| 250 | | $this->ClearAll(); |
| 251 | | } |
| 252 | | |
| 253 | | private function performCreatesOrUpdates($conn) |
| 254 | | { |
| 255 | | foreach ($this->_createOrUpdateCollection as $model) { |
| 256 | | $model->save($conn); |
| 257 | | } |
| 258 | | } |
| 259 | | |
| 260 | | private function performDeletes($conn) |
| 261 | | { |
| 262 | | foreach ($this->_deleteCollection as $model) { |
| 263 | | $model->delete($conn); |
| 264 | | } |
| 265 | | } |
| 266 | | |
| 267 | | private function existsInCollections($model) |
| 268 | | { |
| 269 | | foreach ($this->_createOrUpdateCollection as $m) { |
| 270 | | if ($model->getOid() == $m->getOid()) { |
| 271 | | return true; |
| | 203 | $this->_createOrUpdateCollection[] = $model; |
| | 204 | } |
| | 205 | |
| | 206 | /** |
| | 207 | * Add a model object to the delete collection |
| | 208 | * |
| | 209 | * @param Doctrine_Record $model |
| | 210 | */ |
| | 211 | public function registerModelForDelete($model) |
| | 212 | { |
| | 213 | // code to check to see if the model exists already |
| | 214 | if ($this->_existsInCollections($model)) { |
| | 215 | throw new Exception('model already in another collection for this transaction'); |
| 273 | | } |
| 274 | | |
| 275 | | foreach ($this->_deleteCollection as $m) { |
| 276 | | if ($model->getOid() == $m->getOid()) { |
| 277 | | return true; |
| 278 | | } |
| 279 | | } |
| 280 | | |
| 281 | | return false; |
| 282 | | } |
| | 217 | |
| | 218 | // no? add it |
| | 219 | $this->_deleteCollection[] = $model; |
| | 220 | } |
| | 221 | |
| | 222 | /** |
| | 223 | * Clear the Unit of Work |
| | 224 | */ |
| | 225 | public function ClearAll() |
| | 226 | { |
| | 227 | $this->_deleteCollection = array(); |
| | 228 | $this->_createOrUpdateCollection = array(); |
| | 229 | } |
| | 230 | |
| | 231 | /** |
| | 232 | * Perform a Commit and clear the Unit Of Work. Throw an Exception if it fails and roll back. |
| | 233 | */ |
| | 234 | public function commitAll() |
| | 235 | { |
| | 236 | $manager = Doctrine_Manager::getInstance(); |
| | 237 | $conn = array_pop($manager->getConnections()); |
| | 238 | |
| | 239 | try { |
| | 240 | $conn->beginTransaction(); |
| | 241 | |
| | 242 | $this->performCreatesOrUpdates($conn); |
| | 243 | $this->performDeletes($conn); |
| | 244 | |
| | 245 | $conn->commit(); |
| | 246 | } catch(Doctrine_Exception $e) { |
| | 247 | $conn->rollback(); |
| | 248 | } |
| | 249 | |
| | 250 | $this->ClearAll(); |
| | 251 | } |
| | 252 | |
| | 253 | protected function _performCreatesOrUpdates($conn) |
| | 254 | { |
| | 255 | foreach ($this->_createOrUpdateCollection as $model) { |
| | 256 | $model->save($conn); |
| | 257 | } |
| | 258 | } |
| | 259 | |
| | 260 | protected function _performDeletes($conn) |
| | 261 | { |
| | 262 | foreach ($this->_deleteCollection as $model) { |
| | 263 | $model->delete($conn); |
| | 264 | } |
| | 265 | } |
| | 266 | |
| | 267 | protected function _existsInCollections($model) |
| | 268 | { |
| | 269 | foreach ($this->_createOrUpdateCollection as $m) { |
| | 270 | if ($model->getOid() == $m->getOid()) { |
| | 271 | return true; |
| | 272 | } |
| | 273 | } |
| | 274 | |
| | 275 | foreach ($this->_deleteCollection as $m) { |
| | 276 | if ($model->getOid() == $m->getOid()) { |
| | 277 | return true; |
| | 278 | } |
| | 279 | } |
| | 280 | |
| | 281 | return false; |
| | 282 | } |