PageRenderTime 78ms CodeModel.GetById 10ms RepoModel.GetById 1ms app.codeStats 0ms

/lib/model/om/BasePhysicalObjectI18n.php

https://github.com/mikesname/ehri-ica-atom
PHP | 563 lines | 436 code | 107 blank | 20 comment | 62 complexity | bb24c012532902eb66827d67edb2185c MD5 | raw file
  1. <?php
  2. abstract class BasePhysicalObjectI18n implements ArrayAccess
  3. {
  4. const
  5. DATABASE_NAME = 'propel',
  6. TABLE_NAME = 'physical_object_i18n',
  7. NAME = 'physical_object_i18n.NAME',
  8. DESCRIPTION = 'physical_object_i18n.DESCRIPTION',
  9. LOCATION = 'physical_object_i18n.LOCATION',
  10. ID = 'physical_object_i18n.ID',
  11. CULTURE = 'physical_object_i18n.CULTURE';
  12. public static function addSelectColumns(Criteria $criteria)
  13. {
  14. $criteria->addSelectColumn(QubitPhysicalObjectI18n::NAME);
  15. $criteria->addSelectColumn(QubitPhysicalObjectI18n::DESCRIPTION);
  16. $criteria->addSelectColumn(QubitPhysicalObjectI18n::LOCATION);
  17. $criteria->addSelectColumn(QubitPhysicalObjectI18n::ID);
  18. $criteria->addSelectColumn(QubitPhysicalObjectI18n::CULTURE);
  19. return $criteria;
  20. }
  21. protected static
  22. $physicalObjectI18ns = array();
  23. protected
  24. $keys = array(),
  25. $row = array();
  26. public static function getFromRow(array $row)
  27. {
  28. $keys = array();
  29. $keys['id'] = $row[3];
  30. $keys['culture'] = $row[4];
  31. $key = serialize($keys);
  32. if (!isset(self::$physicalObjectI18ns[$key]))
  33. {
  34. $physicalObjectI18n = new QubitPhysicalObjectI18n;
  35. $physicalObjectI18n->keys = $keys;
  36. $physicalObjectI18n->row = $row;
  37. $physicalObjectI18n->new = false;
  38. self::$physicalObjectI18ns[$key] = $physicalObjectI18n;
  39. }
  40. return self::$physicalObjectI18ns[$key];
  41. }
  42. public static function get(Criteria $criteria, array $options = array())
  43. {
  44. if (!isset($options['connection']))
  45. {
  46. $options['connection'] = Propel::getConnection(QubitPhysicalObjectI18n::DATABASE_NAME);
  47. }
  48. self::addSelectColumns($criteria);
  49. return QubitQuery::createFromCriteria($criteria, 'QubitPhysicalObjectI18n', $options);
  50. }
  51. public static function getAll(array $options = array())
  52. {
  53. return self::get(new Criteria, $options);
  54. }
  55. public static function getOne(Criteria $criteria, array $options = array())
  56. {
  57. $criteria->setLimit(1);
  58. return self::get($criteria, $options)->__get(0, array('defaultValue' => null));
  59. }
  60. public static function getByIdAndCulture($id, $culture, array $options = array())
  61. {
  62. $criteria = new Criteria;
  63. $criteria->add(QubitPhysicalObjectI18n::ID, $id);
  64. $criteria->add(QubitPhysicalObjectI18n::CULTURE, $culture);
  65. if (1 == count($query = self::get($criteria, $options)))
  66. {
  67. return $query[0];
  68. }
  69. }
  70. public static function doDelete(Criteria $criteria, $connection = null)
  71. {
  72. if (!isset($connection))
  73. {
  74. $connection = QubitTransactionFilter::getConnection(QubitPhysicalObjectI18n::DATABASE_NAME);
  75. }
  76. $affectedRows = 0;
  77. $affectedRows += BasePeer::doDelete($criteria, $connection);
  78. return $affectedRows;
  79. }
  80. protected
  81. $tables = array();
  82. public function __construct()
  83. {
  84. $this->tables[] = Propel::getDatabaseMap(QubitPhysicalObjectI18n::DATABASE_NAME)->getTable(QubitPhysicalObjectI18n::TABLE_NAME);
  85. }
  86. protected
  87. $values = array(),
  88. $refFkValues = array();
  89. protected function rowOffsetGet($name, $offset, $options)
  90. {
  91. if (empty($options['clean']) && array_key_exists($name, $this->values))
  92. {
  93. return $this->values[$name];
  94. }
  95. if (array_key_exists($name, $this->keys))
  96. {
  97. return $this->keys[$name];
  98. }
  99. if (!array_key_exists($offset, $this->row))
  100. {
  101. if ($this->new)
  102. {
  103. return;
  104. }
  105. if (!isset($options['connection']))
  106. {
  107. $options['connection'] = Propel::getConnection(QubitPhysicalObjectI18n::DATABASE_NAME);
  108. }
  109. $criteria = new Criteria;
  110. $criteria->add(QubitPhysicalObjectI18n::ID, $this->id);
  111. $criteria->add(QubitPhysicalObjectI18n::CULTURE, $this->culture);
  112. call_user_func(array(get_class($this), 'addSelectColumns'), $criteria);
  113. $statement = BasePeer::doSelect($criteria, $options['connection']);
  114. $this->row = $statement->fetch();
  115. }
  116. return $this->row[$offset];
  117. }
  118. public function __isset($name)
  119. {
  120. $args = func_get_args();
  121. $options = array();
  122. if (1 < count($args))
  123. {
  124. $options = $args[1];
  125. }
  126. $offset = 0;
  127. foreach ($this->tables as $table)
  128. {
  129. foreach ($table->getColumns() as $column)
  130. {
  131. if ($name == $column->getPhpName())
  132. {
  133. return null !== $this->rowOffsetGet($name, $offset, $options);
  134. }
  135. if ("{$name}Id" == $column->getPhpName())
  136. {
  137. return null !== $this->rowOffsetGet("{$name}Id", $offset, $options);
  138. }
  139. $offset++;
  140. }
  141. }
  142. throw new sfException("Unknown record property \"$name\" on \"".get_class($this).'"');
  143. }
  144. public function offsetExists($offset)
  145. {
  146. $args = func_get_args();
  147. return call_user_func_array(array($this, '__isset'), $args);
  148. }
  149. public function __get($name)
  150. {
  151. $args = func_get_args();
  152. $options = array();
  153. if (1 < count($args))
  154. {
  155. $options = $args[1];
  156. }
  157. $offset = 0;
  158. foreach ($this->tables as $table)
  159. {
  160. foreach ($table->getColumns() as $column)
  161. {
  162. if ($name == $column->getPhpName())
  163. {
  164. return $this->rowOffsetGet($name, $offset, $options);
  165. }
  166. if ("{$name}Id" == $column->getPhpName())
  167. {
  168. $relatedTable = $column->getTable()->getDatabaseMap()->getTable($column->getRelatedTableName());
  169. return call_user_func(array($relatedTable->getClassName(), 'getBy'.ucfirst($relatedTable->getColumn($column->getRelatedColumnName())->getPhpName())), $this->rowOffsetGet("{$name}Id", $offset, $options));
  170. }
  171. $offset++;
  172. }
  173. }
  174. throw new sfException("Unknown record property \"$name\" on \"".get_class($this).'"');
  175. }
  176. public function offsetGet($offset)
  177. {
  178. $args = func_get_args();
  179. return call_user_func_array(array($this, '__get'), $args);
  180. }
  181. public function __set($name, $value)
  182. {
  183. $args = func_get_args();
  184. $options = array();
  185. if (2 < count($args))
  186. {
  187. $options = $args[2];
  188. }
  189. $offset = 0;
  190. foreach ($this->tables as $table)
  191. {
  192. foreach ($table->getColumns() as $column)
  193. {
  194. if ($name == $column->getPhpName())
  195. {
  196. $this->values[$name] = $value;
  197. }
  198. if ("{$name}Id" == $column->getPhpName())
  199. {
  200. $relatedTable = $column->getTable()->getDatabaseMap()->getTable($column->getRelatedTableName());
  201. $this->values["{$name}Id"] = $value->__get($relatedTable->getColumn($column->getRelatedColumnName())->getPhpName(), $options);
  202. }
  203. $offset++;
  204. }
  205. }
  206. return $this;
  207. }
  208. public function offsetSet($offset, $value)
  209. {
  210. $args = func_get_args();
  211. return call_user_func_array(array($this, '__set'), $args);
  212. }
  213. public function __unset($name)
  214. {
  215. $offset = 0;
  216. foreach ($this->tables as $table)
  217. {
  218. foreach ($table->getColumns() as $column)
  219. {
  220. if ($name == $column->getPhpName())
  221. {
  222. $this->values[$name] = null;
  223. }
  224. if ("{$name}Id" == $column->getPhpName())
  225. {
  226. $this->values["{$name}Id"] = null;
  227. }
  228. $offset++;
  229. }
  230. }
  231. return $this;
  232. }
  233. public function offsetUnset($offset)
  234. {
  235. $args = func_get_args();
  236. return call_user_func_array(array($this, '__unset'), $args);
  237. }
  238. public function clear()
  239. {
  240. $this->row = $this->values = array();
  241. return $this;
  242. }
  243. protected
  244. $new = true;
  245. protected
  246. $deleted = false;
  247. public function save($connection = null)
  248. {
  249. if ($this->deleted)
  250. {
  251. throw new PropelException('You cannot save an object that has been deleted.');
  252. }
  253. if ($this->new)
  254. {
  255. $this->insert($connection);
  256. }
  257. else
  258. {
  259. $this->update($connection);
  260. }
  261. $offset = 0;
  262. foreach ($this->tables as $table)
  263. {
  264. foreach ($table->getColumns() as $column)
  265. {
  266. if (array_key_exists($column->getPhpName(), $this->values))
  267. {
  268. $this->row[$offset] = $this->values[$column->getPhpName()];
  269. }
  270. $offset++;
  271. }
  272. }
  273. $this->new = false;
  274. $this->values = array();
  275. return $this;
  276. }
  277. protected function param($column)
  278. {
  279. $value = $this->values[$column->getPhpName()];
  280. // Convert to DateTime or SQL zero special case
  281. if (isset($value) && $column->isTemporal() && !$value instanceof DateTime)
  282. {
  283. // Year only: one or more digits. Convert to SQL zero special case
  284. if (preg_match('/^\d+$/', $value))
  285. {
  286. $value .= '-0-0';
  287. }
  288. // Year and month only: one or more digits, plus separator, plus
  289. // one or more digits. Convert to SQL zero special case
  290. else if (preg_match('/^\d+[-\/]\d+$/', $value))
  291. {
  292. $value .= '-0';
  293. }
  294. // Convert to DateTime if not SQL zero special case: year plus
  295. // separator plus zero to twelve (possibly zero padded) plus
  296. // separator plus one or more zeros
  297. if (!preg_match('/^\d+[-\/]0*(?:1[0-2]|\d)[-\/]0+$/', $value))
  298. {
  299. try
  300. {
  301. $value = new DateTime($value);
  302. }
  303. catch (Exception $e)
  304. {
  305. return null;
  306. }
  307. }
  308. }
  309. return $value;
  310. }
  311. protected function insert($connection = null)
  312. {
  313. if (!isset($connection))
  314. {
  315. $connection = QubitTransactionFilter::getConnection(QubitPhysicalObjectI18n::DATABASE_NAME);
  316. }
  317. $offset = 0;
  318. foreach ($this->tables as $table)
  319. {
  320. $criteria = new Criteria;
  321. foreach ($table->getColumns() as $column)
  322. {
  323. if (!array_key_exists($column->getPhpName(), $this->values))
  324. {
  325. if ('createdAt' == $column->getPhpName() || 'updatedAt' == $column->getPhpName())
  326. {
  327. $this->values[$column->getPhpName()] = new DateTime;
  328. }
  329. if ('sourceCulture' == $column->getPhpName())
  330. {
  331. $this->values['sourceCulture'] = sfPropel::getDefaultCulture();
  332. }
  333. }
  334. if (array_key_exists($column->getPhpName(), $this->values))
  335. {
  336. if (null !== $param = $this->param($column))
  337. {
  338. $criteria->add($column->getFullyQualifiedName(), $param);
  339. }
  340. }
  341. $offset++;
  342. }
  343. if (null !== $id = BasePeer::doInsert($criteria, $connection))
  344. {
  345. // Guess that the first primary key of the first table is auto
  346. // incremented
  347. if ($this->tables[0] == $table)
  348. {
  349. $columns = $table->getPrimaryKeyColumns();
  350. $this->values[$columns[0]->getPhpName()] = $this->keys[$columns[0]->getPhpName()] = $id;
  351. }
  352. }
  353. }
  354. return $this;
  355. }
  356. protected function update($connection = null)
  357. {
  358. if (!isset($connection))
  359. {
  360. $connection = QubitTransactionFilter::getConnection(QubitPhysicalObjectI18n::DATABASE_NAME);
  361. }
  362. $offset = 0;
  363. foreach ($this->tables as $table)
  364. {
  365. $criteria = new Criteria;
  366. $selectCriteria = new Criteria;
  367. foreach ($table->getColumns() as $column)
  368. {
  369. if (!array_key_exists($column->getPhpName(), $this->values))
  370. {
  371. if ('updatedAt' == $column->getPhpName())
  372. {
  373. $this->values['updatedAt'] = new DateTime;
  374. }
  375. }
  376. if (array_key_exists($column->getPhpName(), $this->values))
  377. {
  378. if ('serialNumber' == $column->getPhpName())
  379. {
  380. $selectCriteria->add($column->getFullyQualifiedName(), $this->values[$column->getPhpName()]++);
  381. }
  382. $criteria->add($column->getFullyQualifiedName(), $this->param($column));
  383. }
  384. if ($column->isPrimaryKey())
  385. {
  386. $selectCriteria->add($column->getFullyQualifiedName(), $this->keys[$column->getPhpName()]);
  387. }
  388. $offset++;
  389. }
  390. if (0 < $criteria->size())
  391. {
  392. BasePeer::doUpdate($selectCriteria, $criteria, $connection);
  393. }
  394. }
  395. return $this;
  396. }
  397. public function delete($connection = null)
  398. {
  399. if ($this->deleted)
  400. {
  401. throw new PropelException('This object has already been deleted.');
  402. }
  403. $criteria = new Criteria;
  404. $criteria->add(QubitPhysicalObjectI18n::ID, $this->id);
  405. $criteria->add(QubitPhysicalObjectI18n::CULTURE, $this->culture);
  406. self::doDelete($criteria, $connection);
  407. $this->deleted = true;
  408. return $this;
  409. }
  410. /**
  411. * Returns the composite primary key for this object.
  412. * The array elements will be in same order as specified in XML.
  413. * @return array
  414. */
  415. public function getPrimaryKey()
  416. {
  417. $pks = array();
  418. $pks[0] = $this->getid();
  419. $pks[1] = $this->getculture();
  420. return $pks;
  421. }
  422. /**
  423. * Set the [composite] primary key.
  424. *
  425. * @param array $keys The elements of the composite key (order must match the order in XML file).
  426. * @return void
  427. */
  428. public function setPrimaryKey($keys)
  429. {
  430. $this->setid($keys[0]);
  431. $this->setculture($keys[1]);
  432. }
  433. public static function addJoinphysicalObjectCriteria(Criteria $criteria)
  434. {
  435. $criteria->addJoin(QubitPhysicalObjectI18n::ID, QubitPhysicalObject::ID);
  436. return $criteria;
  437. }
  438. public function __call($name, $args)
  439. {
  440. if ('get' == substr($name, 0, 3) || 'set' == substr($name, 0, 3))
  441. {
  442. $args = array_merge(array(strtolower(substr($name, 3, 1)).substr($name, 4)), $args);
  443. return call_user_func_array(array($this, '__'.substr($name, 0, 3)), $args);
  444. }
  445. throw new sfException('Call to undefined method '.get_class($this)."::$name");
  446. }
  447. }