public function testSync() { $product = new Product(); $this->assertEquals(6, count($product->getFieldsInit())); $models = [new ProductList(), new Category(), new User(), $product]; $sync = new Sync($models); $sync->delete(); foreach ($models as $model) { $this->assertFalse($sync->hasTable($model)); } // Create all tables. If table exists - skip. $sync->create(); foreach ($models as $model) { $this->assertTrue($sync->hasTable($model)); } // Remove all tables. If table does not exists - skip. $sync->delete(); foreach ($models as $model) { $this->assertFalse($sync->hasTable($model)); } }
public function actionSync($module = null) { $models = $this->getModels($module); $sync = new Sync(array_values($models)); $sync->create(); }
public function initModels(array $models) { $sync = new Sync($models); $sync->delete(); $sync->create(); }
public function actionSchemamigration($module, $model, $auto = true, $db = null) { $className = strtr("\\Modules\\{module}\\Models\\{model}", ['{module}' => ucfirst($module), '{model}' => ucfirst($model)]); if (class_exists($className) === false) { echo "Model not found in namespace: " . $className . PHP_EOL; exit(1); } $path = Alias::get('App.Modules.' . ucfirst($module) . '.Migrations'); if (!is_dir($path)) { mkdir($path); } $model = new $className(); $migration = new Migration($model, $path); $migration->setDb($db); if ($migration->hasChanges() == false) { echo "Error: " . $migration->getName() . ". No changes." . PHP_EOL; die(1); } $namespace = strtr("Modules\\{module}\\Migrations", ['{module}' => ucfirst($module)]); if ($auto) { $safeUp = $migration->getSafeUp(); $safeDown = $migration->getSafeDown(); } else { $safeUp = ''; $safeDown = ''; } if ($migration->save()) { // TODO $db $fileName = $path . DIRECTORY_SEPARATOR . $migration->getName(); $source = $this->generateTemplate($namespace, $migration->getName(), $safeUp, $safeDown); file_put_contents($fileName . '.php', $source); list(, $timestamp) = explode('_', $migration->getName()); $model = new ModelMigration(); $sync = new Sync($model); if ($sync->hasTable($model)) { $sync->create(); } echo "Migration created: " . $migration->getName() . PHP_EOL; } else { echo "Failed to save migration: " . $migration->getName() . ". No changes." . PHP_EOL; die(1); } }
* All rights reserved. * * @author Falaleev Maxim * @email max@studio107.ru * @version 1.0 * @company Studio107 * @site http://studio107.ru * @date 03/03/14.03.2014 13:12 */ if (is_dir(__DIR__ . '/../vendor')) { include __DIR__ . '/../vendor/autoload.php'; } else { require __DIR__ . '/../src.php'; } use Mindy\Orm\Model; use Mindy\Orm\Sync; use Mindy\Query\Connection; // Set connection to database Model::setConnection(new Connection(['dsn' => 'mysql:host=localhost;dbname=tmp', 'username' => 'root', 'password' => '123456', 'charset' => 'utf8'])); class MyModel extends Model { } $model = new MyModel(); // Create tables in database $sync = new Sync([$model]); $sync->create(); assert(true == $sync->hasTable($model)); // TODO https://github.com/studio107/Mindy_Orm/issues/9 // assert(0 === $model->objects()->count()); assert('0' === $model->objects()->count()); assert([] === $model->objects()->all());