vendor/propel/propel/src/Propel/Runtime/Map/ColumnMap.php line 28

Open in your IDE?
  1. <?php
  2. /**
  3.  * MIT License. This file is part of the Propel package.
  4.  * For the full copyright and license information, please view the LICENSE
  5.  * file that was distributed with this source code.
  6.  */
  7. namespace Propel\Runtime\Map;
  8. use Propel\Generator\Model\PropelTypes;
  9. use Propel\Runtime\Adapter\AdapterInterface;
  10. use Propel\Runtime\Map\Exception\ForeignKeyNotFoundException;
  11. /**
  12.  * ColumnMap is used to model a column of a table in a database.
  13.  *
  14.  * GENERAL NOTE
  15.  * ------------
  16.  * The propel.map classes are abstract building-block classes for modeling
  17.  * the database at runtime. These classes are similar (a lite version) to the
  18.  * propel.engine.database.model classes, which are build-time modeling classes.
  19.  * These classes in themselves do not do any database metadata lookups.
  20.  *
  21.  * @author Hans Lellelid <hans@xmpl.org> (Propel)
  22.  * @author John D. McNally <jmcnally@collab.net> (Torque)
  23.  */
  24. class ColumnMap
  25. {
  26.     /**
  27.      * Propel type of the column
  28.      *
  29.      * @var string
  30.      */
  31.     protected $type;
  32.     /**
  33.      * Size of the column
  34.      *
  35.      * @var int
  36.      */
  37.     protected $size 0;
  38.     /**
  39.      * Is it a primary key?
  40.      *
  41.      * @var bool
  42.      */
  43.     protected $pk false;
  44.     /**
  45.      * Is null value allowed?
  46.      *
  47.      * @var bool
  48.      */
  49.     protected $notNull false;
  50.     /**
  51.      * The default value for this column
  52.      *
  53.      * @var string|null
  54.      */
  55.     protected $defaultValue;
  56.     /**
  57.      * Name of the table that this column is related to
  58.      *
  59.      * @var string
  60.      */
  61.     protected $relatedTableName '';
  62.     /**
  63.      * Name of the column that this column is related to
  64.      *
  65.      * @var string
  66.      */
  67.     protected $relatedColumnName '';
  68.     /**
  69.      * The TableMap for this column
  70.      *
  71.      * @var \Propel\Runtime\Map\TableMap
  72.      */
  73.     protected $table;
  74.     /**
  75.      * The name of the column
  76.      *
  77.      * @var string
  78.      */
  79.     protected $columnName;
  80.     /**
  81.      * The php name of the column
  82.      *
  83.      * @var string
  84.      */
  85.     protected $phpName;
  86.     /**
  87.      * The allowed values for an ENUM or SET column
  88.      *
  89.      * @var array
  90.      */
  91.     protected $valueSet = [];
  92.     /**
  93.      * Is this a primaryString column?
  94.      *
  95.      * @var bool
  96.      */
  97.     protected $isPkString false;
  98.     /**
  99.      * @param string $name The name of the column.
  100.      * @param \Propel\Runtime\Map\TableMap $containingTable TableMap of the table this column is in.
  101.      */
  102.     public function __construct($nameTableMap $containingTable)
  103.     {
  104.         $this->columnName $name;
  105.         $this->table $containingTable;
  106.     }
  107.     /**
  108.      * Get the name of a column.
  109.      *
  110.      * @return string A String with the column name.
  111.      */
  112.     public function getName()
  113.     {
  114.         return $this->columnName;
  115.     }
  116.     /**
  117.      * Get the table map this column belongs to.
  118.      *
  119.      * @return \Propel\Runtime\Map\TableMap
  120.      */
  121.     public function getTable()
  122.     {
  123.         return $this->table;
  124.     }
  125.     /**
  126.      * Get the name of the table this column is in.
  127.      *
  128.      * @return string A String with the table name.
  129.      */
  130.     public function getTableName()
  131.     {
  132.         return $this->table->getName();
  133.     }
  134.     /**
  135.      * Get the table name + column name.
  136.      *
  137.      * @return string A String with the full column name.
  138.      */
  139.     public function getFullyQualifiedName()
  140.     {
  141.         return $this->getTableName() . '.' $this->columnName;
  142.     }
  143.     /**
  144.      * Set the php name of this column.
  145.      *
  146.      * @param string $phpName A string representing the PHP name.
  147.      *
  148.      * @return void
  149.      */
  150.     public function setPhpName($phpName)
  151.     {
  152.         $this->phpName $phpName;
  153.     }
  154.     /**
  155.      * Get the name of a column.
  156.      *
  157.      * @return string A String with the column name.
  158.      */
  159.     public function getPhpName()
  160.     {
  161.         return $this->phpName;
  162.     }
  163.     /**
  164.      * Set the Propel type of this column.
  165.      *
  166.      * @param string $type A string representing the Propel type (e.g. PropelTypes::DATE).
  167.      *
  168.      * @return void
  169.      */
  170.     public function setType($type)
  171.     {
  172.         $this->type $type;
  173.     }
  174.     /**
  175.      * Get the Propel type of this column.
  176.      *
  177.      * @return string A string representing the Propel type (e.g. PropelTypes::DATE).
  178.      */
  179.     public function getType()
  180.     {
  181.         return $this->type;
  182.     }
  183.     /**
  184.      * Get the PDO type of this column.
  185.      *
  186.      * @return int The PDO::PARAM_* value
  187.      */
  188.     public function getPdoType()
  189.     {
  190.         return PropelTypes::getPdoType($this->type);
  191.     }
  192.     /**
  193.      * Whether this is a BLOB, LONGVARBINARY, or VARBINARY.
  194.      *
  195.      * @return bool
  196.      */
  197.     public function isLob()
  198.     {
  199.         return in_array($this->type, [
  200.             PropelTypes::BLOB,
  201.             PropelTypes::VARBINARY,
  202.             PropelTypes::LONGVARBINARY,
  203.         ]);
  204.     }
  205.     /**
  206.      * Whether this is a DATE/TIME/TIMESTAMP column.
  207.      *
  208.      * @return bool
  209.      */
  210.     public function isTemporal()
  211.     {
  212.         return in_array($this->type, [
  213.             PropelTypes::TIMESTAMP,
  214.             PropelTypes::DATE,
  215.             PropelTypes::TIME,
  216.             PropelTypes::BU_DATE,
  217.             PropelTypes::BU_TIMESTAMP,
  218.         ]);
  219.     }
  220.     /**
  221.      * Whether this column is numeric (int, decimal, bigint etc).
  222.      *
  223.      * @return bool
  224.      */
  225.     public function isNumeric()
  226.     {
  227.         return in_array($this->type, [
  228.             PropelTypes::NUMERIC,
  229.             PropelTypes::DECIMAL,
  230.             PropelTypes::TINYINT,
  231.             PropelTypes::SMALLINT,
  232.             PropelTypes::INTEGER,
  233.             PropelTypes::BIGINT,
  234.             PropelTypes::REAL,
  235.             PropelTypes::FLOAT,
  236.             PropelTypes::DOUBLE,
  237.         ]);
  238.     }
  239.     /**
  240.      * Whether this column is of type set.
  241.      *
  242.      * @return bool
  243.      */
  244.     public function isSetType()
  245.     {
  246.         return $this->type === PropelTypes::SET;
  247.     }
  248.     /**
  249.      * Whether this column is a text column (varchar, char, longvarchar).
  250.      *
  251.      * @return bool
  252.      */
  253.     public function isText()
  254.     {
  255.         return in_array($this->type, [
  256.             PropelTypes::VARCHAR,
  257.             PropelTypes::LONGVARCHAR,
  258.             PropelTypes::CHAR,
  259.         ]);
  260.     }
  261.     /**
  262.      * Set the size of this column.
  263.      *
  264.      * @param int $size An int specifying the size.
  265.      *
  266.      * @return void
  267.      */
  268.     public function setSize($size)
  269.     {
  270.         $this->size $size;
  271.     }
  272.     /**
  273.      * Get the size of this column.
  274.      *
  275.      * @return int An int specifying the size.
  276.      */
  277.     public function getSize()
  278.     {
  279.         return $this->size;
  280.     }
  281.     /**
  282.      * Set if this column is a primary key or not.
  283.      *
  284.      * @param bool $pk True if column is a primary key.
  285.      *
  286.      * @return void
  287.      */
  288.     public function setPrimaryKey($pk)
  289.     {
  290.         $this->pk = (bool)$pk;
  291.     }
  292.     /**
  293.      * Is this column a primary key?
  294.      *
  295.      * @return bool True if column is a primary key.
  296.      */
  297.     public function isPrimaryKey()
  298.     {
  299.         return $this->pk;
  300.     }
  301.     /**
  302.      * Set if this column may be null.
  303.      *
  304.      * @param bool $nn True if column may be null.
  305.      *
  306.      * @return void
  307.      */
  308.     public function setNotNull($nn)
  309.     {
  310.         $this->notNull = (bool)$nn;
  311.     }
  312.     /**
  313.      * Is null value allowed ?
  314.      *
  315.      * @return bool True if column may not be null.
  316.      */
  317.     public function isNotNull()
  318.     {
  319.         return $this->notNull || $this->isPrimaryKey();
  320.     }
  321.     /**
  322.      * Sets the default value for this column.
  323.      *
  324.      * @param string|null $defaultValue the default value for the column
  325.      *
  326.      * @return void
  327.      */
  328.     public function setDefaultValue($defaultValue)
  329.     {
  330.         $this->defaultValue $defaultValue;
  331.     }
  332.     /**
  333.      * Gets the default value for this column.
  334.      *
  335.      * @return string|null
  336.      */
  337.     public function getDefaultValue()
  338.     {
  339.         return $this->defaultValue;
  340.     }
  341.     /**
  342.      * Set the foreign key for this column.
  343.      *
  344.      * @param string $tableName The name of the table that is foreign.
  345.      * @param string $columnName The name of the column that is foreign.
  346.      *
  347.      * @return void
  348.      */
  349.     public function setForeignKey($tableName$columnName)
  350.     {
  351.         if ($tableName && $columnName) {
  352.             $this->relatedTableName $tableName;
  353.             $this->relatedColumnName $columnName;
  354.         } else {
  355.             // @TODO to remove because it seems already done by default!
  356.             $this->relatedTableName '';
  357.             $this->relatedColumnName '';
  358.         }
  359.     }
  360.     /**
  361.      * Is this column a foreign key?
  362.      *
  363.      * @return bool True if column is a foreign key.
  364.      */
  365.     public function isForeignKey()
  366.     {
  367.         return (bool)$this->relatedTableName;
  368.     }
  369.     /**
  370.      * Get the RelationMap object for this foreign key
  371.      *
  372.      * @return \Propel\Runtime\Map\RelationMap|null
  373.      */
  374.     public function getRelation()
  375.     {
  376.         if (!$this->relatedTableName) {
  377.             return null;
  378.         }
  379.         foreach ($this->getTable()->getRelations() as $relation) {
  380.             if ($relation->getType() === RelationMap::MANY_TO_ONE) {
  381.                 if (
  382.                     $relation->getForeignTable()->getName() === $this->getRelatedTableName()
  383.                     && array_key_exists($this->getFullyQualifiedName(), $relation->getColumnMappings())
  384.                 ) {
  385.                     return $relation;
  386.                 }
  387.             }
  388.         }
  389.         return null;
  390.     }
  391.     /**
  392.      * Get the table.column that this column is related to.
  393.      *
  394.      * @return string A String with the full name for the related column.
  395.      */
  396.     public function getRelatedName()
  397.     {
  398.         return $this->relatedTableName '.' $this->relatedColumnName;
  399.     }
  400.     /**
  401.      * Get the table name that this column is related to.
  402.      *
  403.      * @return string A String with the name for the related table.
  404.      */
  405.     public function getRelatedTableName()
  406.     {
  407.         return $this->relatedTableName;
  408.     }
  409.     /**
  410.      * Get the column name that this column is related to.
  411.      *
  412.      * @return string A String with the name for the related column.
  413.      */
  414.     public function getRelatedColumnName()
  415.     {
  416.         return $this->relatedColumnName;
  417.     }
  418.     /**
  419.      * Get the TableMap object that this column is related to.
  420.      *
  421.      * @throws \Propel\Runtime\Map\Exception\ForeignKeyNotFoundException when called on a column with no foreign key
  422.      *
  423.      * @return \Propel\Runtime\Map\TableMap The related TableMap object
  424.      */
  425.     public function getRelatedTable()
  426.     {
  427.         if (!$this->relatedTableName) {
  428.             throw new ForeignKeyNotFoundException(sprintf('Cannot fetch RelatedTable for column with no foreign key: %s.'$this->columnName));
  429.         }
  430.         return $this->table->getDatabaseMap()->getTable($this->relatedTableName);
  431.     }
  432.     /**
  433.      * Get the TableMap object that this column is related to.
  434.      *
  435.      * @return self The related ColumnMap object
  436.      */
  437.     public function getRelatedColumn()
  438.     {
  439.         return $this->getRelatedTable()->getColumn($this->relatedColumnName);
  440.     }
  441.     /**
  442.      * Set the valueSet of this column (only valid for ENUM and SET columns).
  443.      *
  444.      * @param array $values A list of allowed values
  445.      *
  446.      * @return void
  447.      */
  448.     public function setValueSet($values)
  449.     {
  450.         $this->valueSet $values;
  451.     }
  452.     /**
  453.      * Get the valueSet of this column (only valid for ENUM and SET columns).
  454.      *
  455.      * @return array A list of allowed values
  456.      */
  457.     public function getValueSet()
  458.     {
  459.         return $this->valueSet;
  460.     }
  461.     /**
  462.      * @param mixed $value
  463.      *
  464.      * @return bool
  465.      */
  466.     public function isInValueSet($value)
  467.     {
  468.         return in_array($value$this->valueSet);
  469.     }
  470.     /**
  471.      * @param mixed $value
  472.      *
  473.      * @return string|int|false
  474.      */
  475.     public function getValueSetKey($value)
  476.     {
  477.         return array_search($value$this->valueSet);
  478.     }
  479.     /**
  480.      * Performs DB-specific ignore case, but only if the column type necessitates it.
  481.      *
  482.      * @param string $str The expression we want to apply the ignore case formatting to (e.g. the column name).
  483.      * @param \Propel\Runtime\Adapter\AdapterInterface $db
  484.      *
  485.      * @return string
  486.      */
  487.     public function ignoreCase($strAdapterInterface $db)
  488.     {
  489.         if ($this->isText()) {
  490.             return $db->ignoreCase($str);
  491.         }
  492.         return $str;
  493.     }
  494.     /**
  495.      * Normalizes the column name, removing table prefix and uppercasing.
  496.      *
  497.      * article.first_name becomes FIRST_NAME
  498.      *
  499.      * @param string $name
  500.      *
  501.      * @return string Normalized column name.
  502.      */
  503.     public static function normalizeName($name)
  504.     {
  505.         if (($pos strrpos($name'.')) !== false) {
  506.             $name substr($name$pos 1);
  507.             $name trim($name" \t\n\r\0\x0B`'()\"[]~!-{}%^&.");
  508.         }
  509.         return strtoupper($name);
  510.     }
  511.     /**
  512.      * Set this column to be a primaryString column.
  513.      *
  514.      * @param bool $pkString
  515.      *
  516.      * @return void
  517.      */
  518.     public function setPrimaryString($pkString)
  519.     {
  520.         $this->isPkString = (bool)$pkString;
  521.     }
  522.     /**
  523.      * Is this column a primaryString column?
  524.      *
  525.      * @return bool True, if this column is the primaryString column.
  526.      */
  527.     public function isPrimaryString()
  528.     {
  529.         return $this->isPkString;
  530.     }
  531. }