duanpang1987 2015-01-23 00:15
浏览 61
已采纳

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
  • 写回答

2条回答 默认 最新

  • dsfsdfsdfsdf6455 2015-01-23 00:41
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 MATLAB动图问题
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名