Smutty

MVC Framework

View Code: Smutty_Model_Transaction

Browse: All · Classes · Plugins

1
<?php
2
3
/**
4
* this class allows models to be grouped together into a
5
* "transaction", which means you can deal with them as
6
* a single unit rather than saving/validating them
7
* all individually.
8
*
9
*/
10
12
13
/** the transactions models */
14
var $models;
15
16
/** any errors found */
17
var $errors;
18
19
/**
20
* constructor
21
*
22
* @return nothing
23
*
24
*/
25
26
27
$this->models = array();
28
$this->errors = array();
29
}
30
31
/**
32
* adds a model to the transaction
33
*
34
* @param Smutty_Model $model model to add
35
* @return nothing
36
*
37
*/
38
39
function add( &$model ) {
40
$this->models[] =& $model;
41
}
42
43
/**
44
* checks if all the models in the transaction are valid
45
*
46
* @return boolean transaction validity
47
*
48
*/
49
50
function isValid() {
51
52
// check models for errors
53
$isValid = true;
54
$this->errors = array();
55
$size = sizeof( $this->models );
56
57
// NEED to use "for" not "foreach" cause of refs
58
for ( $i=0; $i<$size; $i++ )
59
if ( !$this->models[$i]->isValid() ) {
60
$isValid = false;
61
$this->errors = array_merge(
62
$this->errors,
63
$this->models[$i]->getErrors()
64
);
65
}
66
67
return $isValid;
68
69
}
70
71
/**
72
* checks all the models in the transaction for validity
73
* before saving them.
74
*
75
* @return boolean if transaction saved
76
*
77
*/
78
79
function save() {
80
81
if ( !$this->isValid() )
82
return false;
83
84
// NEED to use "for" not "foreach" cause of refs
85
$size = sizeof( $this->models );
86
for ( $i=0; $i<$size; $i++ )
87
$this->models[$i]->save();
88
89
return true;
90
91
}
92
93
/**
94
* returns any errors found when trying to save the transaction
95
*
96
* @return array array of error strings
97
*
98
*/
99
100
function getErrors() {
101
return $this->errors;
102
}
103
104
}
105
106
?>

The code shown here is the code that is currently running this site. If you want to view the latest SVN version of the code then go to the Subversion repository.