1 | <?php |
---|
2 | |
---|
3 | /* |
---|
4 | * $Id: BaseObject.php 1066 2008-07-17 07:33:35Z ron $ |
---|
5 | * |
---|
6 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS |
---|
7 | * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT |
---|
8 | * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR |
---|
9 | * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT |
---|
10 | * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, |
---|
11 | * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT |
---|
12 | * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, |
---|
13 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY |
---|
14 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT |
---|
15 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
---|
16 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
---|
17 | * |
---|
18 | * This software consists of voluntary contributions made by many individuals |
---|
19 | * and is licensed under the LGPL. For more information please see |
---|
20 | * <http://propel.phpdb.org>. |
---|
21 | */ |
---|
22 | |
---|
23 | /** |
---|
24 | * This class contains attributes and methods that are used by all |
---|
25 | * business objects within the system. |
---|
26 | * |
---|
27 | * @author Hans Lellelid <hans@xmpl.org> (Propel) |
---|
28 | * @author Frank Y. Kim <frank.kim@clearink.com> (Torque) |
---|
29 | * @author John D. McNally <jmcnally@collab.net> (Torque) |
---|
30 | * @version $Revision: 1066 $ |
---|
31 | * @package propel.om |
---|
32 | */ |
---|
33 | abstract class BaseObject { |
---|
34 | |
---|
35 | /** |
---|
36 | * attribute to determine if this object has previously been saved. |
---|
37 | * @var boolean |
---|
38 | */ |
---|
39 | private $_new = true; |
---|
40 | |
---|
41 | /** |
---|
42 | * attribute to determine whether this object has been deleted. |
---|
43 | * @var boolean |
---|
44 | */ |
---|
45 | private $_deleted = false; |
---|
46 | |
---|
47 | /** |
---|
48 | * The columns that have been modified in current object. |
---|
49 | * Tracking modified columns allows us to only update modified columns. |
---|
50 | * @var array |
---|
51 | */ |
---|
52 | protected $modifiedColumns = array(); |
---|
53 | |
---|
54 | /** |
---|
55 | * Empty constructor (this allows people with their own BaseObject implementation to use its constructor) |
---|
56 | */ |
---|
57 | public function __construct() { |
---|
58 | |
---|
59 | } |
---|
60 | |
---|
61 | /** |
---|
62 | * Returns whether the object has been modified. |
---|
63 | * |
---|
64 | * @return boolean True if the object has been modified. |
---|
65 | */ |
---|
66 | public function isModified() |
---|
67 | { |
---|
68 | return !empty($this->modifiedColumns); |
---|
69 | } |
---|
70 | |
---|
71 | /** |
---|
72 | * Has specified column been modified? |
---|
73 | * |
---|
74 | * @param string $col |
---|
75 | * @return boolean True if $col has been modified. |
---|
76 | */ |
---|
77 | public function isColumnModified($col) |
---|
78 | { |
---|
79 | return in_array($col, $this->modifiedColumns); |
---|
80 | } |
---|
81 | |
---|
82 | /** |
---|
83 | * Get the columns that have been modified in this object. |
---|
84 | * @return array A unique list of the modified column names for this object. |
---|
85 | */ |
---|
86 | public function getModifiedColumns() |
---|
87 | { |
---|
88 | return array_unique($this->modifiedColumns); |
---|
89 | } |
---|
90 | |
---|
91 | /** |
---|
92 | * Returns whether the object has ever been saved. This will |
---|
93 | * be false, if the object was retrieved from storage or was created |
---|
94 | * and then saved. |
---|
95 | * |
---|
96 | * @return true, if the object has never been persisted. |
---|
97 | */ |
---|
98 | public function isNew() |
---|
99 | { |
---|
100 | return $this->_new; |
---|
101 | } |
---|
102 | |
---|
103 | /** |
---|
104 | * Setter for the isNew attribute. This method will be called |
---|
105 | * by Propel-generated children and Peers. |
---|
106 | * |
---|
107 | * @param boolean $b the state of the object. |
---|
108 | */ |
---|
109 | public function setNew($b) |
---|
110 | { |
---|
111 | $this->_new = (boolean) $b; |
---|
112 | } |
---|
113 | |
---|
114 | /** |
---|
115 | * Whether this object has been deleted. |
---|
116 | * @return boolean The deleted state of this object. |
---|
117 | */ |
---|
118 | public function isDeleted() |
---|
119 | { |
---|
120 | return $this->_deleted; |
---|
121 | } |
---|
122 | |
---|
123 | /** |
---|
124 | * Specify whether this object has been deleted. |
---|
125 | * @param boolean $b The deleted state of this object. |
---|
126 | * @return void |
---|
127 | */ |
---|
128 | public function setDeleted($b) |
---|
129 | { |
---|
130 | $this->_deleted = (boolean) $b; |
---|
131 | } |
---|
132 | |
---|
133 | /** |
---|
134 | * Sets the modified state for the object to be false. |
---|
135 | * @param string $col If supplied, only the specified column is reset. |
---|
136 | * @return void |
---|
137 | */ |
---|
138 | public function resetModified($col = null) |
---|
139 | { |
---|
140 | if ($col !== null) |
---|
141 | { |
---|
142 | while (($offset = array_search($col, $this->modifiedColumns)) !== false) |
---|
143 | array_splice($this->modifiedColumns, $offset, 1); |
---|
144 | } |
---|
145 | else |
---|
146 | { |
---|
147 | $this->modifiedColumns = array(); |
---|
148 | } |
---|
149 | } |
---|
150 | |
---|
151 | /** |
---|
152 | * Compares this with another <code>BaseObject</code> instance. If |
---|
153 | * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to |
---|
154 | * <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>. |
---|
155 | * |
---|
156 | * @param obj The object to compare to. |
---|
157 | * @return Whether equal to the object specified. |
---|
158 | */ |
---|
159 | public function equals($obj) |
---|
160 | { |
---|
161 | $thisclazz = get_class($this); |
---|
162 | if (is_object($obj) && $obj instanceof $thisclazz) { |
---|
163 | if ($this === $obj) { |
---|
164 | return true; |
---|
165 | } elseif ($this->getPrimaryKey() === null || $obj->getPrimaryKey() === null) { |
---|
166 | return false; |
---|
167 | } else { |
---|
168 | return ($this->getPrimaryKey() === $obj->getPrimaryKey()); |
---|
169 | } |
---|
170 | } else { |
---|
171 | return false; |
---|
172 | } |
---|
173 | } |
---|
174 | |
---|
175 | /** |
---|
176 | * If the primary key is not <code>null</code>, return the hashcode of the |
---|
177 | * primary key. Otherwise calls <code>Object.hashCode()</code>. |
---|
178 | * |
---|
179 | * @return int Hashcode |
---|
180 | */ |
---|
181 | public function hashCode() |
---|
182 | { |
---|
183 | $ok = $this->getPrimaryKey(); |
---|
184 | if ($ok === null) { |
---|
185 | return crc32(serialize($this)); |
---|
186 | } |
---|
187 | return crc32(serialize($ok)); // serialize because it could be an array ("ComboKey") |
---|
188 | } |
---|
189 | |
---|
190 | /** |
---|
191 | * Logs a message using Propel::log(). |
---|
192 | * |
---|
193 | * @param string $msg |
---|
194 | * @param int $priority One of the Propel::LOG_* logging levels |
---|
195 | * @return boolean |
---|
196 | */ |
---|
197 | protected function log($msg, $priority = Propel::LOG_INFO) |
---|
198 | { |
---|
199 | return Propel::log(get_class($this) . ': ' . $msg, $priority); |
---|
200 | } |
---|
201 | |
---|
202 | } |
---|