/** * (non-PHPdoc). * * @see Alpha\Model\ActiveRecordProviderInterface::makeTable() */ public function makeTable() { self::$logger->debug('>>makeTable()'); $sqlQuery = 'CREATE TABLE ' . $this->BO->getTableName() . ' (OID INT(11) ZEROFILL NOT NULL AUTO_INCREMENT,'; // get the class attributes $reflection = new ReflectionClass(get_class($this->BO)); $properties = $reflection->getProperties(); foreach ($properties as $propObj) { $propName = $propObj->name; if (!in_array($propName, $this->BO->getTransientAttributes()) && $propName != 'OID') { $propReflect = new ReflectionClass($this->BO->getPropObject($propName)); $propClass = $propReflect->getShortName(); switch (mb_strtoupper($propClass)) { case 'INTEGER': // special properties for RelationLookup OIDs if ($this->BO instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) { $sqlQuery .= "{$propName} INT(" . $this->BO->getPropObject($propName)->getSize() . ') ZEROFILL NOT NULL,'; } else { $sqlQuery .= "{$propName} INT(" . $this->BO->getPropObject($propName)->getSize() . '),'; } break; case 'DOUBLE': $sqlQuery .= "{$propName} DOUBLE(" . $this->BO->getPropObject($propName)->getSize(true) . '),'; break; case 'STRING': $sqlQuery .= "{$propName} VARCHAR(" . $this->BO->getPropObject($propName)->getSize() . ') CHARACTER SET utf8,'; break; case 'TEXT': $sqlQuery .= "{$propName} TEXT CHARACTER SET utf8,"; break; case 'BOOLEAN': $sqlQuery .= "{$propName} CHAR(1) DEFAULT '0',"; break; case 'DATE': $sqlQuery .= "{$propName} DATE,"; break; case 'TIMESTAMP': $sqlQuery .= "{$propName} DATETIME,"; break; case 'ENUM': $sqlQuery .= "{$propName} ENUM("; $enumVals = $this->BO->getPropObject($propName)->getOptions(); foreach ($enumVals as $val) { $sqlQuery .= "'" . $val . "',"; } $sqlQuery = rtrim($sqlQuery, ','); $sqlQuery .= ') CHARACTER SET utf8,'; break; case 'DENUM': $tmp = new DEnum(get_class($this->BO) . '::' . $propName); $sqlQuery .= "{$propName} INT(11) ZEROFILL,"; break; case 'RELATION': $sqlQuery .= "{$propName} INT(11) ZEROFILL UNSIGNED,"; break; default: $sqlQuery .= ''; break; } } } if ($this->BO->isTableOverloaded()) { $sqlQuery .= 'classname VARCHAR(100),'; } $sqlQuery .= 'PRIMARY KEY (OID)) ENGINE=InnoDB DEFAULT CHARACTER SET utf8 COLLATE utf8_general_ci;'; $this->BO->setLastQuery($sqlQuery); if (!($result = self::getConnection()->query($sqlQuery))) { throw new AlphaException('Failed to create the table [' . $this->BO->getTableName() . '] for the class [' . get_class($this->BO) . '], database error is [' . self::getConnection()->error . ']'); self::$logger->debug('<<makeTable'); } // check the table indexes if any additional ones required $this->checkIndexes(); if ($this->BO->getMaintainHistory()) { $this->BO->makeHistoryTable(); } self::$logger->debug('<<makeTable'); }
/** * (non-PHPdoc). * * @see Alpha\Model\ActiveRecordProviderInterface::makeTable() */ public function makeTable() { self::$logger->debug('>>makeTable()'); $sqlQuery = 'CREATE TABLE ' . $this->BO->getTableName() . ' (OID INTEGER PRIMARY KEY,'; // get the class attributes $reflection = new ReflectionClass(get_class($this->BO)); $properties = $reflection->getProperties(); $foreignKeys = array(); foreach ($properties as $propObj) { $propName = $propObj->name; if (!in_array($propName, $this->BO->getTransientAttributes()) && $propName != 'OID') { $propReflect = new ReflectionClass($this->BO->getPropObject($propName)); $propClass = $propReflect->getShortName(); switch (mb_strtoupper($propClass)) { case 'INTEGER': // special properties for RelationLookup OIDs if ($this->BO instanceof RelationLookup && ($propName == 'leftID' || $propName == 'rightID')) { $sqlQuery .= "{$propName} INTEGER(" . $this->BO->getPropObject($propName)->getSize() . ') NOT NULL,'; } else { $sqlQuery .= "{$propName} INTEGER(" . $this->BO->getPropObject($propName)->getSize() . '),'; } break; case 'DOUBLE': $sqlQuery .= "{$propName} REAL(" . $this->BO->getPropObject($propName)->getSize(true) . '),'; break; case 'STRING': $sqlQuery .= "{$propName} TEXT(" . $this->BO->getPropObject($propName)->getSize() . '),'; break; case 'TEXT': $sqlQuery .= "{$propName} TEXT,"; break; case 'BOOLEAN': $sqlQuery .= "{$propName} INTEGER(1) DEFAULT '0',"; break; case 'DATE': $sqlQuery .= "{$propName} TEXT,"; break; case 'TIMESTAMP': $sqlQuery .= "{$propName} TEXT,"; break; case 'ENUM': $sqlQuery .= "{$propName} TEXT,"; break; case 'DENUM': $tmp = new DEnum(get_class($this->BO) . '::' . $propName); $sqlQuery .= "{$propName} INTEGER(11),"; break; case 'RELATION': $sqlQuery .= "{$propName} INTEGER(11),"; $rel = $this->BO->getPropObject($propName); $relatedField = $rel->getRelatedClassField(); $relatedClass = $rel->getRelatedClass(); $relatedBO = new $relatedClass(); $tableName = $relatedBO->getTableName(); $foreignKeys[$propName] = array($tableName, $relatedField); break; default: $sqlQuery .= ''; break; } } } if ($this->BO->isTableOverloaded()) { $sqlQuery .= 'classname TEXT(100)'; } else { $sqlQuery = mb_substr($sqlQuery, 0, -1); } if (count($foreignKeys) > 0) { foreach ($foreignKeys as $field => $related) { $sqlQuery .= ', FOREIGN KEY (' . $field . ') REFERENCES ' . $related[0] . '(' . $related[1] . ')'; } } if (count($this->foreignKeys) > 0) { foreach ($this->foreignKeys as $field => $related) { $sqlQuery .= ', FOREIGN KEY (' . $field . ') REFERENCES ' . $related[0] . '(' . $related[1] . ')'; } } $sqlQuery .= ');'; $this->BO->setLastQuery($sqlQuery); if (!self::getConnection()->exec($sqlQuery)) { throw new AlphaException('Failed to create the table [' . $this->BO->getTableName() . '] for the class [' . get_class($this->BO) . '], database error is [' . self::getLastDatabaseError() . ']'); self::$logger->debug('<<makeTable'); } // check the table indexes if any additional ones required $this->checkIndexes(); if ($this->BO->getMaintainHistory()) { $this->BO->makeHistoryTable(); } self::$logger->debug('<<makeTable'); }