| 1 |
lars |
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.doctrine-project.org>.
|
|
|
20 |
*/
|
|
|
21 |
|
|
|
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.doctrine-project.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 |
|
|
|
45 |
public function setUp()
|
|
|
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);
|
|
|
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 |
|
|
|
116 |
/**
|
|
|
117 |
* Defines a n-uple of fields that must be unique for every record.
|
|
|
118 |
*
|
|
|
119 |
* This method Will automatically add UNIQUE index definition
|
|
|
120 |
* and validate the values on save. The UNIQUE index is not created in the
|
|
|
121 |
* database until you use @see export().
|
|
|
122 |
*
|
|
|
123 |
* @param array $fields values are fieldnames
|
|
|
124 |
* @param array $options array of options for unique validator
|
|
|
125 |
* @param bool $createUniqueIndex Whether or not to create a unique index in the database
|
|
|
126 |
* @return void
|
|
|
127 |
*/
|
|
|
128 |
public function unique($fields, $options = array(), $createUniqueIndex = true)
|
|
|
129 |
{
|
|
|
130 |
return $this->_table->unique($fields, $options, $createUniqueIndex);
|
|
|
131 |
}
|
|
|
132 |
|
|
|
133 |
public function setAttribute($attr, $value)
|
|
|
134 |
{
|
|
|
135 |
$this->_table->setAttribute($attr, $value);
|
|
|
136 |
}
|
|
|
137 |
|
|
|
138 |
public function setTableName($tableName)
|
|
|
139 |
{
|
|
|
140 |
$this->_table->setTableName($tableName);
|
|
|
141 |
}
|
|
|
142 |
|
|
|
143 |
public function setInheritanceMap($map)
|
|
|
144 |
{
|
|
|
145 |
$this->_table->setOption('inheritanceMap', $map);
|
|
|
146 |
}
|
|
|
147 |
|
|
|
148 |
public function setSubclasses($map)
|
|
|
149 |
{
|
|
|
150 |
$class = get_class($this);
|
|
|
151 |
// Set the inheritance map for subclasses
|
|
|
152 |
if (isset($map[$class])) {
|
|
|
153 |
// fix for #1621
|
|
|
154 |
$mapFieldNames = $map[$class];
|
|
|
155 |
$mapColumnNames = array();
|
|
|
156 |
|
|
|
157 |
foreach ($mapFieldNames as $fieldName => $val) {
|
|
|
158 |
$mapColumnNames[$this->getTable()->getColumnName($fieldName)] = $val;
|
|
|
159 |
}
|
|
|
160 |
|
|
|
161 |
$this->_table->setOption('inheritanceMap', $mapColumnNames);
|
|
|
162 |
return;
|
|
|
163 |
} else {
|
|
|
164 |
// Put an index on the key column
|
|
|
165 |
$mapFieldName = array_keys(end($map));
|
|
|
166 |
$this->index($this->getTable()->getTableName().'_'.$mapFieldName[0], array('fields' => array($mapFieldName[0])));
|
|
|
167 |
}
|
|
|
168 |
|
|
|
169 |
// Set the subclasses array for the parent class
|
|
|
170 |
$this->_table->setOption('subclasses', array_keys($map));
|
|
|
171 |
}
|
|
|
172 |
|
|
|
173 |
/**
|
|
|
174 |
* attribute
|
|
|
175 |
* sets or retrieves an option
|
|
|
176 |
*
|
|
|
177 |
* @see Doctrine_Core::ATTR_* constants availible attributes
|
|
|
178 |
* @param mixed $attr
|
|
|
179 |
* @param mixed $value
|
|
|
180 |
* @return mixed
|
|
|
181 |
*/
|
|
|
182 |
public function attribute($attr, $value)
|
|
|
183 |
{
|
|
|
184 |
if ($value == null) {
|
|
|
185 |
if (is_array($attr)) {
|
|
|
186 |
foreach ($attr as $k => $v) {
|
|
|
187 |
$this->_table->setAttribute($k, $v);
|
|
|
188 |
}
|
|
|
189 |
} else {
|
|
|
190 |
return $this->_table->getAttribute($attr);
|
|
|
191 |
}
|
|
|
192 |
} else {
|
|
|
193 |
$this->_table->setAttribute($attr, $value);
|
|
|
194 |
}
|
|
|
195 |
}
|
|
|
196 |
|
|
|
197 |
/**
|
|
|
198 |
* option
|
|
|
199 |
* sets or retrieves an option
|
|
|
200 |
*
|
|
|
201 |
* @see Doctrine_Table::$options availible options
|
|
|
202 |
* @param mixed $name the name of the option
|
|
|
203 |
* @param mixed $value options value
|
|
|
204 |
* @return mixed
|
|
|
205 |
*/
|
|
|
206 |
public function option($name, $value = null)
|
|
|
207 |
{
|
|
|
208 |
if ($value === null) {
|
|
|
209 |
if (is_array($name)) {
|
|
|
210 |
foreach ($name as $k => $v) {
|
|
|
211 |
$this->_table->setOption($k, $v);
|
|
|
212 |
}
|
|
|
213 |
} else {
|
|
|
214 |
return $this->_table->getOption($name);
|
|
|
215 |
}
|
|
|
216 |
} else {
|
|
|
217 |
$this->_table->setOption($name, $value);
|
|
|
218 |
}
|
|
|
219 |
}
|
|
|
220 |
|
|
|
221 |
/**
|
|
|
222 |
* Binds One-to-One aggregate relation
|
|
|
223 |
*
|
|
|
224 |
* @param string $componentName the name of the related component
|
|
|
225 |
* @param string $options relation options
|
|
|
226 |
* @see Doctrine_Relation::_$definition
|
|
|
227 |
* @return Doctrine_Record this object
|
|
|
228 |
*/
|
|
|
229 |
public function hasOne()
|
|
|
230 |
{
|
|
|
231 |
$this->_table->bind(func_get_args(), Doctrine_Relation::ONE);
|
|
|
232 |
|
|
|
233 |
return $this;
|
|
|
234 |
}
|
|
|
235 |
|
|
|
236 |
/**
|
|
|
237 |
* Binds One-to-Many / Many-to-Many aggregate relation
|
|
|
238 |
*
|
|
|
239 |
* @param string $componentName the name of the related component
|
|
|
240 |
* @param string $options relation options
|
|
|
241 |
* @see Doctrine_Relation::_$definition
|
|
|
242 |
* @return Doctrine_Record this object
|
|
|
243 |
*/
|
|
|
244 |
public function hasMany()
|
|
|
245 |
{
|
|
|
246 |
$this->_table->bind(func_get_args(), Doctrine_Relation::MANY);
|
|
|
247 |
|
|
|
248 |
return $this;
|
|
|
249 |
}
|
|
|
250 |
|
|
|
251 |
/**
|
|
|
252 |
* Sets a column definition
|
|
|
253 |
*
|
|
|
254 |
* @param string $name
|
|
|
255 |
* @param string $type
|
|
|
256 |
* @param integer $length
|
|
|
257 |
* @param mixed $options
|
|
|
258 |
* @return void
|
|
|
259 |
*/
|
|
|
260 |
public function hasColumn($name, $type = null, $length = null, $options = array())
|
|
|
261 |
{
|
|
|
262 |
$this->_table->setColumn($name, $type, $length, $options);
|
|
|
263 |
}
|
|
|
264 |
|
|
|
265 |
/**
|
|
|
266 |
* Set multiple column definitions at once
|
|
|
267 |
*
|
|
|
268 |
* @param array $definitions
|
|
|
269 |
* @return void
|
|
|
270 |
*/
|
|
|
271 |
public function hasColumns(array $definitions)
|
|
|
272 |
{
|
|
|
273 |
foreach ($definitions as $name => $options) {
|
|
|
274 |
$length = isset($options['length']) ? $options['length']:null;
|
|
|
275 |
$this->hasColumn($name, $options['type'], $length, $options);
|
|
|
276 |
}
|
|
|
277 |
}
|
|
|
278 |
|
|
|
279 |
/**
|
|
|
280 |
* Customize the array of options for a column or multiple columns. First
|
|
|
281 |
* argument can be a single field/column name or an array of them. The second
|
|
|
282 |
* argument is an array of options.
|
|
|
283 |
*
|
|
|
284 |
* [php]
|
|
|
285 |
* public function setTableDefinition()
|
|
|
286 |
* {
|
|
|
287 |
* parent::setTableDefinition();
|
|
|
288 |
* $this->setColumnOptions('username', array(
|
|
|
289 |
* 'unique' => true
|
|
|
290 |
* ));
|
|
|
291 |
* }
|
|
|
292 |
*
|
|
|
293 |
* @param string $columnName
|
|
|
294 |
* @param array $validators
|
|
|
295 |
* @return void
|
|
|
296 |
*/
|
|
|
297 |
public function setColumnOptions($name, array $options)
|
|
|
298 |
{
|
|
|
299 |
$this->_table->setColumnOptions($name, $options);
|
|
|
300 |
}
|
|
|
301 |
|
|
|
302 |
/**
|
|
|
303 |
* Set an individual column option
|
|
|
304 |
*
|
|
|
305 |
* @param string $columnName
|
|
|
306 |
* @param string $option
|
|
|
307 |
* @param string $value
|
|
|
308 |
* @return void
|
|
|
309 |
*/
|
|
|
310 |
public function setColumnOption($columnName, $option, $value)
|
|
|
311 |
{
|
|
|
312 |
$this->_table->setColumnOption($columnName, $option, $value);
|
|
|
313 |
}
|
|
|
314 |
|
|
|
315 |
/**
|
|
|
316 |
* bindQueryParts
|
|
|
317 |
* binds query parts to given component
|
|
|
318 |
*
|
|
|
319 |
* @param array $queryParts an array of pre-bound query parts
|
|
|
320 |
* @return Doctrine_Record this object
|
|
|
321 |
*/
|
|
|
322 |
public function bindQueryParts(array $queryParts)
|
|
|
323 |
{
|
|
|
324 |
$this->_table->bindQueryParts($queryParts);
|
|
|
325 |
|
|
|
326 |
return $this;
|
|
|
327 |
}
|
|
|
328 |
|
|
|
329 |
public function loadGenerator(Doctrine_Record_Generator $generator)
|
|
|
330 |
{
|
|
|
331 |
$generator->initialize($this->_table);
|
|
|
332 |
|
|
|
333 |
$this->_table->addGenerator($generator, get_class($generator));
|
|
|
334 |
}
|
|
|
335 |
|
|
|
336 |
/**
|
|
|
337 |
* Loads the given plugin.
|
|
|
338 |
*
|
|
|
339 |
* This method loads a behavior in the record. It will add the behavior
|
|
|
340 |
* also to the record table if it.
|
|
|
341 |
* It is tipically called in @see setUp().
|
|
|
342 |
*
|
|
|
343 |
* @param mixed $tpl if an object, must be a subclass of Doctrine_Template.
|
|
|
344 |
* If a string, Doctrine will try to instantiate an object of the classes Doctrine_Template_$tpl and subsequently $tpl, using also autoloading capabilities if defined.
|
|
|
345 |
* @param array $options argument to pass to the template constructor if $tpl is a class name
|
|
|
346 |
* @throws Doctrine_Record_Exception if $tpl is neither an instance of Doctrine_Template subclass or a valid class name, that could be instantiated.
|
|
|
347 |
* @return Doctrine_Record this object; provides a fluent interface.
|
|
|
348 |
*/
|
|
|
349 |
public function actAs($tpl, array $options = array())
|
|
|
350 |
{
|
|
|
351 |
if ( ! is_object($tpl)) {
|
|
|
352 |
$className = 'Doctrine_Template_' . $tpl;
|
|
|
353 |
|
|
|
354 |
if (class_exists($className, true)) {
|
|
|
355 |
$tpl = new $className($options);
|
|
|
356 |
} else if (class_exists($tpl, true)) {
|
|
|
357 |
$tpl = new $tpl($options);
|
|
|
358 |
} else {
|
|
|
359 |
throw new Doctrine_Record_Exception('Could not load behavior named: "' . $tpl . '"');
|
|
|
360 |
}
|
|
|
361 |
}
|
|
|
362 |
|
|
|
363 |
if ( ! ($tpl instanceof Doctrine_Template)) {
|
|
|
364 |
throw new Doctrine_Record_Exception('Loaded behavior class is not an instance of Doctrine_Template.');
|
|
|
365 |
}
|
|
|
366 |
|
|
|
367 |
$className = get_class($tpl);
|
|
|
368 |
|
|
|
369 |
$this->_table->addTemplate($className, $tpl);
|
|
|
370 |
|
|
|
371 |
$tpl->setInvoker($this);
|
|
|
372 |
$tpl->setTable($this->_table);
|
|
|
373 |
$tpl->setUp();
|
|
|
374 |
$tpl->setTableDefinition();
|
|
|
375 |
|
|
|
376 |
return $this;
|
|
|
377 |
}
|
|
|
378 |
|
|
|
379 |
/**
|
|
|
380 |
* Adds a check constraint.
|
|
|
381 |
*
|
|
|
382 |
* This method will add a CHECK constraint to the record table.
|
|
|
383 |
*
|
|
|
384 |
* @param mixed $constraint either a SQL constraint portion or an array of CHECK constraints. If array, all values will be added as constraint
|
|
|
385 |
* @param string $name optional constraint name. Not used if $constraint is an array.
|
|
|
386 |
* @return Doctrine_Record this object
|
|
|
387 |
*/
|
|
|
388 |
public function check($constraint, $name = null)
|
|
|
389 |
{
|
|
|
390 |
if (is_array($constraint)) {
|
|
|
391 |
foreach ($constraint as $name => $def) {
|
|
|
392 |
$this->_table->addCheckConstraint($def, $name);
|
|
|
393 |
}
|
|
|
394 |
} else {
|
|
|
395 |
$this->_table->addCheckConstraint($constraint, $name);
|
|
|
396 |
}
|
|
|
397 |
return $this;
|
|
|
398 |
}
|
|
|
399 |
}
|