PageRenderTime 51ms CodeModel.GetById 26ms RepoModel.GetById 0ms app.codeStats 0ms

/web/exchange/exchange/plugins/sfDoctrinePlugin/lib/doctrine/Doctrine/Record/Abstract.php

https://github.com/kakaroto/e17
PHP | 359 lines | 162 code | 36 blank | 161 comment | 20 complexity | b2d14070c2168c11df01bf27393a97e8 MD5 | raw file
  1. <?php
  2. /*
  3. * $Id$
  4. *
  5. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  6. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  7. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  8. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  9. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  10. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  11. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  12. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  13. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  14. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  15. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  16. *
  17. * This software consists of voluntary contributions made by many individuals
  18. * and is licensed under the LGPL. For more information, see
  19. * <http://www.phpdoctrine.org>.
  20. */
  21. /**
  22. * Doctrine_Record_Abstract
  23. *
  24. * @package Doctrine
  25. * @subpackage Record
  26. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
  27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
  28. * @link www.phpdoctrine.org
  29. * @since 1.0
  30. * @version $Revision$
  31. */
  32. abstract class Doctrine_Record_Abstract extends Doctrine_Access
  33. {
  34. /**
  35. * @param Doctrine_Table $_table reference to associated Doctrine_Table instance
  36. */
  37. protected $_table;
  38. public function setTableDefinition()
  39. {
  40. }
  41. public function setUp()
  42. {
  43. }
  44. /**
  45. * getTable
  46. * returns the associated table object
  47. *
  48. * @return Doctrine_Table the associated table object
  49. */
  50. public function getTable()
  51. {
  52. return $this->_table;
  53. }
  54. /**
  55. * addListener
  56. *
  57. * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
  58. * @return Doctrine_Record
  59. */
  60. public function addListener($listener, $name = null)
  61. {
  62. $this->_table->addRecordListener($listener, $name = null);
  63. return $this;
  64. }
  65. /**
  66. * getListener
  67. *
  68. * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
  69. */
  70. public function getListener()
  71. {
  72. return $this->_table->getRecordListener();
  73. }
  74. /**
  75. * setListener
  76. *
  77. * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
  78. * @return Doctrine_Record
  79. */
  80. public function setListener($listener)
  81. {
  82. $this->_table->setRecordListener($listener);
  83. return $this;
  84. }
  85. /**
  86. * index
  87. * defines or retrieves an index
  88. * if the second parameter is set this method defines an index
  89. * if not this method retrieves index named $name
  90. *
  91. * @param string $name the name of the index
  92. * @param array $definition the definition array
  93. * @return mixed
  94. */
  95. public function index($name, array $definition = array())
  96. {
  97. if ( ! $definition) {
  98. return $this->_table->getIndex($name);
  99. } else {
  100. return $this->_table->addIndex($name, $definition);
  101. }
  102. }
  103. public function setAttribute($attr, $value)
  104. {
  105. $this->_table->setAttribute($attr, $value);
  106. }
  107. public function setTableName($tableName)
  108. {
  109. $this->_table->setTableName($tableName);
  110. }
  111. public function setInheritanceMap($map)
  112. {
  113. $this->_table->setOption('inheritanceMap', $map);
  114. }
  115. public function setSubclasses($map)
  116. {
  117. if (isset($map[get_class($this)])) {
  118. $this->_table->setOption('inheritanceMap', $map[get_class($this)]);
  119. return;
  120. }
  121. $this->_table->setOption('subclasses', array_keys($map));
  122. }
  123. /**
  124. * attribute
  125. * sets or retrieves an option
  126. *
  127. * @see Doctrine::ATTR_* constants availible attributes
  128. * @param mixed $attr
  129. * @param mixed $value
  130. * @return mixed
  131. */
  132. public function attribute($attr, $value)
  133. {
  134. if ($value == null) {
  135. if (is_array($attr)) {
  136. foreach ($attr as $k => $v) {
  137. $this->_table->setAttribute($k, $v);
  138. }
  139. } else {
  140. return $this->_table->getAttribute($attr);
  141. }
  142. } else {
  143. $this->_table->setAttribute($attr, $value);
  144. }
  145. }
  146. /**
  147. * option
  148. * sets or retrieves an option
  149. *
  150. * @see Doctrine_Table::$options availible options
  151. * @param mixed $name the name of the option
  152. * @param mixed $value options value
  153. * @return mixed
  154. */
  155. public function option($name, $value = null)
  156. {
  157. if ($value === null) {
  158. if (is_array($name)) {
  159. foreach ($name as $k => $v) {
  160. $this->_table->setOption($k, $v);
  161. }
  162. } else {
  163. return $this->_table->getOption($name);
  164. }
  165. } else {
  166. $this->_table->setOption($name, $value);
  167. }
  168. }
  169. /**
  170. * DEPRECATED ALSO? - REMOVE SOON
  171. *
  172. * ownsOne
  173. * binds One-to-One composite relation
  174. *
  175. * @param string $componentName the name of the related component
  176. * @param string $options relation options
  177. * @see Doctrine_Relation::_$definition
  178. * @return Doctrine_Record this object
  179. */
  180. public function ownsOne()
  181. {
  182. $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_COMPOSITE);
  183. return $this;
  184. }
  185. /**
  186. * DEPRECATED - REMOVE SOON
  187. *
  188. * ownsMany
  189. * binds One-to-Many / Many-to-Many composite relation
  190. *
  191. * @param string $componentName the name of the related component
  192. * @param string $options relation options
  193. * @see Doctrine_Relation::_$definition
  194. * @return Doctrine_Record this object
  195. */
  196. public function ownsMany()
  197. {
  198. $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_COMPOSITE);
  199. return $this;
  200. }
  201. /**
  202. * hasOne
  203. * binds One-to-One aggregate relation
  204. *
  205. * @param string $componentName the name of the related component
  206. * @param string $options relation options
  207. * @see Doctrine_Relation::_$definition
  208. * @return Doctrine_Record this object
  209. */
  210. public function hasOne()
  211. {
  212. $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_AGGREGATE);
  213. return $this;
  214. }
  215. /**
  216. * hasMany
  217. * binds One-to-Many / Many-to-Many aggregate relation
  218. *
  219. * @param string $componentName the name of the related component
  220. * @param string $options relation options
  221. * @see Doctrine_Relation::_$definition
  222. * @return Doctrine_Record this object
  223. */
  224. public function hasMany()
  225. {
  226. $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_AGGREGATE);
  227. return $this;
  228. }
  229. /**
  230. * hasColumn
  231. * sets a column definition
  232. *
  233. * @param string $name
  234. * @param string $type
  235. * @param integer $length
  236. * @param mixed $options
  237. * @return void
  238. */
  239. public function hasColumn($name, $type, $length = 2147483647, $options = "")
  240. {
  241. $this->_table->setColumn($name, $type, $length, $options);
  242. }
  243. public function hasColumns(array $definitions)
  244. {
  245. foreach ($definitions as $name => $options) {
  246. $this->hasColumn($name, $options['type'], $options['length'], $options);
  247. }
  248. }
  249. /**
  250. * loadTemplate
  251. *
  252. * @param string $template
  253. */
  254. public function loadTemplate($template, array $options = array())
  255. {
  256. $this->actAs($template, $options);
  257. }
  258. /**
  259. * bindQueryParts
  260. * binds query parts to given component
  261. *
  262. * @param array $queryParts an array of pre-bound query parts
  263. * @return Doctrine_Record this object
  264. */
  265. public function bindQueryParts(array $queryParts)
  266. {
  267. $this->_table->bindQueryParts($queryParts);
  268. return $this;
  269. }
  270. public function loadGenerator(Doctrine_Record_Generator $generator)
  271. {
  272. $generator->initialize($this->_table);
  273. $this->_table->addGenerator($generator, get_class($generator));
  274. }
  275. /**
  276. * actAs
  277. * loads the given plugin
  278. *
  279. * @param mixed $tpl
  280. * @param array $options
  281. */
  282. public function actAs($tpl, array $options = array())
  283. {
  284. if ( ! is_object($tpl)) {
  285. $className = 'Doctrine_Template_' . $tpl;
  286. if (class_exists($className, true)) {
  287. $tpl = new $className($options);
  288. } else if (class_exists($tpl, true)) {
  289. $tpl = new $tpl($options);
  290. } else {
  291. throw new Doctrine_Record_Exception('Could not load behavior named: "' . $tpl . '"');
  292. }
  293. }
  294. if ( ! ($tpl instanceof Doctrine_Template)) {
  295. throw new Doctrine_Record_Exception('Loaded behavior class is not an istance of Doctrine_Template.');
  296. }
  297. $className = get_class($tpl);
  298. $this->_table->addTemplate($className, $tpl);
  299. $tpl->setTable($this->_table);
  300. $tpl->setUp();
  301. $tpl->setTableDefinition();
  302. return $this;
  303. }
  304. /**
  305. * check
  306. * adds a check constraint
  307. *
  308. * @param mixed $constraint either a SQL constraint portion or an array of CHECK constraints
  309. * @param string $name optional constraint name
  310. * @return Doctrine_Record this object
  311. */
  312. public function check($constraint, $name = null)
  313. {
  314. if (is_array($constraint)) {
  315. foreach ($constraint as $name => $def) {
  316. $this->_table->addCheckConstraint($def, $name);
  317. }
  318. } else {
  319. $this->_table->addCheckConstraint($constraint, $name);
  320. }
  321. return $this;
  322. }
  323. }