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条)

报告相同问题?

悬赏问题

  • ¥20 cad图纸,chx-3六轴码垛机器人
  • ¥15 移动摄像头专网需要解vlan
  • ¥20 access多表提取相同字段数据并合并
  • ¥20 基于MSP430f5529的MPU6050驱动,求出欧拉角
  • ¥20 Java-Oj-桌布的计算
  • ¥15 powerbuilder中的datawindow数据整合到新的DataWindow
  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失