propel-map
[ class tree: propel-map ] [ index: propel-map ] [ all elements ]

Source for file TableMap.php

Documentation is available at TableMap.php

  1. <?php
  2. /*
  3.  *  $Id: TableMap.php 563 2007-02-01 09:45:55Z heltem $
  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 please see
  19.  * <http://propel.phpdb.org>.
  20.  */
  21.  
  22. /**
  23.  * TableMap is used to model a table in a database.
  24.  *
  25.  * GENERAL NOTE
  26.  * ------------
  27.  * The propel.map classes are abstract building-block classes for modeling
  28.  * the database at runtime.  These classes are similar (a lite version) to the
  29.  * propel.engine.database.model classes, which are build-time modeling classes.
  30.  * These classes in themselves do not do any database metadata lookups, but instead
  31.  * are used by the MapBuilder classes that were generated for your datamodel. The
  32.  * MapBuilder that was created for your datamodel build a representation of your
  33.  * database by creating instances of the DatabaseMap, TableMap, ColumnMap, etc.
  34.  * classes. See propel/templates/om/php5/MapBuilder.tpl and the classes generated
  35.  * by that template for your datamodel to further understand how these are put
  36.  * together.
  37.  *
  38.  * @author     Hans Lellelid <hans@xmpl.org> (Propel)
  39.  * @author     John D. McNally <jmcnally@collab.net> (Torque)
  40.  * @author     Daniel Rall <dlr@finemaltcoding.com> (Torque)
  41.  * @version    $Revision: 563 $
  42.  * @package    propel.map
  43.  */
  44. class TableMap {
  45.  
  46.     /** The columns in the table. */
  47.     private $columns;
  48.  
  49.     /** The database this table belongs to. */
  50.     private $dbMap;
  51.  
  52.     /** The name of the table. */
  53.     private $tableName;
  54.  
  55.     /** The PHP name of the table. */
  56.     private $phpName;
  57.  
  58.     /** The prefix on the table name. */
  59.     private $prefix;
  60.  
  61.     /** The Classname for this table */
  62.     private $classname;
  63.  
  64.     /** Whether to use an id generator for pkey. */
  65.     private $useIdGenerator;
  66.  
  67.     /**
  68.      * Object to store information that is needed if the
  69.      * for generating primary keys.
  70.      */
  71.     private $pkInfo;
  72.  
  73.     /**
  74.      * Construct a new TableMap.
  75.      *
  76.      * @param      string $tableName The name of the table.
  77.      * @param      DatabaseMap $containingDB A DatabaseMap that this table belongs to.
  78.      */
  79.     public function __construct($tableNameDatabaseMap $containingDB)
  80.     {
  81.         $this->tableName $tableName;
  82.         $this->dbMap $containingDB;
  83.         $this->columns array();
  84.     }
  85.  
  86.     /**
  87.      * Normalizes the column name, removing table prefix and uppercasing.
  88.      * @param      string $name 
  89.      * @return     string Normalized column name.
  90.      */
  91.     private function normalizeColName($name{
  92.         if (false !== ($pos strpos($name'.'))) {
  93.             $name substr($name$pos 1);
  94.         }
  95.         $name strtoupper($name);
  96.         return $name;
  97.     }
  98.  
  99.     /**
  100.      * Does this table contain the specified column?
  101.      *
  102.      * @param      string $name name of the column
  103.      * @return     boolean True if the table contains the column.
  104.      */
  105.     public function containsColumn($name)
  106.     {
  107.         if (!is_string($name)) {
  108.             $name $name->getColumnName();
  109.         }
  110.         return isset($this->columns[$this->normalizeColName($name)]);
  111.     }
  112.  
  113.     /**
  114.      * Get the DatabaseMap containing this TableMap.
  115.      *
  116.      * @return     DatabaseMap A DatabaseMap.
  117.      */
  118.     public function getDatabaseMap()
  119.     {
  120.         return $this->dbMap;
  121.     }
  122.  
  123.     /**
  124.      * Get the name of the Table.
  125.      *
  126.      * @return     string A String with the name of the table.
  127.      */
  128.     public function getName()
  129.     {
  130.         return $this->tableName;
  131.     }
  132.  
  133.     /**
  134.      * Get the PHP name of the Table.
  135.      *
  136.      * @return     string A String with the name of the table.
  137.      */
  138.     public function getPhpName()
  139.     {
  140.         return $this->phpName;
  141.     }
  142.  
  143.     /**
  144.      * Set the PHP name of the Table.
  145.      *
  146.      * @param      string $phpName The PHP Name for this table
  147.      */
  148.     public function setPhpName($phpName)
  149.     {
  150.         $this->phpName $phpName;
  151.     }
  152.  
  153.     /**
  154.      * Get table prefix name.
  155.      *
  156.      * @return     string A String with the prefix.
  157.      */
  158.     public function getPrefix()
  159.     {
  160.         return $this->prefix;
  161.     }
  162.  
  163.     /**
  164.      * Set table prefix name.
  165.      *
  166.      * @param      string $prefix The prefix for the table name (ie: SCARAB for
  167.      *  SCARAB_PROJECT).
  168.      * @return     void 
  169.      */
  170.     public function setPrefix($prefix)
  171.     {
  172.         $this->prefix $prefix;
  173.     }
  174.  
  175.     /**
  176.      * Get the Classname of the Propel-Classes belonging to this table.
  177.      * @return     string 
  178.      */
  179.     public function getClassname()
  180.     {
  181.         return $this->classname;
  182.     }
  183.  
  184.     /**
  185.      * Set the Classname of the Table. Could be useful for calling
  186.      * Peer and Object methods dynamically.
  187.      * @param      string $classname The Classname
  188.      */
  189.     public function setClassname($classname)
  190.     {
  191.         $this->classname $classname;
  192.     }
  193.  
  194.     /**
  195.      * Whether to use Id generator for primary key.
  196.      * @return     boolean 
  197.      */
  198.     public function isUseIdGenerator({
  199.         return $this->useIdGenerator;
  200.     }
  201.  
  202.     /**
  203.      * Get the information used to generate a primary key
  204.      *
  205.      * @return     An Object.
  206.      */
  207.     public function getPrimaryKeyMethodInfo()
  208.     {
  209.         return $this->pkInfo;
  210.     }
  211.  
  212.     /**
  213.      * Returns array of ColumnMap objects that make up the primary key for this table.
  214.      * @return     array ColumnMap[]
  215.      */
  216.     public function getPrimaryKeyColumns()
  217.     {
  218.         $pk array();
  219.         foreach ($this->columns as $col{
  220.             if ($col->isPrimaryKey()) {
  221.                 $pk[$col;
  222.             }
  223.         }
  224.         return $pk;
  225.     }
  226.  
  227.     /**
  228.      * Get a ColumnMap[] of the columns in this table.
  229.      *
  230.      * @return     array A ColumnMap[].
  231.      */
  232.     public function getColumns()
  233.     {
  234.         return $this->columns;
  235.     }
  236.  
  237.     /**
  238.      * Get a ColumnMap for the named table.
  239.      *
  240.      * @param      string $name A String with the name of the table.
  241.      * @return     ColumnMap A ColumnMap.
  242.      * @throws     PropelException if the column is undefined
  243.      */
  244.     public function getColumn($name)
  245.     {
  246.         $name $this->normalizeColName($name);
  247.         if (!isset($this->columns[$name])) {
  248.             throw new PropelException("Cannot fetch ColumnMap for undefined column: " $name);
  249.         }
  250.         return $this->columns[$name];
  251.     }
  252.  
  253.     /**
  254.      * Add a primary key column to this Table.
  255.      *
  256.      * @param      string $columnName A String with the column name.
  257.      * @param      string $type A string specifying the Propel type.
  258.      * @param      boolean $isNotNull Whether column does not allow NULL values.
  259.      * @param      $size An int specifying the size.
  260.      * @return     ColumnMap Newly added PrimaryKey column.
  261.      */
  262.     public function addPrimaryKey($columnName$phpName$type$isNotNull false$size null)
  263.     {
  264.         return $this->addColumn($columnName$phpName$type$isNotNull$sizetruenullnull);
  265.     }
  266.  
  267.     /**
  268.      * Add a foreign key column to the table.
  269.      *
  270.      * @param      string $columnName A String with the column name.
  271.      * @param      string $type A string specifying the Propel type.
  272.      * @param      string $fkTable A String with the foreign key table name.
  273.      * @param      string $fkColumn A String with the foreign key column name.
  274.      * @param      boolean $isNotNull Whether column does not allow NULL values.
  275.      * @param      int $size An int specifying the size.
  276.      * @param      string $defaultValue The default value for this column.
  277.      * @return     ColumnMap Newly added ForeignKey column.
  278.      */
  279.     public function addForeignKey($columnName$phpName$type$fkTable$fkColumn$isNotNull false$size 0)
  280.     {
  281.         return $this->addColumn($columnName$phpName$type$isNotNull$sizefalse$fkTable$fkColumn);
  282.     }
  283.  
  284.     /**
  285.      * Add a foreign primary key column to the table.
  286.      *
  287.      * @param      string $columnName A String with the column name.
  288.      * @param      string $type A string specifying the Propel type.
  289.      * @param      string $fkTable A String with the foreign key table name.
  290.      * @param      string $fkColumn A String with the foreign key column name.
  291.      * @param      boolean $isNotNull Whether column does not allow NULL values.
  292.      * @param      int $size An int specifying the size.
  293.      * @param      string $defaultValue The default value for this column.
  294.      * @return     ColumnMap Newly created foreign pkey column.
  295.      */
  296.     public function addForeignPrimaryKey($columnName$phpName$type$fkTable$fkColumn$isNotNull false$size 0)
  297.     {
  298.         return $this->addColumn($columnName$phpName$type$isNotNull$sizetrue$fkTable$fkColumn);
  299.     }
  300.  
  301.     /**
  302.      * Add a pre-created column to this table.  It will replace any
  303.      * existing column.
  304.      *
  305.      * @param      ColumnMap $cmap A ColumnMap.
  306.      * @return     ColumnMap The added column map.
  307.      */
  308.     public function addConfiguredColumn($cmap)
  309.     {
  310.         $this->columns$cmap->getColumnName($cmap;
  311.         return $cmap;
  312.     }
  313.  
  314.     /**
  315.      * Add a column to the table.
  316.      *
  317.      * @param      string name A String with the column name.
  318.      * @param      string $type A string specifying the Propel type.
  319.      * @param      boolean $isNotNull Whether column does not allow NULL values.
  320.      * @param      int $size An int specifying the size.
  321.      * @param      boolean $pk True if column is a primary key.
  322.      * @param      string $fkTable A String with the foreign key table name.
  323.      * @param      $fkColumn A String with the foreign key column name.
  324.      * @param      string $defaultValue The default value for this column.
  325.      * @return     ColumnMap The newly created column.
  326.      */
  327.     public function addColumn($name$phpName$type$isNotNull false$size null$pk null$fkTable null$fkColumn null)
  328.     {
  329.  
  330.         $col new ColumnMap($name$this);
  331.  
  332.         if ($fkTable && $fkColumn{
  333.             if (substr($fkColumn'.'&& substr($fkColumn$fkTable!== false{
  334.                 $fkColumn substr($fkColumnstrlen($fkTable1);
  335.             }
  336.             $col->setForeignKey($fkTable$fkColumn);
  337.         }
  338.  
  339.         $col->setType($type);
  340.         $col->setPrimaryKey($pk);
  341.         $col->setSize($size);
  342.         $col->setPhpName($phpName);
  343.         $col->setNotNull($isNotNull);
  344.  
  345.         $this->columns[$name$col;
  346.  
  347.         return $this->columns[$name];
  348.     }
  349.  
  350.     /**
  351.     * Add a validator to a table's column
  352.     *
  353.     * @param      string $columnName The name of the validator's column
  354.     * @param      string $name The rule name of this validator
  355.     * @param      string $classname The dot-path name of class to use (e.g. myapp.propel.MyValidator)
  356.     * @param      string $value 
  357.     * @param      string $message The error message which is returned on invalid values
  358.     * @return     void 
  359.     */
  360.     public function addValidator($columnName$name$classname$value$message)
  361.     {
  362.         if (false !== ($pos strpos($columnName'.'))) {
  363.             $columnName substr($columnName$pos 1);
  364.         }
  365.  
  366.         $col $this->getColumn($columnName);
  367.         if ($col !== null{
  368.             $validator new ValidatorMap($col);
  369.             $validator->setName($name);
  370.             $validator->setClass($classname);
  371.             $validator->setValue($value);
  372.             $validator->setMessage($message);
  373.             $col->addValidator($validator);
  374.         }
  375.     }
  376.  
  377.     /**
  378.      * Set whether or not to use Id generator for primary key.
  379.      * @param      boolean $bit 
  380.      */
  381.     public function setUseIdGenerator($bit{
  382.         $this->useIdGenerator $bit;
  383.     }
  384.  
  385.     /**
  386.      * Sets the pk information needed to generate a key
  387.      *
  388.      * @param      $pkInfo information needed to generate a key
  389.      */
  390.     public function setPrimaryKeyMethodInfo($pkInfo)
  391.     {
  392.         $this->pkInfo $pkInfo;
  393.     }
  394.  
  395.     //---Utility methods for doing intelligent lookup of table names
  396.  
  397.     /**
  398.      * Tell me if i have PREFIX in my string.
  399.      *
  400.      * @param      data A String.
  401.      * @return     boolean True if prefix is contained in data.
  402.      */
  403.     private function hasPrefix($data)
  404.     {
  405.         return (substr($data$this->getPrefix()) !== false);
  406.     }
  407.  
  408.     /**
  409.      * Removes the PREFIX.
  410.      *
  411.      * @param      string $data A String.
  412.      * @return     string A String with data, but with prefix removed.
  413.      */
  414.     private function removePrefix($data)
  415.     {
  416.         return substr($datastrlen($this->getPrefix()));
  417.     }
  418.  
  419.  
  420.  
  421.     /**
  422.      * Removes the PREFIX, removes the underscores and makes
  423.      * first letter caps.
  424.      *
  425.      * SCARAB_FOO_BAR becomes FooBar.
  426.      *
  427.      * @param      data A String.
  428.      * @return     string A String with data processed.
  429.      */
  430.     public final function removeUnderScores($data)
  431.     {
  432.         $tmp null;
  433.         $out "";
  434.         if ($this->hasPrefix($data)) {
  435.             $tmp $this->removePrefix($data);
  436.         else {
  437.             $tmp $data;
  438.         }
  439.  
  440.         $tok strtok($tmp"_");
  441.         while ($tok{
  442.             $out .= ucfirst($tok);
  443.             $tok strtok("_");
  444.         }
  445.         return $out;
  446.     }
  447.  
  448.     /**
  449.      * Makes the first letter caps and the rest lowercase.
  450.      *
  451.      * @param      string $data A String.
  452.      * @return     string A String with data processed.
  453.      */
  454.     private function firstLetterCaps($data)
  455.     {
  456.         return(ucfirst(strtolower($data)));
  457.     }
  458. }

Documentation generated on Thu, 22 Nov 2007 03:33:55 +0000 by phpDocumentor 1.4.0