首页 \ 问答 \ Symfony 3 Formtype子不会返回相同的块前缀(Symfony 3 Formtype child doesn't return the same block prefix)

Symfony 3 Formtype子不会返回相同的块前缀(Symfony 3 Formtype child doesn't return the same block prefix)

我需要你的帮助。 我有一个实体父母和一个实体孩子的麻烦。 第一个是添加一些新的广告,第二个只是编辑广告而不更改日期。

AdvertType:

class AdvertType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('title', TextType::class)
                ->add('description', TextareaType::class)
                ->add('date', DateTimeType::class)
                ->add('save', SubmitType::class);
        ;

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'LH\PlatformBundle\Entity\Advert'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'lh_platformbundle_advert';
    }

}

和子女AdvertEditType:

class AdvertEditType extends AbstractType
{ /** * {@inheritdoc}      */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->remove('date');
    }

    public function getParent()
    {
        return AdvertType::class;
    }

    public function getBlockPrefix()
    {
        return parent::getBlockPrefix();
    }
 }

}

函数getBlockPrefix()在html / twig视图上添加一些selectType id,在AdvertType树枝形式上带有前缀'lh_platformbundle_advert' ,但是当我使用子类型AdvertEditType创建表单时,它具有不同的blockprefix'lh_platformbundle_advert_edit'

我使用了一个jquery函数 ,它不会在编辑表单上工作,导致不同的块前缀。 是否有人知道如何保持父和子之间的相同blockPrefix,以及为什么symfony会改变它? 非常感谢您的帮助。


I need your help. I have some trouble with 1 entity parent and one entity child. The first is for add some new advert and the second is just for edit the advert without changing the date.

AdvertType:

class AdvertType extends AbstractType
{
    /**
     * {@inheritdoc}
     */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder
                ->add('title', TextType::class)
                ->add('description', TextareaType::class)
                ->add('date', DateTimeType::class)
                ->add('save', SubmitType::class);
        ;

    /**
     * {@inheritdoc}
     */
    public function configureOptions(OptionsResolver $resolver)
    {
        $resolver->setDefaults(array(
            'data_class' => 'LH\PlatformBundle\Entity\Advert'
        ));
    }

    /**
     * {@inheritdoc}
     */
    public function getBlockPrefix()
    {
        return 'lh_platformbundle_advert';
    }

}

and the child AdvertEditType:

class AdvertEditType extends AbstractType
{ /** * {@inheritdoc}      */
    public function buildForm(FormBuilderInterface $builder, array $options)
    {
        $builder->remove('date');
    }

    public function getParent()
    {
        return AdvertType::class;
    }

    public function getBlockPrefix()
    {
        return parent::getBlockPrefix();
    }
 }

}

The function getBlockPrefix() add on the html/twig view some select id whith the prefix 'lh_platformbundle_advert' on the AdvertType twig form but when i use the child AdvertEditType to create the form, it has a different blockprefix 'lh_platformbundle_advert_edit'.

I use a jquery function who doesn't working on the edit form cause to the different block Prefix. Is somebody knows how is it possible to keep the same blockPrefix between the parent and the child, and why symfony change it? Thanks a lot for any help.


原文:https://stackoverflow.com/questions/48939287
更新时间:2022-07-03 19:07

最满意答案

您遇到此问题的原因是,当您将它们与User对象关联时,您可能正在使用未关联的Group实体。 所以你需要从数据库加载它们。

或者,您可以切换到使用Guids作为实体的ID,而不是您设置的任何其他主键。

要回答你的第二个问题,你应该能够设定关系的一面。 但是,我很困惑为什么你会为这段关系设置2个单独的表。 您通常只需要另外一个表:

User
Group
UserGroup

The reason you are experiencing this is because you are likely using unassociated Group entities when you associate them with your User object. So you need to load them from the database.

Alternatively, you can switch to using the Guids as the id for the entity, instead of whatever other primary key you've set up.

To answer your second question, you should just be able to set one side of the relationship. However, I'm confused as to why you would have 2 separate tables for the relationship. You typically only need 1 other table:

User
Group
UserGroup

相关问答

更多

相关文章

更多

最新问答

更多
  • 您如何使用git diff文件,并将其应用于同一存储库的副本的本地分支?(How do you take a git diff file, and apply it to a local branch that is a copy of the same repository?)
  • 将长浮点值剪切为2个小数点并复制到字符数组(Cut Long Float Value to 2 decimal points and copy to Character Array)
  • OctoberCMS侧边栏不呈现(OctoberCMS Sidebar not rendering)
  • 页面加载后对象是否有资格进行垃圾回收?(Are objects eligible for garbage collection after the page loads?)
  • codeigniter中的语言不能按预期工作(language in codeigniter doesn' t work as expected)
  • 在计算机拍照在哪里进入
  • 使用cin.get()从c ++中的输入流中丢弃不需要的字符(Using cin.get() to discard unwanted characters from the input stream in c++)
  • No for循环将在for循环中运行。(No for loop will run inside for loop. Testing for primes)
  • 单页应用程序:页面重新加载(Single Page Application: page reload)
  • 在循环中选择具有相似模式的列名称(Selecting Column Name With Similar Pattern in a Loop)
  • System.StackOverflow错误(System.StackOverflow error)
  • KnockoutJS未在嵌套模板上应用beforeRemove和afterAdd(KnockoutJS not applying beforeRemove and afterAdd on nested templates)
  • 散列包括方法和/或嵌套属性(Hash include methods and/or nested attributes)
  • android - 如何避免使用Samsung RFS文件系统延迟/冻结?(android - how to avoid lag/freezes with Samsung RFS filesystem?)
  • TensorFlow:基于索引列表创建新张量(TensorFlow: Create a new tensor based on list of indices)
  • 企业安全培训的各项内容
  • 错误:RPC失败;(error: RPC failed; curl transfer closed with outstanding read data remaining)
  • C#类名中允许哪些字符?(What characters are allowed in C# class name?)
  • NumPy:将int64值存储在np.array中并使用dtype float64并将其转换回整数是否安全?(NumPy: Is it safe to store an int64 value in an np.array with dtype float64 and later convert it back to integer?)
  • 注销后如何隐藏导航portlet?(How to hide navigation portlet after logout?)
  • 将多个行和可变行移动到列(moving multiple and variable rows to columns)
  • 提交表单时忽略基础href,而不使用Javascript(ignore base href when submitting form, without using Javascript)
  • 对setOnInfoWindowClickListener的意图(Intent on setOnInfoWindowClickListener)
  • Angular $资源不会改变方法(Angular $resource doesn't change method)
  • 在Angular 5中不是一个函数(is not a function in Angular 5)
  • 如何配置Composite C1以将.m和桌面作为同一站点提供服务(How to configure Composite C1 to serve .m and desktop as the same site)
  • 不适用:悬停在悬停时:在元素之前[复制](Don't apply :hover when hovering on :before element [duplicate])
  • 常见的python rpc和cli接口(Common python rpc and cli interface)
  • Mysql DB单个字段匹配多个其他字段(Mysql DB single field matching to multiple other fields)
  • 产品页面上的Magento Up出售对齐问题(Magento Up sell alignment issue on the products page)