PageRenderTime 26ms CodeModel.GetById 17ms RepoModel.GetById 1ms app.codeStats 0ms

/runtime/lib/om/BaseObject.php

https://github.com/homer6/Propel
PHP | 396 lines | 154 code | 35 blank | 207 comment | 18 complexity | 7ba3b71609991aa833cfc9d2e6f4c56a MD5 | raw file
  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. * @method BaseObject fromXML(string $data) Populate the object from an XML string
  14. * @method BaseObject fromYAML(string $data) Populate the object from a YAML string
  15. * @method BaseObject fromJSON(string $data) Populate the object from a JSON string
  16. * @method BaseObject fromCSV(string $data) Populate the object from a CSV string
  17. * @method string toXML() Export the object to an XML string
  18. * @method string toYAML() Export the object to a YAML string
  19. * @method string toJSON() Export the object to a JSON string
  20. * @method string toCSV() Export the object to a CSV string
  21. *
  22. * @author Hans Lellelid <hans@xmpl.org> (Propel)
  23. * @author Frank Y. Kim <frank.kim@clearink.com> (Torque)
  24. * @author John D. McNally <jmcnally@collab.net> (Torque)
  25. * @version $Revision$
  26. * @package propel.runtime.om
  27. */
  28. abstract class BaseObject
  29. {
  30. /**
  31. * attribute to determine if this object has previously been saved.
  32. * @var boolean
  33. */
  34. protected $_new = true;
  35. /**
  36. * attribute to determine whether this object has been deleted.
  37. * @var boolean
  38. */
  39. protected $_deleted = false;
  40. /**
  41. * The columns that have been modified in current object.
  42. * Tracking modified columns allows us to only update modified columns.
  43. * @var array
  44. */
  45. protected $modifiedColumns = array();
  46. /**
  47. * The (virtual) columns that are added at runtime
  48. * The formatters can add supplementary columns based on a resultset
  49. * @var array
  50. */
  51. protected $virtualColumns = array();
  52. /**
  53. * Empty constructor (this allows people with their own BaseObject implementation to use its constructor)
  54. */
  55. public function __construct() {
  56. }
  57. /**
  58. * Returns whether the object has been modified.
  59. *
  60. * @return boolean True if the object has been modified.
  61. */
  62. public function isModified()
  63. {
  64. return !empty($this->modifiedColumns);
  65. }
  66. /**
  67. * Has specified column been modified?
  68. *
  69. * @param string $col column fully qualified name (BasePeer::TYPE_COLNAME), e.g. Book::AUTHOR_ID
  70. * @return boolean True if $col has been modified.
  71. */
  72. public function isColumnModified($col)
  73. {
  74. return in_array($col, $this->modifiedColumns);
  75. }
  76. /**
  77. * Get the columns that have been modified in this object.
  78. * @return array A unique list of the modified column names for this object.
  79. */
  80. public function getModifiedColumns()
  81. {
  82. return array_unique($this->modifiedColumns);
  83. }
  84. /**
  85. * Returns whether the object has ever been saved. This will
  86. * be false, if the object was retrieved from storage or was created
  87. * and then saved.
  88. *
  89. * @return true, if the object has never been persisted.
  90. */
  91. public function isNew()
  92. {
  93. return $this->_new;
  94. }
  95. /**
  96. * Setter for the isNew attribute. This method will be called
  97. * by Propel-generated children and Peers.
  98. *
  99. * @param boolean $b the state of the object.
  100. */
  101. public function setNew($b)
  102. {
  103. $this->_new = (boolean) $b;
  104. }
  105. /**
  106. * Whether this object has been deleted.
  107. * @return boolean The deleted state of this object.
  108. */
  109. public function isDeleted()
  110. {
  111. return $this->_deleted;
  112. }
  113. /**
  114. * Specify whether this object has been deleted.
  115. * @param boolean $b The deleted state of this object.
  116. * @return void
  117. */
  118. public function setDeleted($b)
  119. {
  120. $this->_deleted = (boolean) $b;
  121. }
  122. /**
  123. * Code to be run before persisting the object
  124. * @param PropelPDO $con
  125. * @return bloolean
  126. */
  127. public function preSave(PropelPDO $con = null)
  128. {
  129. return true;
  130. }
  131. /**
  132. * Code to be run after persisting the object
  133. * @param PropelPDO $con
  134. */
  135. public function postSave(PropelPDO $con = null) { }
  136. /**
  137. * Code to be run before inserting to database
  138. * @param PropelPDO $con
  139. * @return boolean
  140. */
  141. public function preInsert(PropelPDO $con = null)
  142. {
  143. return true;
  144. }
  145. /**
  146. * Code to be run after inserting to database
  147. * @param PropelPDO $con
  148. */
  149. public function postInsert(PropelPDO $con = null) { }
  150. /**
  151. * Code to be run before updating the object in database
  152. * @param PropelPDO $con
  153. * @return boolean
  154. */
  155. public function preUpdate(PropelPDO $con = null)
  156. {
  157. return true;
  158. }
  159. /**
  160. * Code to be run after updating the object in database
  161. * @param PropelPDO $con
  162. */
  163. public function postUpdate(PropelPDO $con = null) { }
  164. /**
  165. * Code to be run before deleting the object in database
  166. * @param PropelPDO $con
  167. * @return boolean
  168. */
  169. public function preDelete(PropelPDO $con = null)
  170. {
  171. return true;
  172. }
  173. /**
  174. * Code to be run after deleting the object in database
  175. * @param PropelPDO $con
  176. */
  177. public function postDelete(PropelPDO $con = null) { }
  178. /**
  179. * Sets the modified state for the object to be false.
  180. * @param string $col If supplied, only the specified column is reset.
  181. * @return void
  182. */
  183. public function resetModified($col = null)
  184. {
  185. if ($col !== null) {
  186. while (($offset = array_search($col, $this->modifiedColumns)) !== false) {
  187. array_splice($this->modifiedColumns, $offset, 1);
  188. }
  189. } else {
  190. $this->modifiedColumns = array();
  191. }
  192. }
  193. /**
  194. * Compares this with another <code>BaseObject</code> instance. If
  195. * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
  196. * <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>.
  197. *
  198. * @param obj The object to compare to.
  199. * @return Whether equal to the object specified.
  200. */
  201. public function equals($obj)
  202. {
  203. $thisclazz = get_class($this);
  204. if (is_object($obj) && $obj instanceof $thisclazz) {
  205. if ($this === $obj) {
  206. return true;
  207. } elseif ($this->getPrimaryKey() === null || $obj->getPrimaryKey() === null) {
  208. return false;
  209. } else {
  210. return ($this->getPrimaryKey() === $obj->getPrimaryKey());
  211. }
  212. } else {
  213. return false;
  214. }
  215. }
  216. /**
  217. * If the primary key is not <code>null</code>, return the hashcode of the
  218. * primary key. Otherwise calls <code>Object.hashCode()</code>.
  219. *
  220. * @return int Hashcode
  221. */
  222. public function hashCode()
  223. {
  224. $ok = $this->getPrimaryKey();
  225. if ($ok === null) {
  226. return crc32(serialize($this));
  227. }
  228. return crc32(serialize($ok)); // serialize because it could be an array ("ComboKey")
  229. }
  230. /**
  231. * Get the associative array of the virtual columns in this object
  232. *
  233. * @param string $name The virtual column name
  234. *
  235. * @return array
  236. */
  237. public function getVirtualColumns()
  238. {
  239. return $this->virtualColumns;
  240. }
  241. /**
  242. * Checks the existence of a virtual column in this object
  243. *
  244. * @return boolean
  245. */
  246. public function hasVirtualColumn($name)
  247. {
  248. return array_key_exists($name, $this->virtualColumns);
  249. }
  250. /**
  251. * Get the value of a virtual column in this object
  252. *
  253. * @return mixed
  254. */
  255. public function getVirtualColumn($name)
  256. {
  257. if (!$this->hasVirtualColumn($name)) {
  258. throw new PropelException('Cannot get value of inexistent virtual column ' . $name);
  259. }
  260. return $this->virtualColumns[$name];
  261. }
  262. /**
  263. * Get the value of a virtual column in this object
  264. *
  265. * @param string $name The virtual column name
  266. * @param mixed $value The value to give to the virtual column
  267. *
  268. * @return BaseObject The current object, for fluid interface
  269. */
  270. public function setVirtualColumn($name, $value)
  271. {
  272. $this->virtualColumns[$name] = $value;
  273. return $this;
  274. }
  275. /**
  276. * Logs a message using Propel::log().
  277. *
  278. * @param string $msg
  279. * @param int $priority One of the Propel::LOG_* logging levels
  280. * @return boolean
  281. */
  282. protected function log($msg, $priority = Propel::LOG_INFO)
  283. {
  284. return Propel::log(get_class($this) . ': ' . $msg, $priority);
  285. }
  286. /**
  287. * Populate the current object from a string, using a given parser format
  288. * <code>
  289. * $book = new Book();
  290. * $book->importFrom('JSON', '{"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}');
  291. * </code>
  292. *
  293. * @param mixed $parser A PropelParser instance,
  294. * or a format name ('XML', 'YAML', 'JSON', 'CSV')
  295. * @param string $data The source data to import from
  296. *
  297. * @return BaseObject The current object, for fluid interface
  298. */
  299. public function importFrom($parser, $data)
  300. {
  301. if (!$parser instanceof PropelParser) {
  302. $parser = PropelParser::getParser($parser);
  303. }
  304. return $this->fromArray($parser->toArray($data), BasePeer::TYPE_PHPNAME);
  305. }
  306. /**
  307. * Export the current object properties to a string, using a given parser format
  308. * <code>
  309. * $book = BookQuery::create()->findPk(9012);
  310. * echo $book->exportTo('JSON');
  311. * => {"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}');
  312. * </code>
  313. *
  314. * @param mixed $parser A PropelParser instance,
  315. * or a format name ('XML', 'YAML', 'JSON', 'CSV')
  316. * @return string The exported data
  317. */
  318. public function exportTo($parser)
  319. {
  320. if (!$parser instanceof PropelParser) {
  321. $parser = PropelParser::getParser($parser);
  322. }
  323. return $parser->fromArray($this->toArray(BasePeer::TYPE_PHPNAME, true, array(), true));
  324. }
  325. /**
  326. * Clean up internal collections prior to serializing
  327. * Avoids recursive loops that turn into segmentation faults when serializing
  328. */
  329. public function __sleep()
  330. {
  331. $this->clearAllReferences();
  332. return array_keys(get_object_vars($this));
  333. }
  334. /**
  335. * Catches calls to undefined methods.
  336. * Provides magic getter for virtual columns.
  337. * Provides magic import/export method support (fromXML()/toXML(), fromYAML()/toYAML(), etc.).
  338. * Allows to define default __call() behavior if you use a custom BaseObject
  339. */
  340. public function __call($name, $params)
  341. {
  342. if (preg_match('/get(\w+)/', $name, $matches)) {
  343. $virtualColumn = $matches[1];
  344. if ($this->hasVirtualColumn($virtualColumn)) {
  345. return $this->getVirtualColumn($virtualColumn);
  346. }
  347. // no lcfirst in php<5.3...
  348. $virtualColumn[0] = strtolower($virtualColumn[0]);
  349. if ($this->hasVirtualColumn($virtualColumn)) {
  350. return $this->getVirtualColumn($virtualColumn);
  351. }
  352. }
  353. if (preg_match('/^from(\w+)$/', $name, $matches)) {
  354. return $this->importFrom($matches[1], reset($params));
  355. }
  356. if (preg_match('/^to(\w+)$/', $name, $matches)) {
  357. return $this->exportTo($matches[1]);
  358. }
  359. throw new PropelException('Call to undefined method: ' . $name);
  360. }
  361. }