Symfony2表单尝试在提交时连接到DB
I have a form in Symfony2 where a user is submitting info for their credit card. The form renders perfectly fine intially, but on submission, I get the error "An exception occured in driver: SQLSTATE[28000] [1045] Access denied for user 'root'@'localhost' (using password: NO)".
My code for the controller action:
<?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)
}
}
My question is why does $form->handleRequest($request) attempt to connect to the DB? From the stacktrace it looks like the Validator classes call the Doctrine ORM and it's trying to connect to a db there. I don't want it to do that and I simply want to keep the data in memory and make some API calls with it.
I have no metadata on my entity class that would suggest that Doctrine should try to save it to the db. Any ideas?
Edit:
If I delete the doctrine configuration from app/config/config.yml like below then I can access my form object perfectly fine after $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
If you creating an CreditCardType() object, it will call your entity including doctrine and connection used for checking the object. You can try to create manual the form in controller like this :
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()
;
}
If you can do this successfully, then you can try to modify your type and without creating a CreditCardType() object.
No, $form->handleRequest does not connect to DB explicitly but it does fire form events. Depending on what subforms you have on your CreditCardType, one of them could be connecting to database.