EntityManager不会保留

时间:2017-07-19 03:53:28

标签: symfony doctrine

我从这样的服务中检索实体存储库:

services:
    AppBundle\Repository\SecurityAwareRepository:
        abstract:  true
        calls:
            - method: setTokenStorage
              arguments:
                - "@security.token_storage"


    AppBundle\Repository\InvoiceRepository:
        parent: AppBundle\Repository\SecurityAwareRepository
        factory: ["@doctrine", getRepository]
        arguments:
            - "AppBundle:Invoice"

然后在Controller中处理表单:

public function creditCardSaveAction(Request $request)
{
    $repo = $this->get('AppBundle\Repository\InvoiceRepository');
    /** @var \AppBundle\Entity\InvoiceAddress $latestInvoice */
    $latestInvoice = $repo->getLatestInvoice();
    if ($latestInvoice){
        $form = $this->createForm(CreditCardUpdate::class);
        $form->handleRequest($request);
        if ($form->isSubmitted() && $form->isValid()) {

            $invoiceAddress = $latestInvoice->getInvoiceAddress();
            $invoiceAddress->setCreditcardToken($form->getData()['stripeToken']);
            $invoiceAddress->setPaymentProvider($this->get('payment_provider_repository')->find(1));//hard coded to Stripe
            $repo->persist($invoiceAddress);
            $repo->flush($invoiceAddress);
            return $this->redirectToRoute('site_index');
        }else{
            return $this->redirectToRoute('creditcard_update');
        }

    }
}

然而,这并不能保存' stripeToken'字段,虽然我可以看到它通过setCreditcardToken

到达实体

持久化和刷新功能的实现如下:

class SecurityAwareRepository extends \Doctrine\ORM\EntityRepository
{
...    
    public function persist($entity)
    {
        $this->_em->persist($entity);
    }


    public function flush($entity)
    {
        $this->_em->flush($entity);
    }

在实施之前,我只是尝试了

$em = $this->getDoctrine()->getManager();
$invoiceAddress->setCreditcardToken(...);
$em->persist($invoiceAddress);
$em->flush($invoiceAddress)

然而这也不起作用。

此应用程序中只有一个默认的EntityManager

表单创建:

控制器:

$form = $this->createForm(CreditCardUpdate::class);
        return $this->render('@AppBundle/invoice_address/credit_card_update.html.twig', [
            'form' => $form->createView()
        ]);

Twig:

<form action="{{  path('creditcard_save') }}" method="post" id="payment-form" class="text-center">
            {{ form_widget(form.stripeToken) }}
            {{ form_widget(form._token) }}
            <div class="form-row credit_card_background text-center">
                <div id="card-element">

                </div>

                <div id="card-errors" role="alert"></div>
            </div>

            <button class="btn btn-default ">Submit Payment</button>
        </form>



class CreditCardUpdate extends AbstractType
{
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->add( 'stripeToken', HiddenType::class );
    }
}

2 个答案:

答案 0 :(得分:0)

我最初使用

创建了InvoiceAddress实体
creditcardToken:
type: string
length: 200
nullable: true

然后将其更改为

creditcard_token:
type: string
length: 200
nullable: true

我运行bin / console doctrine:generate:entities AppBundle,但没有更新setter和getter。

答案 1 :(得分:-1)

尝试将“bind”用于表单

$form->bind($this->getRequest());

或者您的代码是

$form->bind($request);

就在这一行之前

    if ($form->isSubmitted() && $form->isValid()) {