我在Symfony2中有一个表单,用户正在为其信用卡提交信息。表单呈现完全正常,但在提交时,我收到错误"An exception occured in driver: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)".
我的控制器操作代码:
<?php
/**
* Class: PaymentController
*
* This controller is used to serve an iframe that contains a form for
* a person's credit card information.
*
* @see Controller
*/
class PaymentController extends Controller
{
/**
* indexAction
*
* Display a form for a user to enter their credit card information
*
* @param Request $request
*/
public function subscribeAction(Request $request)
{
$ccForm = new CreditCard();
$form = $this->createForm(new CreditCardType(), $ccForm);
//This function attempts to connect to DB on form submission
$form->handleRequest($request);
//Never gets here
if ($form->isValid()) {
}
//render view here (this part works fine)
}
}
我的问题是为什么$form->handleRequest($request)
会尝试连接数据库?从堆栈跟踪看起来,Validator类调用Doctrine ORM,并尝试连接到db。我不希望它这样做,我只想将数据保存在内存中,并使用它进行一些API调用。
我的实体类中没有元数据表明Doctrine应该尝试将其保存到db。有什么想法吗?
编辑:
如果我从app/config/config.yml
删除学说配置,那么我可以在$form-isValid()
# Doctrine Configuration
#doctrine:
# dbal:
# driver: "%database_driver%"
# host: "%database_host%"
# port: "%database_port%"
# dbname: "%database_name%"
# user: "%database_user%"
# password: "%database_password%"
# charset: UTF8
# # if using pdo_sqlite as your database driver:
# # 1. add the path in parameters.yml
# # e.g. database_path: "%kernel.root_dir%/data/data.db3"
# # 2. Uncomment database_path in parameters.yml.dist
# # 3. Uncomment next line:
# # path: "%database_path%"
#
# orm:
# auto_generate_proxy_classes: false
# auto_mapping: false
答案 0 :(得分:1)
如果您创建了一个CreditCardType()
对象,它将调用您的实体,包括用于检查对象的doctrine和connection。您可以尝试在控制器中创建手册,如下所示:
public function subscribeAction(Request $request)
{
$form = $this->createCreditCardForm();
$form->handleRequest($request);
if ($form->isValid()) {
$entity = $form->getData();
// do what you want with the $entity
}
//render view here
}
private function createCreditCardForm()
{
return $this->createFormBuilder()->setAction($this->generateUrl('route_here_if_needed'))
->setMethod('GET')
->add('your_field', 'type')
->add('submit', 'submit')
->getForm()
;
}
如果您可以成功完成此操作,则可以尝试修改类型,而无需创建CreditCardType()
对象。
答案 1 :(得分:1)
不,$form->handleRequest
未明确连接到数据库,但 does fire form events 。根据{{1}}上的子表单,其中一个子表单可以连接到数据库。