/** * Migrationを実行します。 * */ public function execute() { $config = $this->dataSource->getDataSourceConfig(); foreach ($config as $name => $dsn) { print "** start migration for {$name}. \n"; $conn = $this->dataSource->getConnection($name); $this->pdo = $conn->getDB(); // DBのmigratoin番号を取得 $remote_num = $this->getRemoteNum(); print "remote: {$remote_num}\n"; // ローカルのmigration番号を取得 $local = new Teeple_Migration_Local(dirname(BASE_DIR) . "/migration/" . $name); $local_num = $local->getMaxNum(); print "local: {$local_num}\n"; while ($remote_num < $local_num) { $remote_num++; print "apply {$remote_num}.."; $ddl = $local->getContent($remote_num); $ddllist = explode(';', $ddl); try { foreach ($ddllist as $q) { $q = trim($q); if (strlen($q)) { $stmt = $this->pdo->prepare($q); $stmt->execute(); } } } catch (Exception $e) { print "fail.\n"; print $e->getMessage(); return; } $this->pdo->beginTransaction(); $stmt = $this->pdo->prepare("UPDATE " . self::TABLE_NAME . " SET version = {$remote_num}"); $stmt->execute(); $this->pdo->commit(); print "success.\n"; } print "** finish migration for {$name}. \n"; } // --entityがセットされていたらEntityも更新する if (isset($this->_argv) && in_array('--entity', $this->_argv)) { print "\n*** update entity class.\n"; $entityGenerator = Teeple_Container::getInstance()->getComponent('Teeple_EntityGenerator'); $entityGenerator->_argv = array('--force'); $entityGenerator->execute(); } }
/** * Entityを取得するマジックメソッドです。 * * @param string $name * @return Teeple_ActiveRecord */ public function __get($name) { $this->log->debug("エンティティ {$name} を取得します。"); if (preg_match('/^Entity_/', $name)) { $ref = new ReflectionClass($name); $ds = $ref->getStaticPropertyValue('_DATASOURCE'); if ($ds == "") { $ds = DEFAULT_DATASOURCE; } $this->log->debug("データソース: {$ds}"); if (isset($this->connection[$ds])) { $conn = $this->connection[$ds]; } else { $conn = $this->dataSource->getConnection($ds); $this->connection[$ds] = $conn; if ($this->isstart) { $conn->beginTransaction(); } } return new $name($conn->getDB()); } return NULL; }