使用表单与数据库交互
问题描述:
I'm trying to make a application with Silex and I want to create a form wich will allow to add things in my database.
I added Symfony's components but I don't find documentation to create this kind of form, only connect forms. How can i do to use Symfony's components like :
$data = $form->getData();
$app['form']->persist($data);
$app['form']->flush();
return $app->redirect($app['url_generator']->generate('homepage'));
Or should I do something like that ?
$data = $form->getData();
$sql = "INSERT INTO `testdb`.`video` (`id` ,`titre`)
VALUES (NULL , $data['name']";
$app['dbs']['mysql_write']->execute($sql);
Thanx for your help.
答
I'd go for either:
- plain PDO
- Doctrine DBAL (DoctrineServiceProvider ships with silex)
- Doctrine ORM (a few providers available)
Whatever you do, for the love of god, use prepared statements.