Reputation: 1846
I want to create the necessary tables and data for my symfony2 project at the first time the project is opened in the browser.
i have a loginAction which is the main entry point. in this action i want to do something like CREATE TABLE IF NOT EXISTS `blub` .. etc.
You know what i mean? Is this possible and how? I didnt found anything on the internet about.
I tried
$em = $this->getDoctrine()->getManager();
$query = $em->createQuery(
"CREATE TABLE IF NOT EXISTS `role` (
`id` int(11) NOT NULL AUTO_INCREMENT,
`name` varchar(30) COLLATE utf8_unicode_ci NOT NULL,
`role` varchar(20) COLLATE utf8_unicode_ci NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY `UNIQ_57698A6A57698A6A` (`role`)
) ENGINE=InnoDB AUTO_INCREMENT=3 DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci;
/*!40000 ALTER TABLE `role` DISABLE KEYS */;
INSERT INTO `role` (`id`, `name`, `role`) VALUES
(1, 'admin', 'ROLE_ADMIN'),
(2, 'station', 'ROLE_STATION');
/*!40000 ALTER TABLE `role` ENABLE KEYS */"
);
$products = $query->getResult();
But it says me Error: Expected SELECT, UPDATE or DELETE, got 'CREATE'. So it seams not possible to me the way i tried.
Would be glad if you can help me :)
Upvotes: 0
Views: 1830
Reputation: 1201
Having an already created entity (for example, 'AppBundle:Log'), it may be something like (quick and dirty)
$schemaManager = $this->getDoctrine()->getConnection()->getSchemaManager();
if ($schemaManager->tablesExist(array('log_1')) == true) {
echo "table exists";
} else {
$manager = $this->getDoctrine()->getManager();
$metadata = $manager->getClassMetadata('AppBundle:Log');
$metadata->setPrimaryTable(array('name' => 'log_1'));
$schemaTool = new \Doctrine\ORM\Tools\SchemaTool($manager);
$schemaTool->createSchema(array($metadata));
}
This will create the table 'log_1' if it does not exist.
Upvotes: 0
Reputation: 1333
If this kind of project is for production purposes, it's HIGHLY discouraged to use table creation / fixtures loading from a controller (or from console at anytime AFTER the project would have been deployed).
You should consider sereval things such as :
Just to explain quickly, you have some console commands in SF2 that allow you to create / alter your data model according to your entities. Hence, you're not supposed to "create" or "update" schemas yourself, but use the command designed for this, aka :
app/console doctrine:schema:update --force
The --force
option is here to force the command to alter your tables. Note that dropping columns (entities properties) will NOT save their data, that's one of the reasons why it's highly not recommanded in production environment.
Please have a look at the official SF2 documentation on the subject.
Then, loading your "base" datas is called "fixtures loading". You can have a look at its official documentation too to help yourself use it.
EDIT : As you are already aware of all this, you should then consider calling both commands from your controller (schema:update and fixtures:load).
There isn't much doc on this subject however. You can watch this example in the doc that should work from a controller as you can get the application from it and then call commands like in the documentation.
Hope this helps :)
Upvotes: 2