~patrix-sbs/oraculum/git

« back to all changes in this revision

Viewing changes to models/doctrine/lib/Doctrine/Relation.php

  • Committer: Patrick Kaminski
  • Date: 2009-09-02 02:33:07 UTC
  • Revision ID: git-v1:943803254fca67bfb4c0374422b1b836b14dc518
Tags: v0.1a
Sending Oraculum Framework v0.1 alpha

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<?php
 
2
/*
 
3
 *  $Id: Relation.php 5095 2008-10-15 21:45:14Z jwage $
 
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
 
 
22
/**
 
23
 * Doctrine_Relation
 
24
 * This class represents a relation between components
 
25
 *
 
26
 * @package     Doctrine
 
27
 * @subpackage  Relation
 
28
 * @license     http://www.opensource.org/licenses/lgpl-license.php LGPL
 
29
 * @link        www.phpdoctrine.org
 
30
 * @since       1.0
 
31
 * @version     $Revision: 5095 $
 
32
 * @author      Konsta Vesterinen <kvesteri@cc.hut.fi>
 
33
 */
 
34
abstract class Doctrine_Relation implements ArrayAccess
 
35
{
 
36
    /**
 
37
     * RELATION CONSTANTS
 
38
     */
 
39
 
 
40
    /**
 
41
     * constant for ONE_TO_ONE and MANY_TO_ONE relationships
 
42
     */
 
43
    const ONE   = 0;
 
44
    
 
45
    /**
 
46
     * constant for MANY_TO_MANY and ONE_TO_MANY relationships
 
47
     */
 
48
    const MANY  = 1;
 
49
    
 
50
    // TRUE => mandatory, everything else is just a default value. this should be refactored
 
51
    // since TRUE can bot be used as a default value this way. All values should be default values.
 
52
    protected $definition = array('alias'       => true,
 
53
                                  'foreign'     => true,
 
54
                                  'local'       => true,
 
55
                                  'class'       => true,
 
56
                                  'type'        => true,
 
57
                                  'table'       => true,
 
58
                                  'localTable'  => true,
 
59
                                  'name'        => null,
 
60
                                  'refTable'    => null,
 
61
                                  'onDelete'    => null,
 
62
                                  'onUpdate'    => null,
 
63
                                  'deferred'    => null,
 
64
                                  'deferrable'  => null,
 
65
                                  'constraint'  => null,
 
66
                                  'equal'       => false,
 
67
                                  'cascade'     => array(), // application-level cascades
 
68
                                  'owningSide'  => false, // whether this is the owning side
 
69
                                  'refClassRelationAlias' => null,
 
70
                                  );
 
71
 
 
72
    /**
 
73
     * constructor
 
74
     *
 
75
     * @param array $definition         an associative array with the following structure:
 
76
     *          name                    foreign key constraint name
 
77
     *
 
78
     *          local                   the local field(s)
 
79
     *
 
80
     *          foreign                 the foreign reference field(s)
 
81
     *
 
82
     *          table                   the foreign table object
 
83
     *
 
84
     *          localTable              the local table object
 
85
     *
 
86
     *          refTable                the reference table object (if any)
 
87
     *
 
88
     *          onDelete                referential delete action
 
89
     *  
 
90
     *          onUpdate                referential update action
 
91
     *
 
92
     *          deferred                deferred constraint checking 
 
93
     *
 
94
     *          alias                   relation alias
 
95
     *
 
96
     *          type                    the relation type, either Doctrine_Relation::ONE or Doctrine_Relation::MANY
 
97
     *
 
98
     *          constraint              boolean value, true if the relation has an explicit referential integrity constraint
 
99
     *
 
100
     * The onDelete and onUpdate keys accept the following values:
 
101
     *
 
102
     * CASCADE: Delete or update the row from the parent table and automatically delete or
 
103
     *          update the matching rows in the child table. Both ON DELETE CASCADE and ON UPDATE CASCADE are supported.
 
104
     *          Between two tables, you should not define several ON UPDATE CASCADE clauses that act on the same column
 
105
     *          in the parent table or in the child table.
 
106
     *
 
107
     * SET NULL: Delete or update the row from the parent table and set the foreign key column or columns in the
 
108
     *          child table to NULL. This is valid only if the foreign key columns do not have the NOT NULL qualifier 
 
109
     *          specified. Both ON DELETE SET NULL and ON UPDATE SET NULL clauses are supported.
 
110
     *
 
111
     * NO ACTION: In standard SQL, NO ACTION means no action in the sense that an attempt to delete or update a primary 
 
112
     *           key value is not allowed to proceed if there is a related foreign key value in the referenced table.
 
113
     *
 
114
     * RESTRICT: Rejects the delete or update operation for the parent table. NO ACTION and RESTRICT are the same as
 
115
     *           omitting the ON DELETE or ON UPDATE clause.
 
116
     *
 
117
     * SET DEFAULT
 
118
     */
 
119
    public function __construct(array $definition)
 
120
    {
 
121
        $def = array();
 
122
        foreach ($this->definition as $key => $val) {
 
123
            if ( ! isset($definition[$key]) && $val) {
 
124
                throw new Doctrine_Exception($key . ' is required!');
 
125
            }
 
126
            if (isset($definition[$key])) {
 
127
                $def[$key] = $definition[$key];
 
128
            } else {
 
129
                $def[$key] = $this->definition[$key];          
 
130
            }
 
131
        }
 
132
        $this->definition = $def;
 
133
    }
 
134
 
 
135
    /**
 
136
     * hasConstraint
 
137
     * whether or not this relation has an explicit constraint
 
138
     *
 
139
     * @return boolean
 
140
     */
 
141
    public function hasConstraint()
 
142
    {
 
143
        return ($this->definition['constraint'] ||
 
144
                ($this->definition['onUpdate']) ||
 
145
                ($this->definition['onDelete']));
 
146
    }
 
147
    public function isDeferred()
 
148
    {
 
149
        return $this->definition['deferred'];
 
150
    }
 
151
 
 
152
    public function isDeferrable()
 
153
    {
 
154
        return $this->definition['deferrable'];
 
155
    }
 
156
    public function isEqual()
 
157
    {
 
158
        return $this->definition['equal'];
 
159
    }
 
160
 
 
161
    public function offsetExists($offset)
 
162
    {
 
163
        return isset($this->definition[$offset]);
 
164
    }
 
165
 
 
166
    public function offsetGet($offset)
 
167
    {
 
168
        if (isset($this->definition[$offset])) {
 
169
            return $this->definition[$offset];
 
170
        }
 
171
        
 
172
        return null;
 
173
    }
 
174
 
 
175
    public function offsetSet($offset, $value)
 
176
    {
 
177
        if (isset($this->definition[$offset])) {
 
178
            $this->definition[$offset] = $value;
 
179
        }
 
180
    }
 
181
 
 
182
    public function offsetUnset($offset)
 
183
    {
 
184
        $this->definition[$offset] = false;
 
185
    }
 
186
 
 
187
    /**
 
188
     * toArray
 
189
     *
 
190
     * @return array
 
191
     */
 
192
    public function toArray() 
 
193
    {
 
194
        return $this->definition;
 
195
    }
 
196
 
 
197
    /**
 
198
     * getAlias
 
199
     * returns the relation alias
 
200
     *
 
201
     * @return string
 
202
     */
 
203
    final public function getAlias()
 
204
    {
 
205
        return $this->definition['alias'];
 
206
    }
 
207
 
 
208
    /**
 
209
     * getType
 
210
     * returns the relation type, either 0 or 1
 
211
     *
 
212
     * @see Doctrine_Relation MANY_* and ONE_* constants
 
213
     * @return integer
 
214
     */
 
215
    final public function getType()
 
216
    {
 
217
        return $this->definition['type'];
 
218
    }
 
219
    
 
220
    /**
 
221
     * Checks whether this relation cascades deletions to the related objects
 
222
     * on the application level.
 
223
     *
 
224
     * @return boolean
 
225
     */
 
226
    public function isCascadeDelete()
 
227
    {
 
228
        return in_array('delete', $this->definition['cascade']);
 
229
    }
 
230
 
 
231
    /**
 
232
     * getTable
 
233
     * returns the foreign table object
 
234
     *
 
235
     * @return object Doctrine_Table
 
236
     */
 
237
    final public function getTable()
 
238
    {
 
239
        return Doctrine_Manager::getInstance()
 
240
               ->getConnectionForComponent($this->definition['class'])
 
241
               ->getTable($this->definition['class']);
 
242
    }
 
243
 
 
244
    /**
 
245
     * getClass
 
246
     * returns the name of the related class
 
247
     *
 
248
     * @return object Doctrine_Record
 
249
     */
 
250
    final public function getClass()
 
251
    {
 
252
        return $this->definition['class'];
 
253
    }
 
254
 
 
255
    /**
 
256
     * getLocal
 
257
     * returns the name of the local column
 
258
     *
 
259
     * @return string
 
260
     */
 
261
    final public function getLocal()
 
262
    {
 
263
        return $this->definition['local'];
 
264
    }
 
265
    
 
266
    /**
 
267
     * getLocalFieldName
 
268
     * returns the field name of the local column
 
269
     */
 
270
    final public function getLocalFieldName()
 
271
    {
 
272
        return $this->definition['localTable']->getFieldName($this->definition['local']);
 
273
    }
 
274
 
 
275
    /**
 
276
     * getLocalColumnName
 
277
     * returns the column name of the local column
 
278
     *
 
279
     * @return string $columnName
 
280
     */
 
281
    final public function getLocalColumnName()
 
282
    {
 
283
        return $this->definition['localTable']->getColumnName($this->definition['local']);
 
284
    }
 
285
 
 
286
    /**
 
287
     * getForeign
 
288
     * returns the name of the foreignkey column where
 
289
     * the localkey column is pointing at
 
290
     *
 
291
     * @return string
 
292
     */
 
293
    final public function getForeign()
 
294
    {
 
295
        return $this->definition['foreign'];
 
296
    }
 
297
    
 
298
    /**
 
299
     * getLocalFieldName
 
300
     * returns the field name of the foreign column
 
301
     */
 
302
    final public function getForeignFieldName()
 
303
    {
 
304
        return $this->definition['table']->getFieldName($this->definition['foreign']);
 
305
    }
 
306
 
 
307
    /**
 
308
     * getForeignColumnName
 
309
     * returns the column name of the foreign column
 
310
     *
 
311
     * @return string $columnName
 
312
     */
 
313
    final public function getForeignColumnName()
 
314
    {
 
315
       return $this->definition['table']->getColumnName($this->definition['foreign']);
 
316
    }
 
317
 
 
318
    /**
 
319
     * isOneToOne
 
320
     * returns whether or not this relation is a one-to-one relation
 
321
     *
 
322
     * @return boolean
 
323
     */
 
324
    final public function isOneToOne()
 
325
    {
 
326
        return ($this->definition['type'] == Doctrine_Relation::ONE);
 
327
    }
 
328
 
 
329
    /**
 
330
     * getRelationDql
 
331
     *
 
332
     * @param integer $count
 
333
     * @return string
 
334
     */
 
335
    public function getRelationDql($count)
 
336
    {
 
337
        $component = $this->getTable()->getComponentName();
 
338
 
 
339
        $dql  = 'FROM ' . $component
 
340
              . ' WHERE ' . $component . '.' . $this->definition['foreign']
 
341
              . ' IN (' . substr(str_repeat('?, ', $count), 0, -2) . ')';
 
342
 
 
343
        return $dql;
 
344
    }
 
345
 
 
346
    /**
 
347
     * fetchRelatedFor
 
348
     *
 
349
     * fetches a component related to given record
 
350
     *
 
351
     * @param Doctrine_Record $record
 
352
     * @return Doctrine_Record|Doctrine_Collection
 
353
     */
 
354
    abstract public function fetchRelatedFor(Doctrine_Record $record);
 
355
 
 
356
    /**
 
357
     * __toString
 
358
     *
 
359
     * @return string
 
360
     */
 
361
    public function __toString()
 
362
    {
 
363
        $r[] = "<pre>";
 
364
        foreach ($this->definition as $k => $v) {
 
365
            if (is_object($v)) {
 
366
                $v = 'Object(' . get_class($v) . ')';
 
367
            }
 
368
            $r[] = $k . ' : ' . $v;
 
369
        }
 
370
        $r[] = "</pre>";
 
371
        return implode("\n", $r);
 
372
    }
 
373
}
 
 
b'\\ No newline at end of file'