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

/sbweb/sbweb_logica/lib/symfony/plugins/sfDoctrinePlugin/lib/routing/sfDoctrineRoute.class.php

http://opac-sbweb.googlecode.com/
PHP | 163 lines | 109 code | 19 blank | 35 comment | 13 complexity | 6b5c4b8bbaecdb4062451b0c19055126 MD5 | raw file
Possible License(s): LGPL-2.1, AGPL-3.0
  1. <?php
  2. /*
  3. * This file is part of the symfony package.
  4. * (c) Fabien Potencier <fabien.potencier@symfony-project.com>
  5. * (c) Jonathan H. Wage <jonwage@gmail.com>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. /**
  11. * sfDoctrineRoute represents a route that is bound to a Doctrine class.
  12. *
  13. * A Doctrine route can represent a single Doctrine object or a list of objects.
  14. *
  15. * @package symfony
  16. * @subpackage doctrine
  17. * @author Fabien Potencier <fabien.potencier@symfony-project.com>
  18. * @author Jonathan H. Wage <jonwage@gmail.com>
  19. * @version SVN: $Id: sfDoctrineRoute.class.php 11475 2008-09-12 11:07:23Z fabien $
  20. */
  21. class sfDoctrineRoute extends sfObjectRoute
  22. {
  23. protected
  24. $query = null;
  25. /**
  26. * Constructor.
  27. *
  28. * @param string $pattern The pattern to match
  29. * @param array $defaults An array of default parameter values
  30. * @param array $requirements An array of requirements for parameters (regexes)
  31. * @param array $options An array of options
  32. *
  33. * @see sfObjectRoute
  34. */
  35. public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
  36. {
  37. parent::__construct($pattern, $defaults, $requirements, $options);
  38. $this->options['object_model'] = $this->options['model'];
  39. }
  40. public function setListQuery(Doctrine_Query $query)
  41. {
  42. if (!$this->isBound())
  43. {
  44. throw new LogicException('The route is not bound.');
  45. }
  46. $this->query = $query;
  47. }
  48. protected function getObjectForParameters($parameters)
  49. {
  50. $results = $this->getObjectsForParameters($parameters);
  51. // If query returned Doctrine_Collection with results inside then we
  52. // need to return the first Doctrine_Record
  53. if ($results instanceof Doctrine_Collection)
  54. {
  55. if (count($results))
  56. {
  57. $results = $results->getFirst();
  58. } else {
  59. $results = null;
  60. }
  61. }
  62. // If an object is returned then lets return it otherwise return null
  63. else if(!is_object($results))
  64. {
  65. $results = null;
  66. }
  67. return $results;
  68. }
  69. protected function getObjectsForParameters($parameters)
  70. {
  71. $this->options['model'] = Doctrine::getTable($this->options['model']);
  72. $variables = array();
  73. $values = array();
  74. foreach($this->getRealVariables() as $variable)
  75. {
  76. if($this->options['model']->hasColumn($this->options['model']->getColumnName($variable)))
  77. {
  78. $variables[] = $variable;
  79. $values[$variable] = $parameters[$variable];
  80. }
  81. }
  82. if (!isset($this->options['method']))
  83. {
  84. if (is_null($this->query))
  85. {
  86. $q = $this->options['model']->createQuery('a');
  87. foreach ($values as $variable => $value)
  88. {
  89. $fieldName = $this->options['model']->getFieldName($variable);
  90. $q->andWhere('a.'. $fieldName . ' = ?', $parameters[$variable]);
  91. }
  92. }
  93. else
  94. {
  95. $q = $this->query;
  96. }
  97. if (isset($this->options['method_for_query']))
  98. {
  99. $method = $this->options['method_for_query'];
  100. $results = $this->options['model']->$method($q);
  101. }
  102. else
  103. {
  104. $results = $q->execute();
  105. }
  106. }
  107. else
  108. {
  109. $method = $this->options['method'];
  110. $results = $this->options['model']->$method($this->filterParameters($parameters));
  111. }
  112. // If query returned a Doctrine_Record instance instead of a
  113. // Doctrine_Collection then we need to create a new Doctrine_Collection with
  114. // one element inside and return that
  115. if ($results instanceof Doctrine_Record)
  116. {
  117. $obj = $results;
  118. $results = new Doctrine_Collection($obj->getTable());
  119. $results[] = $obj;
  120. }
  121. return $results;
  122. }
  123. protected function doConvertObjectToArray($object)
  124. {
  125. if (isset($this->options['convert']) || method_exists($object, 'toParams'))
  126. {
  127. return parent::doConvertObjectToArray($object);
  128. }
  129. $className = $this->options['model'];
  130. $parameters = array();
  131. foreach ($this->getRealVariables() as $variable)
  132. {
  133. try {
  134. $parameters[$variable] = $object->$variable;
  135. } catch (Exception $e) {
  136. try {
  137. $method = 'get'.sfInflector::camelize($variable);
  138. $parameters[$variable] = $object->$method;
  139. } catch (Exception $e) {}
  140. }
  141. }
  142. return $parameters;
  143. }
  144. }