public function testConfig() { $config = $this->structure->getConfig(); $this->assertArrayHasKey('many_many', $config); $this->assertArrayHasKey('tables', $config); $this->assertCount(5, $this->structure->getTables()); }
public function registerManager($tableName, Manager $manager) { if (!$this->structure->hasTable($tableName)) { throw new \Exception("数据表 %{$tableName} 不存在"); } $this->managers[$tableName] = $manager; }
public function hasManyToManyTable($name) { $manyMany = $this->structure->getConfig()['many_many']; foreach ($manyMany as $mm) { if (in_array($this->getName(), $mm) && in_array($name, $mm)) { return true; } } return false; }
/** * Returns the test database connection. * * @return PHPUnit_Extensions_Database_DB_IDatabaseConnection */ protected function getConnection() { $pdo = null; if (!$this->conn) { $this->conn = (require __DIR__ . '/../config/config.php'); $structure = Structure::createFromYaml(__DIR__ . '/../config/structure.yml'); $this->mf = new ManagerFactory($this->conn, $structure); $pdo = $this->conn->getWrappedConnection(); $pdo->query("SET foreign_key_checks = 0"); } return $this->createDefaultDBConnection($pdo); }
/** * 获得数据库的更改后的 SQL语句 数组 * * @param string $fileName 数据结构的文件名 * @return array */ public function getDiffSql($fileName) { $structure = Structure::createFromYaml($fileName); $tables = $structure->getTables(); /** @var SchemaTable[] $schemaTables */ $schemaTables = []; $newSchema = new Schema(); $oldSchema = $this->conn->getSchemaManager()->createSchema(); foreach ($tables as $table) { $schemaTable = $newSchema->createTable($table->getName()); $schemaTable->addColumn('id', 'integer', ['autoincrement' => true]); $schemaTable->setPrimaryKey(['id']); $schemaTables[$table->getName()] = $schemaTable; } //为每个表附加字段 foreach ($tables as $table) { foreach ($table->getFields() as $fieldName => $field) { $schemaTable = $schemaTables[$table->getName()]; $schemaTable->addColumn($fieldName, $field->getRealType(), ['notnull' => $field->isRequired()]); if ($field instanceof RelationField) { $schemaTable->addForeignKeyConstraint($schemaTables[$field->getRelationTable()->getName()], array($field->getName()), ["id"], ["onUpdate" => "CASCADE", "onDelete" => "SET NULL"]); continue; } if ($field instanceof FieldAbstract) { if ($field->isUnique()) { $schemaTable->addUniqueIndex([$field->getName()]); } if ($field->isIndex()) { $schemaTable->addIndex([$field->getName()]); } continue; } } } return $sql = $oldSchema->getMigrateToSql($newSchema, $this->conn->getDatabasePlatform()); }
<?php use PhpGo\Db\Doctrine\Dbal\Structure\Structure; require_once __DIR__ . '/../vendor/autoload.php'; $conn = (require __DIR__ . '/config/config.php'); $structure = Structure::createFromYaml(__DIR__ . '/config/structure.yml'); $mf = new \PhpGo\Db\Doctrine\Dbal\Manager\ManagerFactory($conn, $structure); $gm = $mf->getManager('goods'); //$bean = $gm->createBean([ // 'category_id' => 2, // 'name' => 'apple', //]); //$bean = $gm->get(2); // //$bean->name = 'abc'; //$bean->price = 2; // //$gm->store($bean); $cm = $mf->getManager('category'); $goods = $cm->getMany(2, 'goods'); print_r($goods);