/framework/core/db/DbRelationshipHasMany.php

http://zoop.googlecode.com/ · PHP · 145 lines · 99 code · 28 blank · 18 comment · 10 complexity · f0e586926a1517911aa7bb47db139791 MD5 · raw file

  1. <?php
  2. class DbRelationshipHasMany extends DbRelationshipBasic implements Iterator, ArrayAccess, Countable
  3. {
  4. private $theMany;
  5. public function __construct($name, $params, $dbObject)
  6. {
  7. if(isset($params['order_by']))
  8. trigger_error("depricated paramater: user orderBy");
  9. if(isset($params['orderby']))
  10. trigger_error("depricated paramater: user orderBy");
  11. if(isset($params['map_field']))
  12. trigger_error("depricated paramater: user mapField");
  13. parent::__construct($name, $params, $dbObject);
  14. if(!isset($params['orderBy']))
  15. $this->params['orderBy'] = array();
  16. if(!isset($params['mapField']))
  17. $this->params['mapField'] = false;
  18. if(!isset($params['conditions']))
  19. $this->params['conditions'] = array();
  20. if(!isset($params['createOrderedRows']))
  21. $this->params['createOrderedRows'] = false;
  22. if(!isset($params['createDefaultRows']))
  23. $this->params['createDefaultRows'] = false;
  24. }
  25. public function add()
  26. {
  27. return new $this->remoteClassName(array($this->remoteFieldName => $this->dbObject->getField($this->localFieldName)));
  28. }
  29. public function push($object)
  30. {
  31. $this->theMany[] = $object;
  32. }
  33. public function getInfo()
  34. {
  35. if(!$this->theMany)
  36. {
  37. $params = array();
  38. $params['orderby'] = $this->params['orderBy'];
  39. $remoteTableName = DbObject::_getTableName($this->remoteClassName);
  40. $conditions = $this->params['conditions'];
  41. $conditions[$this->remoteFieldName] = $this->dbObject->getField($this->localFieldName);
  42. $selectInfo = DbConnection::generateSelectInfo($remoteTableName, '*', $conditions, $params);
  43. $rows = $this->dbObject->getDb()->fetchRows($selectInfo['sql'], $selectInfo['params']);
  44. $this->theMany = array();
  45. foreach($rows as $thisRow)
  46. {
  47. if($this->params['mapField'])
  48. $this->theMany[$thisRow[$this->params['mapField']]] = new $this->remoteClassName($thisRow);
  49. else
  50. $this->theMany[] = new $this->remoteClassName($thisRow);
  51. }
  52. }
  53. return $this;
  54. }
  55. //
  56. // begin iterator functions
  57. //
  58. public function rewind()
  59. {
  60. reset($this->theMany);
  61. }
  62. public function current()
  63. {
  64. $var = current($this->theMany);
  65. return $var;
  66. }
  67. public function key()
  68. {
  69. $var = key($this->theMany);
  70. return $var;
  71. }
  72. public function next()
  73. {
  74. $var = next($this->theMany);
  75. return $var;
  76. }
  77. public function valid()
  78. {
  79. $var = $this->current() !== false;
  80. return $var;
  81. }
  82. //
  83. // end iterator functions
  84. //
  85. //
  86. // begin array access functions
  87. //
  88. public function offsetExists($offset)
  89. {
  90. return isset($this->theMany[$offset]);
  91. }
  92. public function offsetGet($offset)
  93. {
  94. return isset($this->theMany[$offset]) ? $this->theMany[$offset] : null;
  95. }
  96. public function offsetSet($offset, $value)
  97. {
  98. $this->theMany[$offset] = $value;
  99. }
  100. public function offsetUnset($offset)
  101. {
  102. unset($this->theMany[$offset]);
  103. }
  104. //
  105. // end array access functions
  106. //
  107. //
  108. // begin countable functions
  109. //
  110. public function count()
  111. {
  112. return count($this->theMany);
  113. }
  114. //
  115. // end countable functions
  116. //
  117. }