PageRenderTime 24ms CodeModel.GetById 13ms RepoModel.GetById 0ms app.codeStats 0ms

/system/application/plugins/doctrine/lib/Doctrine/Record/Abstract.php

https://github.com/cawago/ci_campusync_auth
PHP | 354 lines | 167 code | 40 blank | 147 comment | 25 complexity | 5fc0e90c5cb779333dd67814ebff8c92 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);
  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. /**
  104. * Specify an array of fields that are unique and will be validated as such
  105. *
  106. * @return void
  107. */
  108. public function unique()
  109. {
  110. $args = func_get_args();
  111. if (count($args) == 1) {
  112. $fields = (array) $args[0];
  113. } else if (count($args) > 1) {
  114. $fields = $args;
  115. } else {
  116. throw new Doctrine_Record_Exception('You must specify the fields to make a unique constraint on.');
  117. }
  118. return $this->_table->unique($fields);
  119. }
  120. public function setAttribute($attr, $value)
  121. {
  122. $this->_table->setAttribute($attr, $value);
  123. }
  124. public function setTableName($tableName)
  125. {
  126. $this->_table->setTableName($tableName);
  127. }
  128. public function setInheritanceMap($map)
  129. {
  130. $this->_table->setOption('inheritanceMap', $map);
  131. }
  132. public function setSubclasses($map)
  133. {
  134. if (isset($map[get_class($this)])) {
  135. // fix for #1621
  136. $mapFieldNames = $map[get_class($this)];
  137. $mapColumnNames = array();
  138. foreach ($mapFieldNames as $fieldName => $val) {
  139. $mapColumnNames[$this->getTable()->getColumnName($fieldName)] = $val;
  140. }
  141. $this->_table->setOption('inheritanceMap', $mapColumnNames);
  142. return;
  143. }
  144. $this->_table->setOption('subclasses', array_keys($map));
  145. }
  146. /**
  147. * attribute
  148. * sets or retrieves an option
  149. *
  150. * @see Doctrine::ATTR_* constants availible attributes
  151. * @param mixed $attr
  152. * @param mixed $value
  153. * @return mixed
  154. */
  155. public function attribute($attr, $value)
  156. {
  157. if ($value == null) {
  158. if (is_array($attr)) {
  159. foreach ($attr as $k => $v) {
  160. $this->_table->setAttribute($k, $v);
  161. }
  162. } else {
  163. return $this->_table->getAttribute($attr);
  164. }
  165. } else {
  166. $this->_table->setAttribute($attr, $value);
  167. }
  168. }
  169. /**
  170. * option
  171. * sets or retrieves an option
  172. *
  173. * @see Doctrine_Table::$options availible options
  174. * @param mixed $name the name of the option
  175. * @param mixed $value options value
  176. * @return mixed
  177. */
  178. public function option($name, $value = null)
  179. {
  180. if ($value === null) {
  181. if (is_array($name)) {
  182. foreach ($name as $k => $v) {
  183. $this->_table->setOption($k, $v);
  184. }
  185. } else {
  186. return $this->_table->getOption($name);
  187. }
  188. } else {
  189. $this->_table->setOption($name, $value);
  190. }
  191. }
  192. /**
  193. * hasOne
  194. * binds One-to-One aggregate relation
  195. *
  196. * @param string $componentName the name of the related component
  197. * @param string $options relation options
  198. * @see Doctrine_Relation::_$definition
  199. * @return Doctrine_Record this object
  200. */
  201. public function hasOne()
  202. {
  203. $this->_table->bind(func_get_args(), Doctrine_Relation::ONE);
  204. return $this;
  205. }
  206. /**
  207. * hasMany
  208. * binds One-to-Many / Many-to-Many aggregate relation
  209. *
  210. * @param string $componentName the name of the related component
  211. * @param string $options relation options
  212. * @see Doctrine_Relation::_$definition
  213. * @return Doctrine_Record this object
  214. */
  215. public function hasMany()
  216. {
  217. $this->_table->bind(func_get_args(), Doctrine_Relation::MANY);
  218. return $this;
  219. }
  220. /**
  221. * hasColumn
  222. * sets a column definition
  223. *
  224. * @param string $name
  225. * @param string $type
  226. * @param integer $length
  227. * @param mixed $options
  228. * @return void
  229. */
  230. public function hasColumn($name, $type = null, $length = null, $options = array())
  231. {
  232. $this->_table->setColumn($name, $type, $length, $options);
  233. }
  234. public function hasColumns(array $definitions)
  235. {
  236. foreach ($definitions as $name => $options) {
  237. $length = isset($options['length']) ? $options['length']:null;
  238. $this->hasColumn($name, $options['type'], $length, $options);
  239. }
  240. }
  241. /**
  242. * bindQueryParts
  243. * binds query parts to given component
  244. *
  245. * @param array $queryParts an array of pre-bound query parts
  246. * @return Doctrine_Record this object
  247. */
  248. public function bindQueryParts(array $queryParts)
  249. {
  250. $this->_table->bindQueryParts($queryParts);
  251. return $this;
  252. }
  253. public function loadGenerator(Doctrine_Record_Generator $generator)
  254. {
  255. $generator->initialize($this->_table);
  256. $this->_table->addGenerator($generator, get_class($generator));
  257. }
  258. /**
  259. * Loads the given plugin.
  260. *
  261. * This method loads a behavior in the record. It will add the behavior
  262. * also to the record table if it.
  263. * It is tipically called in @see setUp().
  264. *
  265. * @param mixed $tpl if an object, must be a subclass of Doctrine_Template.
  266. * If a string, Doctrine will try to instantiate an object of the classes Doctrine_Template_$tpl and subsequently $tpl, using also autoloading capabilities if defined.
  267. * @param array $options argument to pass to the template constructor if $tpl is a class name
  268. * @throws Doctrine_Record_Exception if $tpl is neither an instance of Doctrine_Template subclass or a valid class name, that could be instantiated.
  269. * @return Doctrine_Record this object; provides a fluent interface.
  270. */
  271. public function actAs($tpl, array $options = array())
  272. {
  273. if ( ! is_object($tpl)) {
  274. $className = 'Doctrine_Template_' . $tpl;
  275. if (class_exists($className, true)) {
  276. $tpl = new $className($options);
  277. } else if (class_exists($tpl, true)) {
  278. $tpl = new $tpl($options);
  279. } else {
  280. throw new Doctrine_Record_Exception('Could not load behavior named: "' . $tpl . '"');
  281. }
  282. }
  283. if ( ! ($tpl instanceof Doctrine_Template)) {
  284. throw new Doctrine_Record_Exception('Loaded behavior class is not an instance of Doctrine_Template.');
  285. }
  286. $className = get_class($tpl);
  287. $this->_table->addTemplate($className, $tpl);
  288. $tpl->setInvoker($this);
  289. $tpl->setTable($this->_table);
  290. $tpl->setUp();
  291. $tpl->setTableDefinition();
  292. return $this;
  293. }
  294. /**
  295. * Adds a check constraint.
  296. *
  297. * This method will add a CHECK constraint to the record table.
  298. *
  299. * @param mixed $constraint either a SQL constraint portion or an array of CHECK constraints. If array, all values will be added as constraint
  300. * @param string $name optional constraint name. Not used if $constraint is an array.
  301. * @return Doctrine_Record this object
  302. */
  303. public function check($constraint, $name = null)
  304. {
  305. if (is_array($constraint)) {
  306. foreach ($constraint as $name => $def) {
  307. $this->_table->addCheckConstraint($def, $name);
  308. }
  309. } else {
  310. $this->_table->addCheckConstraint($constraint, $name);
  311. }
  312. return $this;
  313. }
  314. }