Abstract.php 9.78 KB
Newer Older
zYne's avatar
zYne committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
<?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
 *
25 26
 * @package     Doctrine
 * @subpackage  Record
zYne's avatar
zYne committed
27 28 29 30 31 32 33 34
 * @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
{
zYne's avatar
zYne committed
35 36 37 38
    /**
     * @param Doctrine_Table $_table     reference to associated Doctrine_Table instance
     */
    protected $_table;
39

zYne's avatar
zYne committed
40 41 42 43 44 45 46 47
    /**
     * addListener
     *
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
     * @return Doctrine_Record
     */
    public function addListener($listener, $name = null)
    {
48
        $this->_table->addRecordListener($listener, $name = null);
zYne's avatar
zYne committed
49 50 51

        return $this;
    }
52

zYne's avatar
zYne committed
53 54 55 56 57 58 59
    /**
     * getListener
     *
     * @return Doctrine_EventListener_Interface|Doctrine_Overloadable
     */
    public function getListener()
    {
60
        return $this->_table->getRecordListener();
zYne's avatar
zYne committed
61
    }
62

zYne's avatar
zYne committed
63 64 65 66 67 68 69 70
    /**
     * setListener
     *
     * @param Doctrine_EventListener_Interface|Doctrine_Overloadable $listener
     * @return Doctrine_Record
     */
    public function setListener($listener)
    {
71
        $this->_table->setRecordListener($listener);
zYne's avatar
zYne committed
72 73 74

        return $this;
    }
75

zYne's avatar
zYne committed
76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99
    /**
     * 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)
    {
100
        $this->_table->setTableName($tableName);
zYne's avatar
zYne committed
101 102 103 104 105
    }
    public function setInheritanceMap($map)
    {
        $this->_table->setOption('inheritanceMap', $map);
    }
106

zYne's avatar
zYne committed
107 108
    public function setSubclasses($map)
    {
109
        if (isset($map[get_class($this)])) {
110 111 112 113 114
            $this->_table->setOption('inheritanceMap', $map[get_class($this)]);
            return;
        }
        $this->_table->setOption('subclasses', array_keys($map));
        $conn = $this->_table->getConnection(); 
zYne's avatar
zYne committed
115
        foreach ($map as $key => $value) {
116
            $table = $conn->getTable($key);
117
            $table->setOption('inheritanceMap', $value);
118 119 120
        }
    }

zYne's avatar
zYne committed
121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143
    /**
     * 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);
        }    
    }
144

zYne's avatar
zYne committed
145 146 147 148 149 150 151 152 153 154 155
    /**
     * 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)
    {
156
        if ($value === null) {
zYne's avatar
zYne committed
157 158 159 160 161 162 163 164 165 166 167
            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);
        }
    }
168

zYne's avatar
zYne committed
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
    /**
     * 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;
    }
184

zYne's avatar
zYne committed
185 186 187 188 189 190 191 192 193 194 195 196 197 198
    /**
     * 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;
    }
199

zYne's avatar
zYne committed
200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
    /**
     * 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;
    }
215

zYne's avatar
zYne committed
216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
    /**
     * 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;
    }
231

zYne's avatar
zYne committed
232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250
    /**
     * 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);
        }
zYne's avatar
zYne committed
251 252 253 254 255 256 257 258
    } 
    /**
     * loadTemplate
     *
     * @param string $template
     */
    public function loadTemplate($template, array $options = array())
    {
259
        $this->actAs($template, $options);
zYne's avatar
zYne committed
260
    }
261

zYne's avatar
zYne committed
262 263 264 265 266 267 268 269 270
    /**
     * 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)
    {
zYne's avatar
zYne committed
271
    	$this->_table->bindQueryParts($queryParts);
zYne's avatar
zYne committed
272 273 274

        return $this;
    }
275

zYne's avatar
zYne committed
276 277 278 279 280 281 282 283 284 285 286 287 288 289
    /**
     * actAs
     * loads a given plugin 
     *
     * @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 {
290
                $className = 'Doctrine_Template_' . $tpl;
zYne's avatar
zYne committed
291

292
                if ( ! class_exists($className, true)) {
zYne's avatar
zYne committed
293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313
                    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 istance of Doctrine_Template.');
        }
        $className = get_class($tpl);
        
        $this->_table->addTemplate($className, $tpl);

        $tpl->setTable($this->_table);
        $tpl->setUp();
        $tpl->setTableDefinition();

        return $this;
    }
314

zYne's avatar
zYne committed
315 316 317 318 319 320 321 322 323 324
    /**
     * 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)
    {
325
        if (is_array($constraint)) {
zYne's avatar
zYne committed
326 327 328 329 330 331 332
            foreach ($constraint as $name => $def) {
                $this->_table->addCheckConstraint($def, $name);
            }
        } else {
            $this->_table->addCheckConstraint($constraint, $name);
        }
        return $this;
zYne's avatar
zYne committed
333
    }
zYne's avatar
zYne committed
334
}