| 1 |
lars |
1 |
<?php
|
|
|
2 |
|
|
|
3 |
/*
|
|
|
4 |
* $Id: BaseObject.php 1262 2009-10-26 20:54:39Z francois $
|
|
|
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: 1262 $
|
|
|
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 |
* Code to be run before persisting the object
|
|
|
135 |
* @param PropelPDO $con
|
|
|
136 |
* @return bloolean
|
|
|
137 |
*/
|
|
|
138 |
public function preSave(PropelPDO $con = null)
|
|
|
139 |
{
|
|
|
140 |
return true;
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
/**
|
|
|
144 |
* Code to be run after persisting the object
|
|
|
145 |
* @param PropelPDO $con
|
|
|
146 |
*/
|
|
|
147 |
public function postSave(PropelPDO $con = null) { }
|
|
|
148 |
|
|
|
149 |
/**
|
|
|
150 |
* Code to be run before inserting to database
|
|
|
151 |
* @param PropelPDO $con
|
|
|
152 |
* @return boolean
|
|
|
153 |
*/
|
|
|
154 |
public function preInsert(PropelPDO $con = null)
|
|
|
155 |
{
|
|
|
156 |
return true;
|
|
|
157 |
}
|
|
|
158 |
|
|
|
159 |
/**
|
|
|
160 |
* Code to be run after inserting to database
|
|
|
161 |
* @param PropelPDO $con
|
|
|
162 |
*/
|
|
|
163 |
public function postInsert(PropelPDO $con = null) { }
|
|
|
164 |
|
|
|
165 |
/**
|
|
|
166 |
* Code to be run before updating the object in database
|
|
|
167 |
* @param PropelPDO $con
|
|
|
168 |
* @return boolean
|
|
|
169 |
*/
|
|
|
170 |
public function preUpdate(PropelPDO $con = null)
|
|
|
171 |
{
|
|
|
172 |
return true;
|
|
|
173 |
}
|
|
|
174 |
|
|
|
175 |
/**
|
|
|
176 |
* Code to be run after updating the object in database
|
|
|
177 |
* @param PropelPDO $con
|
|
|
178 |
*/
|
|
|
179 |
public function postUpdate(PropelPDO $con = null) { }
|
|
|
180 |
|
|
|
181 |
/**
|
|
|
182 |
* Code to be run before deleting the object in database
|
|
|
183 |
* @param PropelPDO $con
|
|
|
184 |
* @return boolean
|
|
|
185 |
*/
|
|
|
186 |
public function preDelete(PropelPDO $con = null)
|
|
|
187 |
{
|
|
|
188 |
return true;
|
|
|
189 |
}
|
|
|
190 |
|
|
|
191 |
/**
|
|
|
192 |
* Code to be run after deleting the object in database
|
|
|
193 |
* @param PropelPDO $con
|
|
|
194 |
*/
|
|
|
195 |
public function postDelete(PropelPDO $con = null) { }
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* Sets the modified state for the object to be false.
|
|
|
199 |
* @param string $col If supplied, only the specified column is reset.
|
|
|
200 |
* @return void
|
|
|
201 |
*/
|
|
|
202 |
public function resetModified($col = null)
|
|
|
203 |
{
|
|
|
204 |
if ($col !== null)
|
|
|
205 |
{
|
|
|
206 |
while (($offset = array_search($col, $this->modifiedColumns)) !== false)
|
|
|
207 |
array_splice($this->modifiedColumns, $offset, 1);
|
|
|
208 |
}
|
|
|
209 |
else
|
|
|
210 |
{
|
|
|
211 |
$this->modifiedColumns = array();
|
|
|
212 |
}
|
|
|
213 |
}
|
|
|
214 |
|
|
|
215 |
/**
|
|
|
216 |
* Compares this with another <code>BaseObject</code> instance. If
|
|
|
217 |
* <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
|
|
|
218 |
* <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>.
|
|
|
219 |
*
|
|
|
220 |
* @param obj The object to compare to.
|
|
|
221 |
* @return Whether equal to the object specified.
|
|
|
222 |
*/
|
|
|
223 |
public function equals($obj)
|
|
|
224 |
{
|
|
|
225 |
$thisclazz = get_class($this);
|
|
|
226 |
if (is_object($obj) && $obj instanceof $thisclazz) {
|
|
|
227 |
if ($this === $obj) {
|
|
|
228 |
return true;
|
|
|
229 |
} elseif ($this->getPrimaryKey() === null || $obj->getPrimaryKey() === null) {
|
|
|
230 |
return false;
|
|
|
231 |
} else {
|
|
|
232 |
return ($this->getPrimaryKey() === $obj->getPrimaryKey());
|
|
|
233 |
}
|
|
|
234 |
} else {
|
|
|
235 |
return false;
|
|
|
236 |
}
|
|
|
237 |
}
|
|
|
238 |
|
|
|
239 |
/**
|
|
|
240 |
* If the primary key is not <code>null</code>, return the hashcode of the
|
|
|
241 |
* primary key. Otherwise calls <code>Object.hashCode()</code>.
|
|
|
242 |
*
|
|
|
243 |
* @return int Hashcode
|
|
|
244 |
*/
|
|
|
245 |
public function hashCode()
|
|
|
246 |
{
|
|
|
247 |
$ok = $this->getPrimaryKey();
|
|
|
248 |
if ($ok === null) {
|
|
|
249 |
return crc32(serialize($this));
|
|
|
250 |
}
|
|
|
251 |
return crc32(serialize($ok)); // serialize because it could be an array ("ComboKey")
|
|
|
252 |
}
|
|
|
253 |
|
|
|
254 |
/**
|
|
|
255 |
* Logs a message using Propel::log().
|
|
|
256 |
*
|
|
|
257 |
* @param string $msg
|
|
|
258 |
* @param int $priority One of the Propel::LOG_* logging levels
|
|
|
259 |
* @return boolean
|
|
|
260 |
*/
|
|
|
261 |
protected function log($msg, $priority = Propel::LOG_INFO)
|
|
|
262 |
{
|
|
|
263 |
return Propel::log(get_class($this) . ': ' . $msg, $priority);
|
|
|
264 |
}
|
|
|
265 |
|
|
|
266 |
}
|