Beispiel #1
0
 /**
  * Initializes the table identifier(s)/primary key(s)
  *
  */
 public function initIdentifier()
 {
     switch (count($this->_identifier)) {
         case 0:
             if (!empty($this->_options['joinedParents'])) {
                 $root = current($this->_options['joinedParents']);
                 $table = $this->_conn->getTable($root);
                 $this->_identifier = $table->getIdentifier();
                 $this->_identifierType = $table->getIdentifierType() !== Doctrine::IDENTIFIER_AUTOINC ? $table->getIdentifierType() : Doctrine::IDENTIFIER_NATURAL;
                 // add all inherited primary keys
                 foreach ((array) $this->_identifier as $id) {
                     $definition = $table->getDefinitionOf($id);
                     // inherited primary keys shouldn't contain autoinc
                     // and sequence definitions
                     unset($definition['autoincrement']);
                     unset($definition['sequence']);
                     // add the inherited primary key column
                     $fullName = $id . ' as ' . $table->getFieldName($id);
                     $this->setColumn($fullName, $definition['type'], $definition['length'], $definition, true);
                 }
             } else {
                 $definition = array('type' => 'integer', 'length' => 20, 'autoincrement' => true, 'primary' => true);
                 $this->setColumn('id', $definition['type'], $definition['length'], $definition, true);
                 $this->_identifier = 'id';
                 $this->_identifierType = Doctrine::IDENTIFIER_AUTOINC;
             }
             $this->columnCount++;
             break;
         case 1:
             foreach ($this->_identifier as $pk) {
                 $columnName = $this->getColumnName($pk);
                 $e = $this->_columns[$columnName];
                 $found = false;
                 foreach ($e as $option => $value) {
                     if ($found) {
                         break;
                     }
                     $e2 = explode(':', $option);
                     switch (strtolower($e2[0])) {
                         case 'autoincrement':
                         case 'autoinc':
                             $this->_identifierType = Doctrine::IDENTIFIER_AUTOINC;
                             $found = true;
                             break;
                         case 'seq':
                         case 'sequence':
                             $this->_identifierType = Doctrine::IDENTIFIER_SEQUENCE;
                             $found = true;
                             if ($value) {
                                 $this->_options['sequenceName'] = $value;
                             } else {
                                 if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) {
                                     $this->_options['sequenceName'] = $sequence;
                                 } else {
                                     $this->_options['sequenceName'] = $this->_conn->getSequenceName($this->_options['tableName']);
                                 }
                             }
                             break;
                     }
                 }
                 if (!isset($this->_identifierType)) {
                     $this->_identifierType = Doctrine::IDENTIFIER_NATURAL;
                 }
             }
             $this->_identifier = $pk;
             break;
         default:
             $this->_identifierType = Doctrine::IDENTIFIER_COMPOSITE;
     }
 }
Beispiel #2
0
 /**
  * the constructor
  * @throws Doctrine_Connection_Exception    if there are no opened connections
  * @throws Doctrine_Table_Exception         if there is already an instance of this table
  * @return void
  */
 public function __construct($name, Doctrine_Connection $conn)
 {
     $this->conn = $conn;
     $this->setParent($this->conn);
     $this->options['name'] = $name;
     $this->_parser = new Doctrine_Relation_Parser($this);
     if (!class_exists($name) || empty($name)) {
         throw new Doctrine_Exception("Couldn't find class " . $name);
     }
     $record = new $name($this);
     $names = array();
     $class = $name;
     // get parent classes
     do {
         if ($class == "Doctrine_Record") {
             break;
         }
         $name = $class;
         $names[] = $name;
     } while ($class = get_parent_class($class));
     // reverse names
     $names = array_reverse($names);
     // save parents
     array_pop($names);
     $this->options['parents'] = $names;
     // create database table
     if (method_exists($record, 'setTableDefinition')) {
         $record->setTableDefinition();
         // set the table definition for the given tree implementation
         if ($this->isTree()) {
             $this->getTree()->setTableDefinition();
         }
         $this->columnCount = count($this->columns);
         if (isset($this->columns)) {
             // get the declaring class of setTableDefinition method
             $method = new ReflectionMethod($this->options['name'], 'setTableDefinition');
             $class = $method->getDeclaringClass();
             $this->options['declaringClass'] = $class;
             if (!isset($this->options['tableName'])) {
                 $this->options['tableName'] = Doctrine::tableize($class->getName());
             }
             switch (count($this->primaryKeys)) {
                 case 0:
                     $this->columns = array_merge(array('id' => array('type' => 'integer', 'length' => 20, 'autoincrement' => true, 'primary' => true)), $this->columns);
                     $this->primaryKeys[] = 'id';
                     $this->identifier = 'id';
                     $this->identifierType = Doctrine::IDENTIFIER_AUTOINC;
                     $this->columnCount++;
                     break;
                 default:
                     if (count($this->primaryKeys) > 1) {
                         $this->identifier = $this->primaryKeys;
                         $this->identifierType = Doctrine::IDENTIFIER_COMPOSITE;
                     } else {
                         foreach ($this->primaryKeys as $pk) {
                             $e = $this->columns[$pk];
                             $found = false;
                             foreach ($e as $option => $value) {
                                 if ($found) {
                                     break;
                                 }
                                 $e2 = explode(':', $option);
                                 switch (strtolower($e2[0])) {
                                     case 'autoincrement':
                                     case 'autoinc':
                                         $this->identifierType = Doctrine::IDENTIFIER_AUTOINC;
                                         $found = true;
                                         break;
                                     case 'seq':
                                     case 'sequence':
                                         $this->identifierType = Doctrine::IDENTIFIER_SEQUENCE;
                                         $found = true;
                                         if ($value) {
                                             $this->options['sequenceName'] = $value;
                                         } else {
                                             if (($sequence = $this->getAttribute(Doctrine::ATTR_DEFAULT_SEQUENCE)) !== null) {
                                                 $this->options['sequenceName'] = $sequence;
                                             } else {
                                                 $this->options['sequenceName'] = $this->conn->getSequenceName($this->options['tableName']);
                                             }
                                         }
                                         break;
                                 }
                             }
                             if (!isset($this->identifierType)) {
                                 $this->identifierType = Doctrine::IDENTIFIER_NATURAL;
                             }
                             $this->identifier = $pk;
                         }
                     }
             }
         }
     } else {
         throw new Doctrine_Table_Exception("Class '{$name}' has no table definition.");
     }
     $record->setUp();
     // if tree, set up tree
     if ($this->isTree()) {
         $this->getTree()->setUp();
     }
     $this->repository = new Doctrine_Table_Repository($this);
 }