# Source for file Record.php

Documentation is available at Record.php

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.com>.
20. */
22. /**
23. * Doctrine_Hydrate_Record
24. * defines a record fetching strategy for Doctrine_Hydrate
25. *
26. * @package Doctrine
27. * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
28. * @category Object Relational Mapping
29. * @link www.phpdoctrine.com
30. * @since 1.0
31. * @version $Revision$
32. * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
33. */
34. class Doctrine_Hydrate_Record extends Doctrine_Object
35. {
36. protected $_collections = array();
38. protected $_records = array();
40. protected $_tables = array();
42. public function getElementCollection($component)
43. {
44. $coll = new Doctrine_Collection($component);
45. $this->_collections[] = $coll;
47. return $coll;
48. }
49. public function search(Doctrine_Record $record, Doctrine_Collection $coll)
50. {
51. return array_search($record, $coll->getData(), true);
52. }
53. public function initRelated($record, $name)
54. {
55. if ( ! is_array($record)) {
56. $record[$name];
58. return true;
59. }
60. return false;
61. }
62. public function registerCollection(Doctrine_Collection $coll)
63. {
64. $this->_collections[] = $coll;
65. }
66. /**
67. * isIdentifiable
68. * returns whether or not a given data row is identifiable (it contains
69. * all primary key fields specified in the second argument)
70. *
71. * @param array $row
72. * @param Doctrine_Table $table
73. * @return boolean
74. */
75. public function isIdentifiable(array $row, Doctrine_Table $table)
76. {
77. $primaryKeys = $table->getIdentifier();
79. if (is_array($primaryKeys)) {
80. foreach ($primaryKeys as $id) {
81. if ( ! isset($row[$id])) {
82. return false;
83. }
84. }
85. } else {
86. if ( ! isset($row[$primaryKeys])) {
87. return false;
88. }
89. }
90. return true;
91. }
92. public function getNullPointer()
93. {
94. return self::$_null;
95. }
96. public function getElement(array $data, $component)
97. {
98. if ( ! isset($this->_tables[$component])) {
99. $this->_tables[$component] = Doctrine_Manager::getInstance()->getTable($component);
100. $this->_tables[$component]->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, false);
101. }
102. $this->_tables[$component]->setData($data);
103. $record = $this->_tables[$component]->getRecord();
104. $this->_records[] = $record;
106. return $record;
107. }
108. public function flush()
109. {
110. // take snapshots from all initialized collections
111. foreach ($this->_collections as $key => $coll) {
112. $coll->takeSnapshot();
113. }
114. foreach ($this->_tables as $table) {
115. $table->setAttribute(Doctrine::ATTR_LOAD_REFERENCES, true);
116. }
117. }
118. }