【问题标题】:Symfony2 modal presistSymfony2 模态预置
【发布时间】:2015-07-17 08:24:04
【问题描述】:

我想以模态方式处理$doctrine->presist($modal); 以进行保存。这可能吗?

以下是示例。

由配置键和值字段组成的模态类。

AmazonsS3Setting.phpModal

<?php
namespace Eggs\BusinessPanelBundle\Form\Model;

use Eggs\CoreBundle\Entity\Business;
use Symfony\Component\Validator\Constraints as Assert;

class AmazonS3Setting
{
    private $storageAwsAccessKey;
    private $storageAwsSecretKey;
    private $storageAwsBucketName;

    private $business;

    /**
     * @param Business $business
     */
    public function __construct (Business $business)
    {
        $this->business = $business;

        $this->storageAwsAccessKey = $business->getConfigValueByKey('storageAwsAccessKey');
        $this->storageAwsSecretKey = $business->getConfigValueByKey('storageAwsSecretKey');
        $this->storageAwsBucketName = $business->getConfigValueByKey('storageAwsBucketName');
    }

    /**
     * @return mixed
     */
    public function getStorageAwsAccessKey ()
    {
        return $this->storageAwsAccessKey;
    }

    /**
     * @param mixed $storageAwsAccessKey
     */
    public function setStorageAwsAccessKey ($storageAwsAccessKey)
    {
        $this->storageAwsAccessKey = $storageAwsAccessKey;
    }

    /**
     * @return mixed
     */
    public function getStorageAwsSecretKey ()
    {
        return $this->storageAwsSecretKey;
    }

    /**
     * @param mixed $storageAwsSecretKey
     */
    public function setStorageAwsSecretKey ($storageAwsSecretKey)
    {
        $this->storageAwsSecretKey = $storageAwsSecretKey;
    }

    /**
     * @return mixed
     */
    public function getStorageAwsBucketName ()
    {
        return $this->storageAwsBucketName;
    }

    /**
     * @param mixed $storageAwsBucketName
     */
    public function setStorageAwsBucketName ($storageAwsBucketName)
    {
        $this->storageAwsBucketName = $storageAwsBucketName;
    }
}

AmazonsS3SettingType.phpForm Type

<?php

namespace Eggs\BusinessPanelBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolver;

class AmazonS3SettingType extends AbstractType
{
    /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
            ->add('storageAwsAccesskey', 'text', ['label' => 'Access Key'])
            ->add('storageAwsSecretkey', 'text', ['label' => 'Secret Key'])
            ->add('storageAwsBucketname', 'text', ['label' => 'Bucket Name'])
            //->add('baseurl', 'text', ['label' => 'Bucket Base Url', 'help' => 'your-business-account.s3.amazonaws.com'])
        ;
    }

    /**
     * @param OptionsResolver $resolver
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'Eggs\BusinessPanelBundle\Form\Model\AmazonS3Setting'
        ));
    }

    /**
     * @return string
     */
    public function getName()
    {
        return 'business_amazons3setting';
    }
}

控制器方法用于settings

/**
     * @Route("/storage", name="business_setting_storage")
     * @Template("@BusinessPanel/Setting/storageS3.html.twig")
     */
    public function storageAction(Request $request)
    {
        $business = $this->getUser();

        $storage = new AmazonS3Setting($business);

        $form = $this->createForm(New AmazonS3SettingType(), $storage)->add('Save', 'submit');

        $form->handleRequest($request);

        if ($form->isValid())
        {
            $em = $this->getDoctrine()->getManager();
            $em->persist($storage);
            $em->flush();

            $this->addFlash('success', 'Saved Successfully');
        }

        return [
            'form' => $form->createView()
        ];
    }

我想像这样保持$em-&gt;presist($storage)$em-&gt;flush(); 一样,并从模态类处理数据保存和更新。这可能吗?还是有其他方法可以做到这一点?

由于我的表是用只有键和值的字段构建的,我不想在控制器内部手动设置这些名称,我更喜欢在外部,所以我以后可以更改模式或其他类,它会在控制器方法内部自动更改。

【问题讨论】:

    标签: php symfony controller doctrine modal-dialog


    【解决方案1】:

    我认为最好的方法是创建一个Entity Repository 那么你就可以保持你的控制器干净了。

    同样处理模态类的数据保存和更新

    您想处理来自 Model 类的更新?还是我理解错了。

    【讨论】:

    • 我不确定实体存储库将如何解决键值问题。我正在尝试基于键和值构建配置,因此每个键和值将位于单独的行中,我需要按键查找并按键保存,尽可能保持代码动态,因为键和值稍后可能会更改.
    猜你喜欢
    • 1970-01-01
    • 2012-05-26
    • 1970-01-01
    • 2011-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-02-11
    • 1970-01-01
    相关资源
    最近更新 更多