1
0
mirror of synced 2024-12-15 15:46:02 +03:00
doctrine2/lib/Doctrine/Record/Abstract.php

407 lines
11 KiB
PHP
Raw Normal View History

2007-06-27 20:59:23 +04:00
<?php
/*
* $Id$
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
* A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
* OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* This software consists of voluntary contributions made by many individuals
* and is licensed under the LGPL. For more information, see
* <http://www.phpdoctrine.com>.
*/
Doctrine::autoload('Doctrine_Access');
/**
* Doctrine_Record_Abstract
*
* @package Doctrine
* @subpackage Record
2007-06-27 20:59:23 +04:00
* @author Konsta Vesterinen <kvesteri@cc.hut.fi>
* @license http://www.opensource.org/licenses/lgpl-license.php LGPL
* @link www.phpdoctrine.com
* @since 1.0
* @version $Revision$
*/
abstract class Doctrine_Record_Abstract extends Doctrine_Access
{
2007-08-03 15:52:24 +04:00
/**
* @param Doctrine_Table $_table reference to associated Doctrine_Table instance
*/
protected $_table;
/**
*
* @var Doctrine_Mapper_Abstract
*/
protected $_mapper;
2007-11-28 03:01:03 +03:00
public function setTableDefinition()
{
}
2007-11-28 03:01:03 +03:00
public function setUp()
{
}
2007-11-27 01:27:23 +03:00
/**
* getTable
* returns the associated table object
*
* @return Doctrine_Table the associated table object
*/
public function getTable()
{
return $this->_table;
}
public function getMapper()
{
return $this->_mapper;
}
2007-06-27 20:59:23 +04:00
/**
* addListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Record
*/
public function addListener($listener, $name = null)
{
$this->_table->addRecordListener($listener, $name = null);
2007-06-27 20:59:23 +04:00
return $this;
}
2007-06-27 20:59:23 +04:00
/**
* getListener
*
* @return Doctrine_EventListener_Interface|Doctrine_Overloadable
*/
public function getListener()
{
return $this->_table->getRecordListener();
2007-06-27 20:59:23 +04:00
}
2007-06-27 20:59:23 +04:00
/**
* setListener
*
* @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
* @return Doctrine_Record
*/
public function setListener($listener)
{
$this->_table->setRecordListener($listener);
2007-06-27 20:59:23 +04:00
return $this;
}
2007-06-27 20:59:23 +04:00
/**
* index
* defines or retrieves an index
* if the second parameter is set this method defines an index
* if not this method retrieves index named $name
*
* @param string $name the name of the index
* @param array $definition the definition array
* @return mixed
*/
public function index($name, array $definition = array())
{
if ( ! $definition) {
return $this->_table->getIndex($name);
} else {
return $this->_table->addIndex($name, $definition);
}
}
public function setAttribute($attr, $value)
{
$this->_table->setAttribute($attr, $value);
}
public function setTableName($tableName)
{
$this->_table->setTableName($tableName);
2007-06-27 20:59:23 +04:00
}
/**
*
* @deprecated Use setSubclasses()
*/
2007-06-27 20:59:23 +04:00
public function setInheritanceMap($map)
{
$this->_table->setOption('inheritanceMap', $map);
}
2007-08-03 15:24:45 +04:00
public function setSubclasses($map)
{
//echo "setting inheritance map on " . get_class($this) . "<br />";
$this->_table->setOption('inheritanceMap', $map);
$this->_table->setOption('subclasses', array_keys($map));
$this->_table->setInheritanceType(Doctrine::INHERITANCETYPE_SINGLE_TABLE);
}
2007-06-27 20:59:23 +04:00
/**
* attribute
* sets or retrieves an option
*
* @see Doctrine::ATTR_* constants availible attributes
* @param mixed $attr
* @param mixed $value
* @return mixed
*/
public function attribute($attr, $value)
{
if ($value == null) {
if (is_array($attr)) {
foreach ($attr as $k => $v) {
$this->_table->setAttribute($k, $v);
}
} else {
return $this->_table->getAttribute($attr);
}
} else {
$this->_table->setAttribute($attr, $value);
}
}
2007-06-27 20:59:23 +04:00
/**
* option
* sets or retrieves an option
*
* @see Doctrine_Table::$options availible options
* @param mixed $name the name of the option
* @param mixed $value options value
* @return mixed
*/
public function option($name, $value = null)
{
if ($value === null) {
2007-06-27 20:59:23 +04:00
if (is_array($name)) {
foreach ($name as $k => $v) {
$this->_table->setOption($k, $v);
}
} else {
return $this->_table->getOption($name);
}
} else {
$this->_table->setOption($name, $value);
}
}
2007-06-27 20:59:23 +04:00
/**
2007-12-20 07:08:25 +03:00
* DEPRECATED ALSO? - REMOVE SOON
*
2007-06-27 20:59:23 +04:00
* ownsOne
* binds One-to-One composite relation
*
* @param string $componentName the name of the related component
* @param string $options relation options
* @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/
public function ownsOne()
{
$this->_table->bind(func_get_args(), Doctrine_Relation::ONE_COMPOSITE);
return $this;
}
2007-06-27 20:59:23 +04:00
/**
2007-12-20 07:08:25 +03:00
* DEPRECATED - REMOVE SOON
*
2007-06-27 20:59:23 +04:00
* ownsMany
* binds One-to-Many / Many-to-Many composite relation
*
* @param string $componentName the name of the related component
* @param string $options relation options
* @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/
public function ownsMany()
{
$this->_table->bind(func_get_args(), Doctrine_Relation::MANY_COMPOSITE);
return $this;
}
2007-06-27 20:59:23 +04:00
/**
* hasOne
* binds One-to-One aggregate relation
*
* @param string $componentName the name of the related component
* @param string $options relation options
* @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/
public function hasOne()
{
$this->_table->bind(func_get_args(), Doctrine_Relation::ONE_AGGREGATE);
return $this;
}
2007-06-27 20:59:23 +04:00
/**
* hasMany
* binds One-to-Many / Many-to-Many aggregate relation
*
* @param string $componentName the name of the related component
* @param string $options relation options
* @see Doctrine_Relation::_$definition
* @return Doctrine_Record this object
*/
public function hasMany()
{
$this->_table->bind(func_get_args(), Doctrine_Relation::MANY_AGGREGATE);
return $this;
}
2007-06-27 20:59:23 +04:00
/**
* hasColumn
* sets a column definition
*
* @param string $name
* @param string $type
* @param integer $length
* @param mixed $options
* @return void
*/
public function hasColumn($name, $type, $length = 2147483647, $options = "")
{
$this->_table->setColumn($name, $type, $length, $options);
}
public function hasColumns(array $definitions)
{
foreach ($definitions as $name => $options) {
$this->hasColumn($name, $options['type'], $options['length'], $options);
}
2007-08-03 15:25:53 +04:00
}
/**
* loadTemplate
*
* @param string $template
*/
public function loadTemplate($template, array $options = array())
{
$this->actAs($template, $options);
2007-08-03 15:25:53 +04:00
}
2007-10-06 00:28:08 +04:00
/**
* bindQueryParts
* binds query parts to given component
*
* @param array $queryParts an array of pre-bound query parts
* @return Doctrine_Record this object
*/
public function bindQueryParts(array $queryParts)
{
if (!$this->_table) {
try {
throw new Exception();
} catch (Exception $e) {
echo $e->getTraceAsString() . "<br />";
}
}
2007-10-06 00:29:33 +04:00
$this->_table->bindQueryParts($queryParts);
2007-10-06 00:28:08 +04:00
return $this;
}
2007-11-29 01:56:45 +03:00
public function loadGenerator(Doctrine_Record_Generator $generator)
2007-11-28 03:01:03 +03:00
{
2007-11-29 01:56:45 +03:00
$generator->initialize($this->_table);
2007-11-28 03:01:03 +03:00
2007-11-29 01:56:45 +03:00
$this->_table->addGenerator($generator, get_class($generator));
2007-11-28 03:01:03 +03:00
}
2007-08-03 15:25:53 +04:00
/**
* actAs
2007-11-27 01:27:23 +03:00
* loads the given plugin
2007-08-03 15:25:53 +04:00
*
* @param mixed $tpl
* @param array $options
*/
public function actAs($tpl, array $options = array())
{
if ( ! is_object($tpl)) {
if (class_exists($tpl, true)) {
$tpl = new $tpl($options);
} else {
$className = 'Doctrine_Template_' . $tpl;
2007-08-03 15:25:53 +04:00
if ( ! class_exists($className, true)) {
2007-08-03 15:25:53 +04:00
throw new Doctrine_Record_Exception("Couldn't load plugin.");
}
$tpl = new $className($options);
}
}
if ( ! ($tpl instanceof Doctrine_Template)) {
throw new Doctrine_Record_Exception('Loaded plugin class is not an instance of Doctrine_Template.');
2007-08-03 15:25:53 +04:00
}
2007-11-27 06:36:40 +03:00
2007-08-03 15:25:53 +04:00
$className = get_class($tpl);
2007-11-27 06:36:40 +03:00
2007-08-03 15:25:53 +04:00
$this->_table->addTemplate($className, $tpl);
$tpl->setTable($this->_table);
$tpl->setUp();
$tpl->setTableDefinition();
return $this;
}
/**
*
*
*/
public function setInheritanceType($type, array $options = null)
{
if ($type == Doctrine::INHERITANCETYPE_SINGLE_TABLE) {
$this->setSubclasses($options);
} else if ($type == Doctrine::INHERITANCETYPE_JOINED) {
$this->setSubclasses($options);
} else if ($type == Doctrine::INHERITANCETYPE_TABLE_PER_CLASS) {
// concrete table inheritance ...
}
$this->_table->setInheritanceType($type);
}
protected function _getMapper($className)
{
}
2007-08-03 15:24:45 +04:00
/**
* check
* adds a check constraint
*
* @param mixed $constraint either a SQL constraint portion or an array of CHECK constraints
* @param string $name optional constraint name
* @return Doctrine_Record this object
*/
public function check($constraint, $name = null)
{
if (is_array($constraint)) {
2007-08-03 15:24:45 +04:00
foreach ($constraint as $name => $def) {
$this->_table->addCheckConstraint($def, $name);
}
} else {
$this->_table->addCheckConstraint($constraint, $name);
}
return $this;
2007-06-27 20:59:23 +04:00
}
2007-10-06 00:28:08 +04:00
}