示例#1
0
 public static function setUpBeforeClass()
 {
     $configDir = __DIR__ . '/../../../config';
     static::$structure = Structure::createFromYaml($configDir . '/structure.yml');
     static::$conn = (require_once $configDir . '/config.php');
     static::$mf = new ManagerFactory(static::$conn, static::$structure, true);
 }
示例#2
0
 /**
  * 获得数据库的更改后的 SQL语句 数组
  *
  * @param  string $fileName 数据结构的文件名
  * @return array
  */
 public function getDiffSql($fileName)
 {
     $structure = Structure::createFromYaml($fileName);
     $data = $structure->getStructure();
     $structureMap = Structure::getTypeMap();
     $tables = [];
     $newSchema = new Schema();
     if (is_array($data['tables'])) {
         foreach ($data['tables'] as $tblName => $tbl) {
             /** @var \Doctrine\DBAL\Schema\Table $newTable */
             $newTable = $tables[$tblName] = $newSchema->createTable($tblName);
             foreach ($tbl['fields'] as $fieldName => $field) {
                 $options = [];
                 $options['notnull'] = isset($field['required']) ? (bool) $field['required'] : false;
                 if (isset($structureMap[$field['type']])) {
                     $newTable->addColumn($fieldName, $structureMap[$field['type']], $options);
                 } else {
                     $newTable->addColumn($fieldName, $structureMap['string'], $options);
                 }
                 if (isset($field['unique']) && (bool) $field['unique']) {
                     $newTable->addUniqueIndex([$fieldName]);
                 }
                 if (isset($field['index']) && (bool) $field['index']) {
                     $newTable->addIndex([$fieldName]);
                 }
             }
             $newTable->addColumn("id", "integer", array('autoincrement' => true));
             $newTable->setPrimaryKey(array("id"));
             /** timestamp_able 创建时间,更改时间 */
             $timeAble = isset($tbl['timestamp_able']) ? $tbl['timestamp_able'] : false;
             if ($timeAble) {
                 (new Behavior\TimestampBehavior())->doIt($newTable);
             }
             /** tree_able 可进行树状存储 */
             $treeAble = isset($tbl['tree_able']) ? $tbl['tree_able'] : false;
             if ($treeAble) {
                 (new Behavior\TreeBehavior())->doIt($newTable);
             }
         }
     }
     /** 多对一 */
     if (is_array($data['tables'])) {
         foreach ($data['tables'] as $tblName => $tbl) {
             if (isset($tbl['belong_to'])) {
                 foreach ($tbl['belong_to'] as $p) {
                     $belongToTblName = is_array($p) ? key($p) : $p;
                     $this->addForeign($tables[$tblName], $tables[$belongToTblName]);
                 }
             }
         }
     }
     /** 多对多 */
     if (isset($data['many_many']) && is_array($data['many_many'])) {
         foreach ($data['many_many'] as $mm) {
             sort($mm);
             $tblName0 = is_array($mm[0]) ? key($mm[0]) : $mm[0];
             $tblName1 = is_array($mm[1]) ? key($mm[1]) : $mm[1];
             $tblName = $tblName0 . '_' . $tblName1;
             $tables[$tblName] = $newSchema->createTable($tblName);
             $this->addForeign($tables[$tblName], $tables[$tblName0]);
             $this->addForeign($tables[$tblName], $tables[$tblName1]);
         }
     }
     $oldSchema = $this->conn->getSchemaManager()->createSchema();
     return $sql = $oldSchema->getMigrateToSql($newSchema, $this->conn->getDatabasePlatform());
 }