PageRenderTime 45ms CodeModel.GetById 17ms app.highlight 21ms RepoModel.GetById 1ms app.codeStats 0ms

/vendor/propel/generator/lib/model/Column.php

https://bitbucket.org/bayrock/gw2spidy
PHP | 1318 lines | 749 code | 156 blank | 413 comment | 91 complexity | f8a7ab725da6e75e952cc6a69a4c4b3c MD5 | raw file
   1<?php
   2
   3/**
   4 * This file is part of the Propel package.
   5 * For the full copyright and license information, please view the LICENSE
   6 * file that was distributed with this source code.
   7 *
   8 * @license    MIT License
   9 */
  10
  11require_once dirname(__FILE__) . '/XMLElement.php';
  12require_once dirname(__FILE__) . '/../exception/EngineException.php';
  13require_once dirname(__FILE__) . '/PropelTypes.php';
  14require_once dirname(__FILE__) . '/Inheritance.php';
  15require_once dirname(__FILE__) . '/Domain.php';
  16require_once dirname(__FILE__) . '/ColumnDefaultValue.php';
  17
  18/**
  19 * A Class for holding data about a column used in an Application.
  20 *
  21 * @author     Hans Lellelid <hans@xmpl.org> (Propel)
  22 * @author     Leon Messerschmidt <leon@opticode.co.za> (Torque)
  23 * @author     Jason van Zyl <jvanzyl@apache.org> (Torque)
  24 * @author     Jon S. Stevens <jon@latchkey.com> (Torque)
  25 * @author     Daniel Rall <dlr@finemaltcoding.com> (Torque)
  26 * @author     Byron Foster <byron_foster@yahoo.com> (Torque)
  27 * @author     Bernd Goldschmidt <bgoldschmidt@rapidsoft.de>
  28 * @version    $Revision$
  29 * @package    propel.generator.model
  30 */
  31class Column extends XMLElement
  32{
  33
  34    const DEFAULT_TYPE = "VARCHAR";
  35    const DEFAULT_VISIBILITY = 'public';
  36    public static $valid_visibilities = array('public', 'protected', 'private');
  37
  38    private $name;
  39    private $description;
  40    private $phpName = null;
  41    private $phpNamingMethod;
  42    private $isNotNull = false;
  43    private $size;
  44    private $namePrefix;
  45    private $accessorVisibility;
  46    private $mutatorVisibility;
  47
  48    /**
  49     * The name to use for the Peer constant that identifies this column.
  50     * (Will be converted to all-uppercase in the templates.)
  51     * @var				 string
  52     */
  53    private $peerName;
  54
  55    /**
  56     * Native PHP type (scalar or class name)
  57     * @var				 string "string", "boolean", "int", "double"
  58     */
  59    private $phpType;
  60
  61    /**
  62     * @var				 Table
  63     */
  64    private $parentTable;
  65
  66    private $position;
  67    private $isPrimaryKey = false;
  68    private $isNodeKey = false;
  69    private $nodeKeySep;
  70    private $isNestedSetLeftKey = false;
  71    private $isNestedSetRightKey = false;
  72    private $isTreeScopeKey = false;
  73    private $isUnique = false;
  74    private $isAutoIncrement = false;
  75    private $isLazyLoad = false;
  76    private $defaultValue;
  77    private $referrers;
  78    private $isPrimaryString = false;
  79
  80    // only one type is supported currently, which assumes the
  81    // column either contains the classnames or a key to
  82    // classnames specified in the schema.	Others may be
  83    // supported later.
  84    private $inheritanceType;
  85    private $isInheritance;
  86    private $isEnumeratedClasses;
  87    private $inheritanceList;
  88    private $needsTransactionInPostgres; //maybe this can be retrieved from vendorSpecificInfo
  89
  90    /**
  91     * @var stores the possible values of an ENUM column
  92     */
  93    protected $valueSet = array();
  94
  95    /**
  96     * @var				 Domain The domain object associated with this Column.
  97     */
  98    private $domain;
  99
 100    /**
 101     * Creates a new column and set the name
 102     *
 103     * @param			 name column name
 104     */
 105    public function __construct($name = null)
 106    {
 107        $this->name = $name;
 108    }
 109
 110    /**
 111     * Return a comma delimited string listing the specified columns.
 112     *
 113     * @param			 columns Either a list of <code>Column</code> objects, or
 114     * a list of <code>String</code> objects with column names.
 115     * @deprecated Use the Platform::getColumnListDDL() method instead
 116     *
 117     * @return string
 118     */
 119    public static function makeList($columns, PropelPlatformInterface $platform)
 120    {
 121        $list = array();
 122        foreach ($columns as $col) {
 123            if ($col instanceof Column) {
 124                $col = $col->getName();
 125            }
 126            $list[] = $platform->quoteIdentifier($col);
 127        }
 128
 129        return implode(", ", $list);
 130    }
 131
 132    /**
 133     * Sets up the Column object based on the attributes that were passed to loadFromXML().
 134     * @see				 parent::loadFromXML()
 135     */
 136    protected function setupObject()
 137    {
 138        try {
 139            $dom = $this->getAttribute("domain");
 140            if ($dom) {
 141                $this->getDomain()->copy($this->getTable()->getDatabase()->getDomain($dom));
 142            } else {
 143                $type = strtoupper($this->getAttribute("type"));
 144                if ($type) {
 145                    if ($platform = $this->getPlatform()) {
 146                        $this->getDomain()->copy($this->getPlatform()->getDomainForType($type));
 147                    } else {
 148                        // no platform - probably during tests
 149                        $this->setDomain(new Domain($type));
 150                    }
 151                } else {
 152                    if ($platform = $this->getPlatform()) {
 153                        $this->getDomain()->copy($this->getPlatform()->getDomainForType(self::DEFAULT_TYPE));
 154                    } else {
 155                        // no platform - probably during tests
 156                        $this->setDomain(new Domain(self::DEFAULT_TYPE));
 157                    }
 158                }
 159            }
 160
 161            $this->name = $this->getAttribute("name");
 162            $this->phpName = $this->getAttribute("phpName");
 163            $this->phpType = $this->getAttribute("phpType");
 164
 165            if ($this->getAttribute("prefix", null) !== null) {
 166                $this->namePrefix = $this->getAttribute("prefix");
 167            } elseif ($this->getTable()->getAttribute('columnPrefix', null) !== null) {
 168                $this->namePrefix = $this->getTable()->getAttribute('columnPrefix');
 169            } else {
 170                $this->namePrefix = '';
 171            }
 172
 173            // Accessor visibility
 174            if ($this->getAttribute('accessorVisibility', null) !== null) {
 175                $this->setAccessorVisibility($this->getAttribute('accessorVisibility'));
 176            } elseif ($this->getTable()->getAttribute('defaultAccessorVisibility', null) !== null) {
 177                $this->setAccessorVisibility($this->getTable()->getAttribute('defaultAccessorVisibility'));
 178            } elseif ($this->getTable()->getDatabase()->getAttribute('defaultAccessorVisibility', null) !== null) {
 179                $this->setAccessorVisibility($this->getTable()->getDatabase()->getAttribute('defaultAccessorVisibility'));
 180            } else {
 181                $this->setAccessorVisibility(self::DEFAULT_VISIBILITY);
 182            }
 183
 184            // Mutator visibility
 185            if ($this->getAttribute('mutatorVisibility', null) !== null) {
 186                $this->setMutatorVisibility($this->getAttribute('mutatorVisibility'));
 187            } elseif ($this->getTable()->getAttribute('defaultMutatorVisibility', null) !== null) {
 188                $this->setMutatorVisibility($this->getTable()->getAttribute('defaultMutatorVisibility'));
 189            } elseif ($this->getTable()->getDatabase()->getAttribute('defaultMutatorVisibility', null) !== null) {
 190                $this->setMutatorVisibility($this->getTable()->getDatabase()->getAttribute('defaultMutatorVisibility'));
 191            } else {
 192                $this->setMutatorVisibility(self::DEFAULT_VISIBILITY);
 193            }
 194
 195            $this->peerName = $this->getAttribute("peerName");
 196
 197            // retrieves the method for converting from specified name to a PHP name, defaulting to parent tables default method
 198            $this->phpNamingMethod = $this->getAttribute("phpNamingMethod", $this->parentTable->getDatabase()->getDefaultPhpNamingMethod());
 199
 200            $this->isPrimaryString = $this->booleanValue($this->getAttribute("primaryString"));
 201
 202            $this->isPrimaryKey = $this->booleanValue($this->getAttribute("primaryKey"));
 203
 204            $this->isNodeKey = $this->booleanValue($this->getAttribute("nodeKey"));
 205            $this->nodeKeySep = $this->getAttribute("nodeKeySep", ".");
 206
 207            $this->isNestedSetLeftKey = $this->booleanValue($this->getAttribute("nestedSetLeftKey"));
 208            $this->isNestedSetRightKey = $this->booleanValue($this->getAttribute("nestedSetRightKey"));
 209            $this->isTreeScopeKey = $this->booleanValue($this->getAttribute("treeScopeKey"));
 210
 211            $this->isNotNull = ($this->booleanValue($this->getAttribute("required"), false) || $this->isPrimaryKey); // primary keys are required
 212
 213            //AutoIncrement/Sequences
 214            $this->isAutoIncrement = $this->booleanValue($this->getAttribute("autoIncrement"));
 215            $this->isLazyLoad = $this->booleanValue($this->getAttribute("lazyLoad"));
 216
 217            // Add type, size information to associated Domain object
 218            $this->getDomain()->replaceSqlType($this->getAttribute("sqlType"));
 219            if (!$this->getAttribute("size") && $this->getDomain()->getType() == 'VARCHAR' && $this->hasPlatform() && !$this->getAttribute("sqlType") && !$this->getPlatform()->supportsVarcharWithoutSize()) {
 220                $size = 255;
 221            } else {
 222                $size = $this->getAttribute("size");
 223            }
 224            $this->getDomain()->replaceSize($size);
 225            $this->getDomain()->replaceScale($this->getAttribute("scale"));
 226
 227            $defval = $this->getAttribute("defaultValue", $this->getAttribute("default"));
 228            if ($defval !== null && strtolower($defval) !== 'null') {
 229                $this->getDomain()->setDefaultValue(new ColumnDefaultValue($defval, ColumnDefaultValue::TYPE_VALUE));
 230            } elseif ($this->getAttribute("defaultExpr") !== null) {
 231                $this->getDomain()->setDefaultValue(new ColumnDefaultValue($this->getAttribute("defaultExpr"), ColumnDefaultValue::TYPE_EXPR));
 232            }
 233
 234            if ($this->getAttribute('valueSet', null) !== null) {
 235                $valueSet = explode(',', $this->getAttribute("valueSet"));
 236                $valueSet = array_map('trim', $valueSet);
 237                $this->valueSet = $valueSet;
 238            }
 239
 240            $this->inheritanceType = $this->getAttribute("inheritance");
 241            $this->isInheritance = ($this->inheritanceType !== null
 242            && $this->inheritanceType !== "false"); // here we are only checking for 'false', so don't
 243            // use boleanValue()
 244
 245            $this->description = $this->getAttribute("description");
 246        } catch (Exception $e) {
 247            throw new EngineException("Error setting up column " . var_export($this->getAttribute("name"), true) . ": " . $e->getMessage());
 248        }
 249    }
 250
 251    /**
 252     * Gets domain for this column, creating a new empty domain object if none is set.
 253     * @return Domain
 254     */
 255    public function getDomain()
 256    {
 257        if ($this->domain === null) {
 258            $this->domain = new Domain();
 259        }
 260
 261        return $this->domain;
 262    }
 263
 264    /**
 265     * Sets domain for this column
 266     * @param Domain $domain
 267     */
 268    public function setDomain(Domain $domain)
 269    {
 270        $this->domain = $domain;
 271    }
 272
 273    /**
 274     * Returns table.column
 275     */
 276    public function getFullyQualifiedName()
 277    {
 278        return ($this->parentTable->getName() . '.' . strtoupper($this->getName()));
 279    }
 280
 281    /**
 282     * Get the name of the column
 283     */
 284    public function getName()
 285    {
 286        return $this->name;
 287    }
 288
 289    /**
 290     * Set the name of the column
 291     */
 292    public function setName($newName)
 293    {
 294        $this->name = $newName;
 295    }
 296
 297    /**
 298     * Determines whether a column name is plural
 299     */
 300    public function isNamePlural()
 301    {
 302        return $this->getSingularName() != $this->name;
 303    }
 304
 305    /**
 306     * Gets the singular name for the column
 307     */
 308    public function getSingularName()
 309    {
 310        return rtrim($this->name, 's');
 311    }
 312
 313    /**
 314     * Get the description for the Table
 315     */
 316    public function getDescription()
 317    {
 318        return $this->description;
 319    }
 320
 321    /**
 322     * Set the description for the Table
 323     *
 324     * @param			 newDescription description for the Table
 325     */
 326    public function setDescription($newDescription)
 327    {
 328        $this->description = $newDescription;
 329    }
 330
 331    /**
 332     * Get name to use in PHP sources. It will set & return
 333     * a self-generated phpName from it's name if it's
 334     * not already set.
 335     * @return string
 336     */
 337    public function getPhpName()
 338    {
 339        if ($this->phpName === null) {
 340            $this->setPhpName();
 341        }
 342
 343        return $this->phpName;
 344    }
 345
 346    /**
 347     * Set name to use in PHP sources.
 348     *
 349     * It will generate a phpName from it's name if no
 350     * $phpName is passed.
 351     *
 352     * @param String $phpName PhpName to be set
 353     */
 354    public function setPhpName($phpName = null)
 355    {
 356        if ($phpName == null) {
 357            $this->phpName = self::generatePhpName($this->name, $this->phpNamingMethod, $this->namePrefix);
 358        } else {
 359            $this->phpName = $phpName;
 360        }
 361    }
 362
 363    /**
 364     * Get studly version of PHP name.
 365     *
 366     * The studly name is the PHP name with the first character lowercase.
 367     *
 368     * @return string
 369     */
 370    public function getStudlyPhpName()
 371    {
 372        $phpname = $this->getPhpName();
 373        if (strlen($phpname) > 1) {
 374            return strtolower(substr($phpname, 0, 1)) . substr($phpname, 1);
 375        } else { // 0 or 1 chars (I suppose that's rare)
 376
 377            return strtolower($phpname);
 378        }
 379    }
 380
 381    /**
 382     * Get the visibility of the accessors of this column / attribute
 383     * @return string
 384     */
 385    public function getAccessorVisibility()
 386    {
 387        if ($this->accessorVisibility !== null) {
 388            return $this->accessorVisibility;
 389        } else {
 390            return self::DEFAULT_VISIBILITY;
 391        }
 392    }
 393
 394    /**
 395     * Set the visibility of the accessor methods for this column / attribute
 396     * @param 	 $newVisibility string
 397     */
 398    public function setAccessorVisibility($newVisibility)
 399    {
 400        if (in_array($newVisibility, self::$valid_visibilities)) {
 401            $this->accessorVisibility = $newVisibility;
 402        } else {
 403            $this->accessorVisibility = self::DEFAULT_VISIBILITY;
 404        }
 405
 406    }
 407
 408    /**
 409     * Get the visibility of the mutator of this column / attribute
 410     * @return string
 411     */
 412    public function getMutatorVisibility()
 413    {
 414        if ($this->mutatorVisibility !== null) {
 415            return $this->mutatorVisibility;
 416        } else {
 417            return self::DEFAULT_VISIBILITY;
 418        }
 419    }
 420
 421    /**
 422     * Set the visibility of the mutator methods for this column / attribute
 423     * @param 	 $newVisibility string
 424     */
 425    public function setMutatorVisibility($newVisibility)
 426    {
 427        if (in_array($newVisibility, self::$valid_visibilities)) {
 428            $this->mutatorVisibility = $newVisibility;
 429        } else {
 430            $this->mutatorVisibility = self::DEFAULT_VISIBILITY;
 431        }
 432
 433    }
 434
 435    /**
 436     * Get the column constant name (e.g. PeerName::COLUMN_NAME).
 437     *
 438     * @return string A column constant name for insertion into PHP code
 439     */
 440    public function getConstantName()
 441    {
 442        $classname = $this->getTable()->getPhpName() . 'Peer';
 443        $const = $this->getConstantColumnName();
 444
 445        return $classname.'::'.$const;
 446    }
 447
 448    public function getConstantColumnName()
 449    {
 450        // was it overridden in schema.xml ?
 451        if ($this->getPeerName()) {
 452            return strtoupper($this->getPeerName());
 453        } else {
 454            return strtoupper($this->getName());
 455        }
 456    }
 457
 458    /**
 459     * Get the Peer constant name that will identify this column.
 460     * @return string
 461     */
 462    public function getPeerName()
 463    {
 464        return $this->peerName;
 465    }
 466
 467    /**
 468     * Set the Peer constant name that will identify this column.
 469     * @param 	 $name string
 470     */
 471    public function setPeerName($name)
 472    {
 473        $this->peerName = $name;
 474    }
 475
 476    /**
 477     * Get type to use in PHP sources.
 478     *
 479     * If no type has been specified, then uses results of getPhpNative().
 480     *
 481     * @return string The type name.
 482     * @see				 getPhpNative()
 483     */
 484    public function getPhpType()
 485    {
 486        if ($this->phpType !== null) {
 487            return $this->phpType;
 488        }
 489
 490        return $this->getPhpNative();
 491    }
 492
 493    /**
 494     * Get the location of this column within the table (one-based).
 495     * @return int value of position.
 496     */
 497    public function getPosition()
 498    {
 499        return $this->position;
 500    }
 501
 502    /**
 503     * Get the location of this column within the table (one-based).
 504     * @param int $v Value to assign to position.
 505     */
 506    public function setPosition($v)
 507    {
 508        $this->position = $v;
 509    }
 510
 511    /**
 512     * Set the parent Table of the column
 513     */
 514    public function setTable(Table $parent)
 515    {
 516        $this->parentTable = $parent;
 517    }
 518
 519    /**
 520     * Get the parent Table of the column
 521     */
 522    public function getTable()
 523    {
 524        return $this->parentTable;
 525    }
 526
 527    /**
 528     * Returns the Name of the table the column is in
 529     */
 530    public function getTableName()
 531    {
 532        return $this->parentTable->getName();
 533    }
 534
 535    /**
 536     * Adds a new inheritance definition to the inheritance list and set the
 537     * parent column of the inheritance to the current column
 538     * @param Inheritance|string $inhdata Inheritance or XML data.
 539     *
 540     * @return Inheritance
 541     */
 542    public function addInheritance($inhdata)
 543    {
 544        if ($inhdata instanceof Inheritance) {
 545            $inh = $inhdata;
 546            $inh->setColumn($this);
 547            if ($this->inheritanceList === null) {
 548                $this->inheritanceList = array();
 549                $this->isEnumeratedClasses = true;
 550            }
 551            $this->inheritanceList[] = $inh;
 552
 553            return $inh;
 554        } else {
 555            $inh = new Inheritance();
 556            $inh->loadFromXML($inhdata);
 557
 558            return $this->addInheritance($inh);
 559        }
 560    }
 561
 562    /**
 563     * Get the inheritance definitions.
 564     */
 565    public function getChildren()
 566    {
 567        return $this->inheritanceList;
 568    }
 569
 570    /**
 571     * Determine if this column is a normal property or specifies a
 572     * the classes that are represented in the table containing this column.
 573     */
 574    public function isInheritance()
 575    {
 576        return $this->isInheritance;
 577    }
 578
 579    /**
 580     * Determine if possible classes have been enumerated in the xml file.
 581     */
 582    public function isEnumeratedClasses()
 583    {
 584        return $this->isEnumeratedClasses;
 585    }
 586
 587    /**
 588     * Return the isNotNull property of the column
 589     */
 590    public function isNotNull()
 591    {
 592        return $this->isNotNull;
 593    }
 594
 595    /**
 596     * Set the isNotNull property of the column
 597     */
 598    public function setNotNull($status)
 599    {
 600        $this->isNotNull = (boolean) $status;
 601    }
 602
 603    /**
 604     * Return NOT NULL String for this column
 605     *
 606     * @return "NOT NULL" if null values are not allowed or an empty string.
 607     */
 608    public function getNotNullString()
 609    {
 610        return $this->getTable()->getDatabase()->getPlatform()->getNullString($this->isNotNull());
 611    }
 612
 613    /**
 614     * Set whether the column is the primary string,
 615     * i.e. whether its value is the default string representation of the table
 616     * @param boolean $v
 617     */
 618    public function setPrimaryString($v)
 619    {
 620        $this->isPrimaryString = (boolean) $v;
 621    }
 622
 623    /**
 624     * Return true if the column is the primary string,
 625     * i.e. if its value is the default string representation of the table
 626     */
 627    public function isPrimaryString()
 628    {
 629        return $this->isPrimaryString;
 630    }
 631
 632    /**
 633     * Set whether the column is a primary key or not.
 634     * @param boolean $v
 635     */
 636    public function setPrimaryKey($v)
 637    {
 638        $this->isPrimaryKey = (boolean) $v;
 639    }
 640
 641    /**
 642     * Return true if the column is a primary key
 643     */
 644    public function isPrimaryKey()
 645    {
 646        return $this->isPrimaryKey;
 647    }
 648
 649    /**
 650     * Set if the column is the node key of a tree
 651     */
 652    public function setNodeKey($nk)
 653    {
 654        $this->isNodeKey = (boolean) $nk;
 655    }
 656
 657    /**
 658     * Return true if the column is a node key of a tree
 659     */
 660    public function isNodeKey()
 661    {
 662        return $this->isNodeKey;
 663    }
 664
 665    /**
 666     * Set if the column is the node key of a tree
 667     */
 668    public function setNodeKeySep($sep)
 669    {
 670        $this->nodeKeySep = (string) $sep;
 671    }
 672
 673    /**
 674     * Return true if the column is a node key of a tree
 675     */
 676    public function getNodeKeySep()
 677    {
 678        return $this->nodeKeySep;
 679    }
 680
 681    /**
 682     * Set if the column is the nested set left key of a tree
 683     */
 684    public function setNestedSetLeftKey($nslk)
 685    {
 686        $this->isNestedSetLeftKey = (boolean) $nslk;
 687    }
 688
 689    /**
 690     * Return true if the column is a nested set key of a tree
 691     */
 692    public function isNestedSetLeftKey()
 693    {
 694        return $this->isNestedSetLeftKey;
 695    }
 696
 697    /**
 698     * Set if the column is the nested set right key of a tree
 699     */
 700    public function setNestedSetRightKey($nsrk)
 701    {
 702        $this->isNestedSetRightKey = (boolean) $nsrk;
 703    }
 704
 705    /**
 706     * Return true if the column is a nested set right key of a tree
 707     */
 708    public function isNestedSetRightKey()
 709    {
 710        return $this->isNestedSetRightKey;
 711    }
 712
 713    /**
 714     * Set if the column is the scope key of a tree
 715     */
 716    public function setTreeScopeKey($tsk)
 717    {
 718        $this->isTreeScopeKey = (boolean) $tsk;
 719    }
 720
 721    /**
 722     * Return true if the column is a scope key of a tree
 723     * @return boolean
 724     */
 725    public function isTreeScopeKey()
 726    {
 727        return $this->isTreeScopeKey;
 728    }
 729
 730    /**
 731     * Set true if the column is UNIQUE
 732     * @param boolean $u
 733     */
 734    public function setUnique($u)
 735    {
 736        $this->isUnique = $u;
 737    }
 738
 739    /**
 740     * Get the UNIQUE property.
 741     * @return boolean
 742     */
 743    public function isUnique()
 744    {
 745        return $this->isUnique;
 746    }
 747
 748    /**
 749     * Return true if the column requires a transaction in Postgres
 750     * @return boolean
 751     */
 752    public function requiresTransactionInPostgres()
 753    {
 754        return $this->needsTransactionInPostgres;
 755    }
 756
 757    /**
 758     * Utility method to determine if this column is a foreign key.
 759     * @return boolean
 760     */
 761    public function isForeignKey()
 762    {
 763        return (count($this->getForeignKeys()) > 0);
 764    }
 765
 766    /**
 767     * Whether this column is a part of more than one foreign key.
 768     * @return boolean
 769     */
 770    public function hasMultipleFK()
 771    {
 772        return (count($this->getForeignKeys()) > 1);
 773    }
 774
 775    /**
 776     * Get the foreign key objects for this column (if it is a foreign key or part of a foreign key)
 777     * @return array
 778     */
 779    public function getForeignKeys()
 780    {
 781        return $this->parentTable->getColumnForeignKeys($this->name);
 782    }
 783
 784    /**
 785     * Adds the foreign key from another table that refers to this column.
 786     */
 787    public function addReferrer(ForeignKey $fk)
 788    {
 789        if ($this->referrers === null) {
 790            $this->referrers = array();
 791        }
 792        $this->referrers[] = $fk;
 793    }
 794
 795    /**
 796     * Get list of references to this column.
 797     */
 798    public function getReferrers()
 799    {
 800        if ($this->referrers === null) {
 801            $this->referrers = array();
 802        }
 803
 804        return $this->referrers;
 805    }
 806
 807    public function hasReferrers()
 808    {
 809        return $this->referrers !== null;
 810    }
 811
 812    public function hasReferrer(ForeignKey $fk)
 813    {
 814        return $this->hasReferrers() && in_array($fk, $this->referrers, true);
 815    }
 816
 817    public function clearReferrers()
 818    {
 819        $this->referrers = null;
 820    }
 821
 822    public function clearInheritanceList()
 823    {
 824        $this->inheritanceList = array();
 825    }
 826
 827    /**
 828     * Sets the domain up for specified Propel type.
 829     *
 830     * Calling this method will implicitly overwrite any previously set type,
 831     * size, scale (or other domain attributes).
 832     *
 833     * @param string $propelType
 834     */
 835    public function setDomainForType($propelType)
 836    {
 837        $this->getDomain()->copy($this->getPlatform()->getDomainForType($propelType));
 838    }
 839
 840    /**
 841     * Sets the propel colunm type.
 842     * @param string $propelType
 843     * @see				 Domain::setType()
 844     */
 845    public function setType($propelType)
 846    {
 847        $this->getDomain()->setType($propelType);
 848        if ($propelType == PropelTypes::VARBINARY|| $propelType == PropelTypes::LONGVARBINARY || $propelType == PropelTypes::BLOB) {
 849            $this->needsTransactionInPostgres = true;
 850        }
 851    }
 852
 853    /**
 854     * Returns the Propel column type as a string.
 855     * @return string The constant representing Propel type: e.g. "VARCHAR".
 856     * @see				 Domain::getType()
 857     */
 858    public function getType()
 859    {
 860        return $this->getDomain()->getType();
 861    }
 862
 863    /**
 864     * Returns the column PDO type integer for this column's Propel type.
 865     * @return int The integer value representing PDO type param: e.g. PDO::PARAM_INT
 866     */
 867    public function getPDOType()
 868    {
 869        return PropelTypes::getPDOType($this->getType());
 870    }
 871
 872    /**
 873     * Returns the column type as given in the schema as an object
 874     */
 875    public function getPropelType()
 876    {
 877        return $this->getType();
 878    }
 879
 880    public function isDefaultSqlType(PropelPlatformInterface $platform = null)
 881    {
 882        if (null === $this->domain || null === $this->domain->getSqlType() || null === $platform) {
 883            return true;
 884        }
 885        $defaultSqlType = $platform->getDomainForType($this->getType())->getSqlType();
 886        if ($defaultSqlType == $this->getDomain()->getSqlType()) {
 887            return true;
 888        }
 889
 890        return false;
 891    }
 892
 893    /**
 894     * Utility method to know whether column needs Blob/Lob handling.
 895     * @return boolean
 896     */
 897    public function isLobType()
 898    {
 899        return PropelTypes::isLobType($this->getType());
 900    }
 901
 902    /**
 903     * Utility method to see if the column is text type.
 904     */
 905    public function isTextType()
 906    {
 907        return PropelTypes::isTextType($this->getType());
 908    }
 909
 910    /**
 911     * Utility method to see if the column is numeric type.
 912     * @return boolean
 913     */
 914    public function isNumericType()
 915    {
 916        return PropelTypes::isNumericType($this->getType());
 917    }
 918
 919    /**
 920     * Utility method to see if the column is boolean type.
 921     * @return boolean
 922     */
 923    public function isBooleanType()
 924    {
 925        return PropelTypes::isBooleanType($this->getType());
 926    }
 927
 928    /**
 929     * Utility method to know whether column is a temporal column.
 930     * @return boolean
 931     */
 932    public function isTemporalType()
 933    {
 934        return PropelTypes::isTemporalType($this->getType());
 935    }
 936
 937    /**
 938     * Utility method to know whether column is an array column.
 939     * @return boolean
 940     */
 941    public function isPhpArrayType()
 942    {
 943        return PropelTypes::isPhpArrayType($this->getType());
 944    }
 945
 946    /**
 947     * Utility method to know whether column is an ENUM column.
 948     * @return boolean
 949     */
 950    public function isEnumType()
 951    {
 952        return $this->getType() == PropelTypes::ENUM;
 953    }
 954
 955    /**
 956     * Sets the list of possible values for an ENUM column
 957     * @param array
 958     */
 959    public function setValueSet($valueSet)
 960    {
 961        $this->valueSet = $valueSet;
 962    }
 963
 964    /**
 965     * Returns the list of possible values for an ENUM column
 966     * @return array
 967     */
 968    public function getValueSet()
 969    {
 970        return $this->valueSet;
 971    }
 972
 973    /**
 974     * @see				 XMLElement::appendXml(DOMNode)
 975     */
 976    public function appendXml(DOMNode $node)
 977    {
 978        $doc = ($node instanceof DOMDocument) ? $node : $node->ownerDocument;
 979
 980        $colNode = $node->appendChild($doc->createElement('column'));
 981        $colNode->setAttribute('name', $this->name);
 982
 983        if ($this->phpName !== null) {
 984            $colNode->setAttribute('phpName', $this->getPhpName());
 985        }
 986
 987        $colNode->setAttribute('type', $this->getType());
 988
 989        $domain = $this->getDomain();
 990
 991        if ($domain->getSize() !== null) {
 992            $colNode->setAttribute('size', $domain->getSize());
 993        }
 994
 995        if ($domain->getScale() !== null) {
 996            $colNode->setAttribute('scale', $domain->getScale());
 997        }
 998
 999        if ($this->hasPlatform() && !$this->isDefaultSqlType($this->getPlatform())) {
1000            $colNode->setAttribute('sqlType', $domain->getSqlType());
1001        }
1002
1003        if ($this->description !== null) {
1004            $colNode->setAttribute('description', $this->description);
1005        }
1006
1007        if ($this->isPrimaryKey) {
1008            $colNode->setAttribute('primaryKey', var_export($this->isPrimaryKey, true));
1009        }
1010
1011        if ($this->isAutoIncrement) {
1012            $colNode->setAttribute('autoIncrement', var_export($this->isAutoIncrement, true));
1013        }
1014
1015        if ($this->isNotNull) {
1016            $colNode->setAttribute('required', 'true');
1017        } else {
1018            $colNode->setAttribute('required', 'false');
1019        }
1020
1021        if ($domain->getDefaultValue() !== null) {
1022            $def = $domain->getDefaultValue();
1023            if ($def->isExpression()) {
1024                $colNode->setAttribute('defaultExpr', $def->getValue());
1025            } else {
1026                $colNode->setAttribute('defaultValue', $def->getValue());
1027            }
1028        }
1029
1030        if ($this->isInheritance()) {
1031            $colNode->setAttribute('inheritance', $this->inheritanceType);
1032            foreach ($this->inheritanceList as $inheritance) {
1033                $inheritance->appendXml($colNode);
1034            }
1035        }
1036
1037        if ($this->isNodeKey()) {
1038            $colNode->setAttribute('nodeKey', 'true');
1039            if ($this->getNodeKeySep() !== null) {
1040                $colNode->setAttribute('nodeKeySep', $this->nodeKeySep);
1041            }
1042        }
1043
1044        foreach ($this->vendorInfos as $vi) {
1045            $vi->appendXml($colNode);
1046        }
1047    }
1048
1049    /**
1050     * Returns the size of the column
1051     * @return string
1052     */
1053    public function getSize()
1054    {
1055        return $this->domain ? $this->domain->getSize() : false;
1056    }
1057
1058    /**
1059     * Set the size of the column
1060     * @param string $newSize
1061     */
1062    public function setSize($newSize)
1063    {
1064        $this->domain->setSize($newSize);
1065    }
1066
1067    /**
1068     * Returns the scale of the column
1069     * @return string
1070     */
1071    public function getScale()
1072    {
1073        return $this->domain->getScale();
1074    }
1075
1076    /**
1077     * Set the scale of the column
1078     * @param string $newScale
1079     */
1080    public function setScale($newScale)
1081    {
1082        $this->domain->setScale($newScale);
1083    }
1084
1085    /**
1086     * Return the size in brackets for use in an sql
1087     * schema if the type is String.	Otherwise return an empty string
1088     */
1089    public function printSize()
1090    {
1091        return $this->domain->printSize();
1092    }
1093
1094    /**
1095     * Return a string that will give this column a default value in SQL
1096     * @deprecated use Platform::getColumnDefaultValueDDL() instead
1097     * @return string
1098     */
1099    public function getDefaultSetting()
1100    {
1101        return $this->getPlatform()->getColumnDefaultValueDDL($this);
1102    }
1103
1104    /**
1105     * Return a string that will give this column a default value in PHP
1106     * @return string
1107     */
1108    public function getDefaultValueString()
1109    {
1110        $defaultValue = $this->getDefaultValue();
1111        if ($defaultValue !== null) {
1112            if ($this->isNumericType()) {
1113                $dflt = (float) $defaultValue->getValue();
1114            } elseif ($this->isTextType() || $this->getDefaultValue()->isExpression()) {
1115                $dflt = "'" . str_replace("'", "\'", $defaultValue->getValue()) . "'";
1116            } elseif ($this->getType() == PropelTypes::BOOLEAN) {
1117                $dflt = $this->booleanValue($defaultValue->getValue()) ? 'true' : 'false';
1118            } else {
1119                $dflt = "'" . $defaultValue->getValue() . "'";
1120            }
1121        } else {
1122            $dflt = "null";
1123        }
1124
1125        return $dflt;
1126    }
1127
1128    /**
1129     * Set a string that will give this column a default value.
1130     *
1131     * @param ColumnDefaultValue|int|float|bool|string column default value
1132     *
1133     * @return Column
1134     */
1135    public function setDefaultValue($def)
1136    {
1137        if (!$def instanceof ColumnDefaultValue) {
1138            $def = new ColumnDefaultValue($def, ColumnDefaultValue::TYPE_VALUE);
1139        }
1140        $this->domain->setDefaultValue($def);
1141
1142        return $this;
1143    }
1144
1145    /**
1146     * Get the default value object for this column.
1147     * @return ColumnDefaultValue
1148     * @see				 Domain::getDefaultValue()
1149     */
1150    public function getDefaultValue()
1151    {
1152        return $this->domain->getDefaultValue();
1153    }
1154
1155    /**
1156     * Get the default value suitable for use in PHP.
1157     * @return mixed
1158     * @see				 Domain::getPhpDefaultValue()
1159     */
1160    public function getPhpDefaultValue()
1161    {
1162        return $this->domain->getPhpDefaultValue();
1163    }
1164
1165    /**
1166     * Return auto increment/sequence string for the target database. We need to
1167     * pass in the props for the target database!
1168     */
1169    public function isAutoIncrement()
1170    {
1171        return $this->isAutoIncrement;
1172    }
1173
1174    /**
1175     * Return true if the columns has to be lazy loaded, i.e. if a runtime query
1176     * on the table doesn't hydrate this column, but a getter does.
1177     */
1178    public function isLazyLoad()
1179    {
1180        return $this->isLazyLoad;
1181    }
1182
1183    /**
1184     * Gets the auto-increment string.
1185     * @return string
1186     * @throws EngineException
1187     */
1188    public function getAutoIncrementString()
1189    {
1190        if ($this->isAutoIncrement() && IDMethod::NATIVE === $this->getTable()->getIdMethod()) {
1191            return $this->getPlatform()->getAutoIncrement();
1192        } elseif ($this->isAutoIncrement()) {
1193            throw new EngineException(sprintf(
1194                'You have specified autoIncrement for column "%s", but you have not specified idMethod="native" for table "%s".',
1195                $this->name,
1196                $this->getTable()->getName()
1197            ));
1198        }
1199
1200        return '';
1201    }
1202
1203    /**
1204     * Set the auto increment value.
1205     * Use isAutoIncrement() to find out if it is set or not.
1206     */
1207    public function setAutoIncrement($value)
1208    {
1209        $this->isAutoIncrement = (boolean) $value;
1210    }
1211
1212    /**
1213     * Set the column type from a string property
1214     * (normally a string from an sql input file)
1215     *
1216     * @deprecated Do not use; this will be removed in next release.
1217     */
1218    public function setTypeFromString($typeName, $size)
1219    {
1220        $tn = strtoupper($typeName);
1221        $this->setType($tn);
1222
1223        if ($size !== null) {
1224            $this->size = $size;
1225        }
1226
1227        if (strpos($tn, "CHAR") !== false) {
1228            $this->domain->setType(PropelTypes::VARCHAR);
1229        } elseif (strpos($tn, "INT") !== false) {
1230            $this->domain->setType(PropelTypes::INTEGER);
1231        } elseif (strpos($tn, "FLOAT") !== false) {
1232            $this->domain->setType(PropelTypes::FLOAT);
1233        } elseif (strpos($tn, "DATE") !== false) {
1234            $this->domain->setType(PropelTypes::DATE);
1235        } elseif (strpos($tn, "TIME") !== false) {
1236            $this->domain->setType(PropelTypes::TIMESTAMP);
1237        } elseif (strpos($tn, "BINARY") !== false) {
1238            $this->domain->setType(PropelTypes::LONGVARBINARY);
1239        } else {
1240            $this->domain->setType(PropelTypes::VARCHAR);
1241        }
1242    }
1243
1244    /**
1245     * Return a string representation of the native PHP type which corresponds
1246     * to the propel type of this column. Use in the generation of Base objects.
1247     *
1248     * @return string PHP datatype used by propel.
1249     */
1250    public function getPhpNative()
1251    {
1252        return PropelTypes::getPhpNative($this->getType());
1253    }
1254
1255    /**
1256     * Returns true if the column's PHP native type is an boolean, int, long, float, double, string.
1257     * @return boolean
1258     * @see				 PropelTypes::isPhpPrimitiveType()
1259     */
1260    public function isPhpPrimitiveType()
1261    {
1262        return PropelTypes::isPhpPrimitiveType($this->getPhpType());
1263    }
1264
1265    /**
1266     * Return true if column's PHP native type is an boolean, int, long, float, double.
1267     * @return boolean
1268     * @see				 PropelTypes::isPhpPrimitiveNumericType()
1269     */
1270    public function isPhpPrimitiveNumericType()
1271    {
1272        return PropelTypes::isPhpPrimitiveNumericType($this->getPhpType());
1273    }
1274
1275    /**
1276     * Returns true if the column's PHP native type is a class name.
1277     * @return boolean
1278     * @see				 PropelTypes::isPhpObjectType()
1279     */
1280    public function isPhpObjectType()
1281    {
1282        return PropelTypes::isPhpObjectType($this->getPhpType());
1283    }
1284
1285    /**
1286     * Get the platform/adapter impl.
1287     *
1288     * @return Platform
1289     */
1290    public function getPlatform()
1291    {
1292        return $this->getTable()->getDatabase()->getPlatform();
1293    }
1294
1295    public function hasPlatform()
1296    {
1297        return null !== $this->getTable() && null !== $this->getTable()->getDatabase() && null !== $this->getTable()->getDatabase()->getPlatform();
1298    }
1299
1300    public function getValidator()
1301    {
1302        foreach ($this->getTable()->getValidators() as $validator) {
1303            if ($validator->getColumn() == $this) {
1304                return $validator;
1305            }
1306        }
1307    }
1308
1309    public function __clone()
1310    {
1311        $this->referrers = null;
1312    }
1313
1314    public static function generatePhpName($name, $phpNamingMethod = PhpNameGenerator::CONV_METHOD_CLEAN, $namePrefix = null)
1315    {
1316        return NameFactory::generateName(NameFactory::PHP_GENERATOR, array($name, $phpNamingMethod, $namePrefix));
1317    }
1318}