BaseObject.php 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320
  1. <?php
  2. /**
  3. * This file is part of the Propel package.
  4. * For the full copyright and license information, please view the LICENSE
  5. * file that was distributed with this source code.
  6. *
  7. * @license MIT License
  8. */
  9. /**
  10. * This class contains attributes and methods that are used by all
  11. * business objects within the system.
  12. *
  13. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  14. * @author Frank Y. Kim <frank.kim@clearink.com> (Torque)
  15. * @author John D. McNally <jmcnally@collab.net> (Torque)
  16. * @version $Revision: 1673 $
  17. * @package propel.runtime.om
  18. */
  19. abstract class BaseObject
  20. {
  21. /**
  22. * attribute to determine if this object has previously been saved.
  23. * @var boolean
  24. */
  25. protected $_new = true;
  26. /**
  27. * attribute to determine whether this object has been deleted.
  28. * @var boolean
  29. */
  30. protected $_deleted = false;
  31. /**
  32. * The columns that have been modified in current object.
  33. * Tracking modified columns allows us to only update modified columns.
  34. * @var array
  35. */
  36. protected $modifiedColumns = array();
  37. /**
  38. * The (virtual) columns that are added at runtime
  39. * The formatters can add supplementary columns based on a resultset
  40. * @var array
  41. */
  42. protected $virtualColumns = array();
  43. /**
  44. * Empty constructor (this allows people with their own BaseObject implementation to use its constructor)
  45. */
  46. public function __construct() {
  47. }
  48. /**
  49. * Returns whether the object has been modified.
  50. *
  51. * @return boolean True if the object has been modified.
  52. */
  53. public function isModified()
  54. {
  55. return !empty($this->modifiedColumns);
  56. }
  57. /**
  58. * Has specified column been modified?
  59. *
  60. * @param string $col column fully qualified name (BasePeer::TYPE_COLNAME), e.g. Book::AUTHOR_ID
  61. * @return boolean True if $col has been modified.
  62. */
  63. public function isColumnModified($col)
  64. {
  65. return in_array($col, $this->modifiedColumns);
  66. }
  67. /**
  68. * Get the columns that have been modified in this object.
  69. * @return array A unique list of the modified column names for this object.
  70. */
  71. public function getModifiedColumns()
  72. {
  73. return array_unique($this->modifiedColumns);
  74. }
  75. /**
  76. * Returns whether the object has ever been saved. This will
  77. * be false, if the object was retrieved from storage or was created
  78. * and then saved.
  79. *
  80. * @return true, if the object has never been persisted.
  81. */
  82. public function isNew()
  83. {
  84. return $this->_new;
  85. }
  86. /**
  87. * Setter for the isNew attribute. This method will be called
  88. * by Propel-generated children and Peers.
  89. *
  90. * @param boolean $b the state of the object.
  91. */
  92. public function setNew($b)
  93. {
  94. $this->_new = (boolean) $b;
  95. }
  96. /**
  97. * Whether this object has been deleted.
  98. * @return boolean The deleted state of this object.
  99. */
  100. public function isDeleted()
  101. {
  102. return $this->_deleted;
  103. }
  104. /**
  105. * Specify whether this object has been deleted.
  106. * @param boolean $b The deleted state of this object.
  107. * @return void
  108. */
  109. public function setDeleted($b)
  110. {
  111. $this->_deleted = (boolean) $b;
  112. }
  113. /**
  114. * Code to be run before persisting the object
  115. * @param PropelPDO $con
  116. * @return bloolean
  117. */
  118. public function preSave(PropelPDO $con = null)
  119. {
  120. return true;
  121. }
  122. /**
  123. * Code to be run after persisting the object
  124. * @param PropelPDO $con
  125. */
  126. public function postSave(PropelPDO $con = null) { }
  127. /**
  128. * Code to be run before inserting to database
  129. * @param PropelPDO $con
  130. * @return boolean
  131. */
  132. public function preInsert(PropelPDO $con = null)
  133. {
  134. return true;
  135. }
  136. /**
  137. * Code to be run after inserting to database
  138. * @param PropelPDO $con
  139. */
  140. public function postInsert(PropelPDO $con = null) { }
  141. /**
  142. * Code to be run before updating the object in database
  143. * @param PropelPDO $con
  144. * @return boolean
  145. */
  146. public function preUpdate(PropelPDO $con = null)
  147. {
  148. return true;
  149. }
  150. /**
  151. * Code to be run after updating the object in database
  152. * @param PropelPDO $con
  153. */
  154. public function postUpdate(PropelPDO $con = null) { }
  155. /**
  156. * Code to be run before deleting the object in database
  157. * @param PropelPDO $con
  158. * @return boolean
  159. */
  160. public function preDelete(PropelPDO $con = null)
  161. {
  162. return true;
  163. }
  164. /**
  165. * Code to be run after deleting the object in database
  166. * @param PropelPDO $con
  167. */
  168. public function postDelete(PropelPDO $con = null) { }
  169. /**
  170. * Sets the modified state for the object to be false.
  171. * @param string $col If supplied, only the specified column is reset.
  172. * @return void
  173. */
  174. public function resetModified($col = null)
  175. {
  176. if ($col !== null) {
  177. while (($offset = array_search($col, $this->modifiedColumns)) !== false) {
  178. array_splice($this->modifiedColumns, $offset, 1);
  179. }
  180. } else {
  181. $this->modifiedColumns = array();
  182. }
  183. }
  184. /**
  185. * Compares this with another <code>BaseObject</code> instance. If
  186. * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
  187. * <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>.
  188. *
  189. * @param obj The object to compare to.
  190. * @return Whether equal to the object specified.
  191. */
  192. public function equals($obj)
  193. {
  194. $thisclazz = get_class($this);
  195. if (is_object($obj) && $obj instanceof $thisclazz) {
  196. if ($this === $obj) {
  197. return true;
  198. } elseif ($this->getPrimaryKey() === null || $obj->getPrimaryKey() === null) {
  199. return false;
  200. } else {
  201. return ($this->getPrimaryKey() === $obj->getPrimaryKey());
  202. }
  203. } else {
  204. return false;
  205. }
  206. }
  207. /**
  208. * If the primary key is not <code>null</code>, return the hashcode of the
  209. * primary key. Otherwise calls <code>Object.hashCode()</code>.
  210. *
  211. * @return int Hashcode
  212. */
  213. public function hashCode()
  214. {
  215. $ok = $this->getPrimaryKey();
  216. if ($ok === null) {
  217. return crc32(serialize($this));
  218. }
  219. return crc32(serialize($ok)); // serialize because it could be an array ("ComboKey")
  220. }
  221. /**
  222. * Get the associative array of the virtual columns in this object
  223. *
  224. * @param string $name The virtual column name
  225. *
  226. * @return array
  227. */
  228. public function getVirtualColumns()
  229. {
  230. return $this->virtualColumns;
  231. }
  232. /**
  233. * Checks the existence of a virtual column in this object
  234. *
  235. * @return boolean
  236. */
  237. public function hasVirtualColumn($name)
  238. {
  239. return array_key_exists($name, $this->virtualColumns);
  240. }
  241. /**
  242. * Get the value of a virtual column in this object
  243. *
  244. * @return mixed
  245. */
  246. public function getVirtualColumn($name)
  247. {
  248. if (!$this->hasVirtualColumn($name)) {
  249. throw new PropelException('Cannot get value of inexistent virtual column ' . $name);
  250. }
  251. return $this->virtualColumns[$name];
  252. }
  253. /**
  254. * Get the value of a virtual column in this object
  255. *
  256. * @param string $name The virtual column name
  257. * @param mixed $value The value to give to the virtual column
  258. *
  259. * @return BaseObject The current object, for fluid interface
  260. */
  261. public function setVirtualColumn($name, $value)
  262. {
  263. $this->virtualColumns[$name] = $value;
  264. return $this;
  265. }
  266. /**
  267. * Logs a message using Propel::log().
  268. *
  269. * @param string $msg
  270. * @param int $priority One of the Propel::LOG_* logging levels
  271. * @return boolean
  272. */
  273. protected function log($msg, $priority = Propel::LOG_INFO)
  274. {
  275. return Propel::log(get_class($this) . ': ' . $msg, $priority);
  276. }
  277. /**
  278. * Clean up internal collections prior to serializing
  279. * Avoids recursive loops that turn into segmentation faults when serializing
  280. */
  281. public function __sleep()
  282. {
  283. $this->clearAllReferences();
  284. return array_keys(get_object_vars($this));
  285. }
  286. }