2016-06-17 7 views
0

異なる名前の同じテーブルをたくさん生成したいと思います。 (http://symfony.com/doc/current/book/doctrine.html#add-mapping-information)のようなマッピングで1つのエンティティクラスを作成し、パラメータ名を持つコマンドを実行してデータベースにテーブルを作成することは可能ですか?私はすでに1つの解決策を実行しましたが、エンティティクラスに基づいていません。コンストラクタでDoctrine2:エンティティクラスに基づいてエンティティを生成する

:作成機能以外

public function __construct() 
{ 
    $this->schema = new \Doctrine\DBAL\Schema\Schema(); 
    $this->config = new \Doctrine\DBAL\Configuration(); 
    $this->connection = DriverManager::getConnection($this->connectionParams, $this->config); 
    $this->queryBuilder = $this->connection->createQueryBuilder(); 
    $this->myPlatform = new MySqlPlatform(); 
    $this->comparator = new \Doctrine\DBAL\Schema\Comparator(); 
} 

public function setTable($name) 
{ 
$myTable = $this->schema->createTable($name); 
$myTable->addColumn("id", "integer", array("unsigned" => true)); 
$myTable->addColumn("name", "string", array("length" => 32)); 
$myTable->setPrimaryKey(array("id")); 
$myTable->addUniqueIndex(array("name")); 

$queries = $this->schema->toSql($this->myPlatform); 
$sm = $this->connection->getSchemaManager(); 
$fromSchema = $sm->createSchema(); 
$toSchema = $this->schema; 

$schemaDiff = $this->comparator->compare($fromSchema, $toSchema); 
$queries = $schemaDiff->toSql($this->myPlatform); 
$sql = $schemaDiff->toSaveSql($this->myPlatform); 
foreach ($sql as $sentence) { 
     $this->connection->exec($sentence); 
    } 
} 

と同じテーブルを作成するためのパラメータ$ table_nameを持つマッピングと1クラスに基づいて他のソリューションとコマンドがあります別の名前ですか? symfony 3とDoctrine 2を使用しています。

+0

誰ですか?何かご意見は? – sanof

答えて

0

エンティティクラスは、テーブルに接続して対応させるべきオブジェクトです。より良い方法は、@ORM \ Table()アノテーションを使用せずに、あなたのEntityAbstractClassと同じように見えるEntityAbstractClassを作成し、EntityAbstractClassを拡張する複数のエンティティを作成することです。

抽象クラス:

abstract class EntityAbstractClass() { 
    // your fields here 
} 

/** 
* @ORM\Table(name="first_table") 
**/ 
class FirstEntityClass extends EntityAbstractClass { 

} 

/** 
* @ORM\Table(name="second_table") 
**/ 
class SecondEntityClass extends EntityAbstractClass { 

} 

のように。短いファイルを保存してプログラムで管理してから、スキーマ更新を実行することもできます。

代わり代わりに抽象クラスのおトレイトを使用することができます。

trait MyEntityTrait { 
    // your fields here like: 
    /** @ORM\Column() **/ 
    private $name; 
} 

とすべての新しいエンティティクラスで:

/** 
* @ORM\Table(name="first_table") 
**/ 
class FirstEntityClass { 
    use MyEntityTrait; 
} 

...

+0

良い答え、私はそれについても考えていましたが、問題はあなたが事前にエンティティの数を知らないということです。 – sanof

+0

必要なものに応じて、いくつかのスクリプトを使用してエンティティファイルを生成することができます。 –

関連する問題