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