I need some functionality for dynamic creation table in DB.
Algorithm is:
1) register new user -> make record in DB (this is simple)
2) after registration create table like user_data_abcdef123456...
where abcdef123456
- is random pregenerated hash. this table must created from (for example) existing default entity (user_data_)
Entity:
Path\To\Entity\UserData:
type: entity
table: user_data_
....
in Controller do like this:
$doctrine = $this->getDoctrine();
/** @var AbstractSchemaManager $manager */
$manager = $doctrine->getConnection()->getSchemaManager();
/** @var ClassMetadata $metadata */
$metadata = $doctrine->getManager()->getClassMetadata($class);
$metadata->setPrimaryTable(array('name' => $metadata->getTableName() . $project));
$table = new Table($metadata->getTableName());
$manager->createTable($table);
and I need set all column, all index.. etc.
Is there any correct solution?
If you are using Repository (and you should use them) clean way of creating table:
Hooray! I found solution! Thanks for trevorengstrom in Doctrine automatically create all the database tables answer. The solution is:
where
$class = 'DemoBundle:UserData'
and$project = 'abcdef123456...';