# Coverage for Doctrine_Record_Abstract

Back to coverage report

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 | Doctrine::autoload('Doctrine_Access');
22 | /**
23 | * Doctrine_Record_Abstract
24 | *
25 | * @package Doctrine
26 | * @subpackage Record
27 | * @author Konsta Vesterinen <kvesteri@cc.hut.fi>
28 | * @license http://www.opensource.org/licenses/lgpl-license.php LGPL
29 | * @link www.phpdoctrine.org
30 | * @since 1.0
31 | * @version $Revision$
32 | */
33 | abstract class Doctrine_Record_Abstract extends Doctrine_Access
34 | {
35 | /**
36 | * @param Doctrine_Table $_table reference to associated Doctrine_Table instance
37 | */
38 | protected $_table;
39 |
40 | /**
41 | * addListener
42 | *
43 | * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
44 | * @return Doctrine_Record
45 | */
46 | public function addListener($listener, $name = null)
47 | {
48 | $this->_table->addRecordListener($listener, $name = null);
49 |
50 | return $this;
51 | }
52 |
53 | /**
54 | * getListener
55 | *
56 | * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
57 | */
58 | public function getListener()
59 | {
60 | return $this->_table->getRecordListener();
61 | }
62 |
63 | /**
64 | * setListener
65 | *
66 | * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
67 | * @return Doctrine_Record
68 | */
69 | public function setListener($listener)
70 | {
71 | $this->_table->setRecordListener($listener);
72 |
73 | return $this;
74 | }
75 |
76 | /**
77 | * index
78 | * defines or retrieves an index
79 | * if the second parameter is set this method defines an index
80 | * if not this method retrieves index named $name
81 | *
82 | * @param string $name the name of the index
83 | * @param array $definition the definition array
84 | * @return mixed
85 | */
86 | public function index($name, array $definition = array())
87 | {
88 | if ( ! $definition) {
89 | return $this->_table->getIndex($name);
90 | } else {
91 | return $this->_table->addIndex($name, $definition);
92 | }
93 | }
94 | public function setAttribute($attr, $value)
95 | {
96 | $this->_table->setAttribute($attr, $value);
97 | }
98 | public function setTableName($tableName)
99 | {
100 | $this->_table->setTableName($tableName);
101 | }
102 | public function setInheritanceMap($map)
103 | {
104 | $this->_table->setOption('inheritanceMap', $map);
105 | }
106 |
107 | public function setSubclasses($map)
108 | {
109 | if (isset($map[get_class($this)])) {
110 | $this->_table->setOption('inheritanceMap', $map[get_class($this)]);
111 | return;
112 | }
113 | $this->_table->setOption('subclasses', array_keys($map));
114 | $conn = $this->_table->getConnection();
115 | foreach ($map as $key => $value) {
116 | $table = $conn->getTable($key);
117 | $table->setOption('inheritanceMap', $value);
118 | }
119 | }
120 |
121 | /**
122 | * attribute
123 | * sets or retrieves an option
124 | *
125 | * @see Doctrine::ATTR_* constants availible attributes
126 | * @param mixed $attr
127 | * @param mixed $value
128 | * @return mixed
129 | */
130 | public function attribute($attr, $value)
131 | {
132 | if ($value == null) {
133 | if (is_array($attr)) {
134 | foreach ($attr as $k => $v) {
135 | $this->_table->setAttribute($k, $v);
136 | }
137 | } else {
138 | return $this->_table->getAttribute($attr);
139 | }
140 | } else {
141 | $this->_table->setAttribute($attr, $value);
142 | }
143 | }
144 |
145 | /**
146 | * option
147 | * sets or retrieves an option
148 | *
149 | * @see Doctrine_Table::$options availible options
150 | * @param mixed $name the name of the option
151 | * @param mixed $value options value
152 | * @return mixed
153 | */
154 | public function option($name, $value = null)
155 | {
156 | if ($value === null) {
157 | if (is_array($name)) {
158 | foreach ($name as $k => $v) {
159 | $this->_table->setOption($k, $v);
160 | }
161 | } else {
162 | return $this->_table->getOption($name);
163 | }
164 | } else {
165 | $this->_table->setOption($name, $value);
166 | }
167 | }
168 |
169 | /**
170 | * ownsOne
171 | * binds One-to-One composite relation
172 | *
173 | * @param string $componentName the name of the related component
174 | * @param string $options relation options
175 | * @see Doctrine_Relation::_$definition
176 | * @return Doctrine_Record this object
177 | */
178 | public function ownsOne()
179 | {
180 | $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_COMPOSITE);
181 |
182 | return $this;
183 | }
184 |
185 | /**
186 | * ownsMany
187 | * binds One-to-Many / Many-to-Many composite relation
188 | *
189 | * @param string $componentName the name of the related component
190 | * @param string $options relation options
191 | * @see Doctrine_Relation::_$definition
192 | * @return Doctrine_Record this object
193 | */
194 | public function ownsMany()
195 | {
196 | $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_COMPOSITE);
197 | return $this;
198 | }
199 |
200 | /**
201 | * hasOne
202 | * binds One-to-One aggregate relation
203 | *
204 | * @param string $componentName the name of the related component
205 | * @param string $options relation options
206 | * @see Doctrine_Relation::_$definition
207 | * @return Doctrine_Record this object
208 | */
209 | public function hasOne()
210 | {
211 | $this->_table->bind(func_get_args(), Doctrine_Relation::ONE_AGGREGATE);
212 |
213 | return $this;
214 | }
215 |
216 | /**
217 | * hasMany
218 | * binds One-to-Many / Many-to-Many aggregate relation
219 | *
220 | * @param string $componentName the name of the related component
221 | * @param string $options relation options
222 | * @see Doctrine_Relation::_$definition
223 | * @return Doctrine_Record this object
224 | */
225 | public function hasMany()
226 | {
227 | $this->_table->bind(func_get_args(), Doctrine_Relation::MANY_AGGREGATE);
228 |
229 | return $this;
230 | }
231 |
232 | /**
233 | * hasColumn
234 | * sets a column definition
235 | *
236 | * @param string $name
237 | * @param string $type
238 | * @param integer $length
239 | * @param mixed $options
240 | * @return void
241 | */
242 | public function hasColumn($name, $type, $length = 2147483647, $options = "")
243 | {
244 | $this->_table->setColumn($name, $type, $length, $options);
245 | }
246 | public function hasColumns(array $definitions)
247 | {
248 | foreach ($definitions as $name => $options) {
249 | $this->hasColumn($name, $options['type'], $options['length'], $options);
250 | }
251 | }
252 | /**
253 | * loadTemplate
254 | *
255 | * @param string $template
256 | */
257 | public function loadTemplate($template, array $options = array())
258 | {
259 | $this->actAs($template, $options);
260 | }
261 |
262 | /**
263 | * bindQueryParts
264 | * binds query parts to given component
265 | *
266 | * @param array $queryParts an array of pre-bound query parts
267 | * @return Doctrine_Record this object
268 | */
269 | public function bindQueryParts(array $queryParts)
270 | {
271 | $this->_table->bindQueryParts($queryParts);
272 |
273 | return $this;
274 | }
275 |
276 | /**
277 | * actAs
278 | * loads a given plugin
279 | *
280 | * @param mixed $tpl
281 | * @param array $options
282 | */
283 | public function actAs($tpl, array $options = array())
284 | {
285 |
286 | if ( ! is_object($tpl)) {
287 | if (class_exists($tpl, true)) {
288 | $tpl = new $tpl($options);
289 | } else {
290 | $className = 'Doctrine_Template_' . $tpl;
291 |
292 | if ( ! class_exists($className, true)) {
293 | throw new Doctrine_Record_Exception("Couldn't load plugin.");
294 | }
295 |
296 |
297 | $tpl = new $className($options);
298 | }
299 | }
300 |
301 | if ( ! ($tpl instanceof Doctrine_Template)) {
302 | throw new Doctrine_Record_Exception('Loaded plugin class is not an istance of Doctrine_Template.');
303 | }
304 | $className = get_class($tpl);
305 |
306 | $this->_table->addTemplate($className, $tpl);
307 |
308 | $tpl->setTable($this->_table);
309 | $tpl->setUp();
310 | $tpl->setTableDefinition();
311 |
312 | return $this;
313 | }
314 |
315 | /**
316 | * check
317 | * adds a check constraint
318 | *
319 | * @param mixed $constraint either a SQL constraint portion or an array of CHECK constraints
320 | * @param string $name optional constraint name
321 | * @return Doctrine_Record this object
322 | */
323 | public function check($constraint, $name = null)
324 | {
325 | if (is_array($constraint)) {
326 | foreach ($constraint as $name => $def) {
327 | $this->_table->addCheckConstraint($def, $name);
328 | }
329 | } else {
330 | $this->_table->addCheckConstraint($constraint, $name);
331 | }
332 | return $this;
333 | }
334 | }