PageRenderTime 50ms CodeModel.GetById 17ms RepoModel.GetById 0ms app.codeStats 0ms

/runtime/lib/om/BaseObject.php

https://github.com/Tactics/Propel
PHP | 431 lines | 166 code | 42 blank | 223 comment | 18 complexity | 6a63e781e48aa131a0cf51d9a19af94e 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(boolean $includeLazyLoadColumns) Export the object to an XML string
  18. * @method string toYAML(boolean $includeLazyLoadColumns) Export the object to a YAML string
  19. * @method string toJSON(boolean $includeLazyLoadColumns) Export the object to a JSON string
  20. * @method string toCSV(boolean $includeLazyLoadColumns) 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. /**
  59. * Returns whether the object has been modified.
  60. *
  61. * @return boolean True if the object has been modified.
  62. */
  63. public function isModified()
  64. {
  65. return !empty($this->modifiedColumns);
  66. }
  67. /**
  68. * Has specified column been modified?
  69. *
  70. * @param string $col column fully qualified name (BasePeer::TYPE_COLNAME), e.g. Book::AUTHOR_ID
  71. * @return boolean True if $col has been modified.
  72. */
  73. public function isColumnModified($col)
  74. {
  75. return in_array($col, $this->modifiedColumns);
  76. }
  77. /**
  78. * Get the columns that have been modified in this object.
  79. * @return array A unique list of the modified column names for this object.
  80. */
  81. public function getModifiedColumns()
  82. {
  83. return array_unique($this->modifiedColumns);
  84. }
  85. /**
  86. * Returns whether the object has ever been saved. This will
  87. * be false, if the object was retrieved from storage or was created
  88. * and then saved.
  89. *
  90. * @return true, if the object has never been persisted.
  91. */
  92. public function isNew()
  93. {
  94. return $this->_new;
  95. }
  96. /**
  97. * Setter for the isNew attribute. This method will be called
  98. * by Propel-generated children and Peers.
  99. *
  100. * @param boolean $b the state of the object.
  101. */
  102. public function setNew($b)
  103. {
  104. $this->_new = (boolean) $b;
  105. }
  106. /**
  107. * Whether this object has been deleted.
  108. * @return boolean The deleted state of this object.
  109. */
  110. public function isDeleted()
  111. {
  112. return $this->_deleted;
  113. }
  114. /**
  115. * Specify whether this object has been deleted.
  116. * @param boolean $b The deleted state of this object.
  117. * @return void
  118. */
  119. public function setDeleted($b)
  120. {
  121. $this->_deleted = (boolean) $b;
  122. }
  123. /**
  124. * Code to be run before persisting the object
  125. * @param PropelPDO $con
  126. * @return bloolean
  127. */
  128. public function preSave(PropelPDO $con = null)
  129. {
  130. return true;
  131. }
  132. /**
  133. * Code to be run after persisting the object
  134. * @param PropelPDO $con
  135. */
  136. public function postSave(PropelPDO $con = null)
  137. {
  138. }
  139. /**
  140. * Code to be run before inserting to database
  141. * @param PropelPDO $con
  142. * @return boolean
  143. */
  144. public function preInsert(PropelPDO $con = null)
  145. {
  146. return true;
  147. }
  148. /**
  149. * Code to be run after inserting to database
  150. * @param PropelPDO $con
  151. */
  152. public function postInsert(PropelPDO $con = null)
  153. {
  154. }
  155. /**
  156. * Code to be run before updating the object in database
  157. * @param PropelPDO $con
  158. * @return boolean
  159. */
  160. public function preUpdate(PropelPDO $con = null)
  161. {
  162. return true;
  163. }
  164. /**
  165. * Code to be run after updating the object in database
  166. * @param PropelPDO $con
  167. */
  168. public function postUpdate(PropelPDO $con = null)
  169. {
  170. }
  171. /**
  172. * Code to be run before deleting the object in database
  173. * @param PropelPDO $con
  174. * @return boolean
  175. */
  176. public function preDelete(PropelPDO $con = null)
  177. {
  178. return true;
  179. }
  180. /**
  181. * Code to be run after deleting the object in database
  182. * @param PropelPDO $con
  183. */
  184. public function postDelete(PropelPDO $con = null)
  185. {
  186. }
  187. /**
  188. * Code to be run after hydrating the object from database
  189. *
  190. * @param array $row The row returned by PDOStatement->fetch(PDO::FETCH_NUM)
  191. * @param int $startcol 0-based offset column which indicates which restultset column to start with.
  192. * @param boolean $rehydrate Whether this object is being re-hydrated from the database.
  193. */
  194. public function postHydrate($row, $startcol = 0, $rehydrate = false)
  195. {
  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. while (($offset = array_search($col, $this->modifiedColumns)) !== false) {
  206. array_splice($this->modifiedColumns, $offset, 1);
  207. }
  208. } else {
  209. $this->modifiedColumns = array();
  210. }
  211. }
  212. /**
  213. * Compares this with another <code>BaseObject</code> instance. If
  214. * <code>obj</code> is an instance of <code>BaseObject</code>, delegates to
  215. * <code>equals(BaseObject)</code>. Otherwise, returns <code>false</code>.
  216. *
  217. * @param obj The object to compare to.
  218. * @return Whether equal to the object specified.
  219. */
  220. public function equals($obj)
  221. {
  222. $thisclazz = get_class($this);
  223. if (is_object($obj) && $obj instanceof $thisclazz) {
  224. if ($this === $obj) {
  225. return true;
  226. } elseif ($this->getPrimaryKey() === null || $obj->getPrimaryKey() === null) {
  227. return false;
  228. } else {
  229. return ($this->getPrimaryKey() === $obj->getPrimaryKey());
  230. }
  231. } else {
  232. return false;
  233. }
  234. }
  235. /**
  236. * If the primary key is not null, return the hashcode of the
  237. * primary key. Otherwise, return the hash code of the object.
  238. *
  239. * @return int Hashcode
  240. */
  241. public function hashCode()
  242. {
  243. if (null !== $this->getPrimaryKey()) {
  244. return crc32(serialize($this->getPrimaryKey()));
  245. }
  246. return crc32(serialize($this));
  247. }
  248. /**
  249. * Get the associative array of the virtual columns in this object
  250. *
  251. * @param string $name The virtual column name
  252. *
  253. * @return array
  254. */
  255. public function getVirtualColumns()
  256. {
  257. return $this->virtualColumns;
  258. }
  259. /**
  260. * Checks the existence of a virtual column in this object
  261. *
  262. * @return boolean
  263. */
  264. public function hasVirtualColumn($name)
  265. {
  266. return array_key_exists($name, $this->virtualColumns);
  267. }
  268. /**
  269. * Get the value of a virtual column in this object
  270. *
  271. * @return mixed
  272. *
  273. * @throws PropelException
  274. */
  275. public function getVirtualColumn($name)
  276. {
  277. if (!$this->hasVirtualColumn($name)) {
  278. throw new PropelException('Cannot get value of inexistent virtual column ' . $name);
  279. }
  280. return $this->virtualColumns[$name];
  281. }
  282. /**
  283. * Set the value of a virtual column in this object
  284. *
  285. * @param string $name The virtual column name
  286. * @param mixed $value The value to give to the virtual column
  287. *
  288. * @return BaseObject The current object, for fluid interface
  289. */
  290. public function setVirtualColumn($name, $value)
  291. {
  292. $this->virtualColumns[$name] = $value;
  293. return $this;
  294. }
  295. /**
  296. * Logs a message using Propel::log().
  297. *
  298. * @param string $msg
  299. * @param int $priority One of the Propel::LOG_* logging levels
  300. * @return boolean
  301. */
  302. protected function log($msg, $priority = Propel::LOG_INFO)
  303. {
  304. return Propel::log(get_class($this) . ': ' . $msg, $priority);
  305. }
  306. /**
  307. * Populate the current object from a string, using a given parser format
  308. * <code>
  309. * $book = new Book();
  310. * $book->importFrom('JSON', '{"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}');
  311. * </code>
  312. *
  313. * @param mixed $parser A PropelParser instance,
  314. * or a format name ('XML', 'YAML', 'JSON', 'CSV')
  315. * @param string $data The source data to import from
  316. *
  317. * @return BaseObject The current object, for fluid interface
  318. */
  319. public function importFrom($parser, $data)
  320. {
  321. if (!$parser instanceof PropelParser) {
  322. $parser = PropelParser::getParser($parser);
  323. }
  324. return $this->fromArray($parser->toArray($data), BasePeer::TYPE_PHPNAME);
  325. }
  326. /**
  327. * Export the current object properties to a string, using a given parser format
  328. * <code>
  329. * $book = BookQuery::create()->findPk(9012);
  330. * echo $book->exportTo('JSON');
  331. * => {"Id":9012,"Title":"Don Juan","ISBN":"0140422161","Price":12.99,"PublisherId":1234,"AuthorId":5678}');
  332. * </code>
  333. *
  334. * @param mixed $parser A PropelParser instance, or a format name ('XML', 'YAML', 'JSON', 'CSV')
  335. * @param boolean $includeLazyLoadColumns (optional) Whether to include lazy load(ed) columns. Defaults to TRUE.
  336. * @return string The exported data
  337. */
  338. public function exportTo($parser, $includeLazyLoadColumns = true)
  339. {
  340. if (!$parser instanceof PropelParser) {
  341. $parser = PropelParser::getParser($parser);
  342. }
  343. return $parser->fromArray($this->toArray(BasePeer::TYPE_PHPNAME, $includeLazyLoadColumns, array(), true));
  344. }
  345. /**
  346. * Clean up internal collections prior to serializing
  347. * Avoids recursive loops that turn into segmentation faults when serializing
  348. */
  349. public function __sleep()
  350. {
  351. $this->clearAllReferences();
  352. return array_keys(get_object_vars($this));
  353. }
  354. /**
  355. * Catches calls to undefined methods.
  356. *
  357. * Provides magic import/export method support (fromXML()/toXML(), fromYAML()/toYAML(), etc.).
  358. * Allows to define default __call() behavior if you use a custom BaseObject
  359. *
  360. * @param string $name
  361. * @param mixed $params
  362. *
  363. * @return array|string
  364. *
  365. * @throws PropelException
  366. */
  367. public function __call($name, $params)
  368. {
  369. if (preg_match('/get(\w+)/', $name, $matches)) {
  370. $virtualColumn = $matches[1];
  371. if ($this->hasVirtualColumn($virtualColumn)) {
  372. return $this->getVirtualColumn($virtualColumn);
  373. }
  374. // no lcfirst in php<5.3...
  375. $virtualColumn[0] = strtolower($virtualColumn[0]);
  376. if ($this->hasVirtualColumn($virtualColumn)) {
  377. return $this->getVirtualColumn($virtualColumn);
  378. }
  379. }
  380. if (preg_match('/^from(\w+)$/', $name, $matches)) {
  381. return $this->importFrom($matches[1], reset($params));
  382. }
  383. if (preg_match('/^to(\w+)$/', $name, $matches)) {
  384. $includeLazyLoadColumns = isset($params[0]) ? $params[0] : true;
  385. return $this->exportTo($matches[1], $includeLazyLoadColumns);
  386. }
  387. throw new PropelException('Call to undefined method: ' . $name);
  388. }
  389. }