Source for file Abstract.php

Documentation is available at Abstract.php

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