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
最满意答案
您遇到此问题的原因是,当您将它们与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
相关问答
更多-
删除ManyToMany关系(delete ManyToMany relationship)[2023-04-08]
文件 : >>> land.photo.remove(some_picture) 或者相反,使用提供的related_name参数: >>> picture.Land_Pictures.remove(some_land) 默认情况下,没有related_name ,它将是: >>> picture.land_set.remove(some_land) Documentation: >>> land.photo.remove(some_picture) Or the other way around, ... -
您遇到此问题的原因是,当您将它们与User对象关联时,您可能正在使用未关联的Group实体。 所以你需要从数据库加载它们。 或者,您可以切换到使用Guids作为实体的ID,而不是您设置的任何其他主键。 要回答你的第二个问题,你应该能够设定关系的一面。 但是,我很困惑为什么你会为这段关系设置2个单独的表。 您通常只需要另外一个表: User Group UserGroup The reason you are experiencing this is because you are likely using ...
-
这应该选择具有确切成员列表且没有其他成员的所有组。 SELECT group1 FROM Groups group1 JOIN group1.members member1 WHERE SIZE(:members) = ( SELECT COUNT(member2) FROM Groups group2 JOIN group2.members member2 WHERE group2.id = group1.id) AND SIZE(: ...
-
尝试更换 {% if thing.watchlist.user != request.user %} 通过 {% if request.user not in thing.watchlist.user.all %} 因为现在你有一个m2m关系,所以用户列表不仅仅是一个。 所以.all将返回用户列表而不是m2m对象(不确定它是一个对象,不知道如何调用它) Try replacing {% if thing.watchlist.user != request.user %} by {% if reques ...
-
NHibernate双向ManyToMany关系缓存问题(NHibernate Bi-directional ManyToMany Relationship Caching Issue)[2023-08-17]
关系的每一面都有自己的缓存; 由于第一方的更改,NHibernate不会使第二方的缓存无效。 您可以使用SessionFactory.EvictCollection手动使其无效。 Each side of the relationship has its own cache; NHibernate will not invalidate the cache of the second side because of changes in the first side. You can use Session ... -
试试related_name Django相关名称 注意ManyToManyField中的related_name kwarg class Speaker(models.Model): id_speaker = models.UUIDField(primary_key=True) name = models.TextField() class Session(models.Model): id_session = models.UUIDField(primary_key=True) ...
-
在上面的文件中找不到此问题的答案。 我只是忘了在NHibernateHelper.OnConfigure()期间将ModelMapper添加到AppPrivilegeMap 。 现在最后一个任务是找出放置.Inverse(true) ,因此序列化对象不会因为循环引用而导致异常。 The answer to this problem cannot be found in the files above. I just forgot to add the AppPrivilegeMap to the Model ...
-
对,我发现了问题所在。 我正在研究的网站已经添加了NHibernate。 为了使用代码优先/模式生成方法,我使用了一个自动化工具将现有模式转换为可以从中神奇地创建模式的类。 我使用NHhibernate设计器自动生成类,然后清理实体以符合我们的标准。 http://www.devart.com/entitydeveloper/nhibernate-designer.html 当工具为查找表生成一个类时,错误就会出现。 因此UserAddressAssignment在系统中有自己的类。 在创建多对多关系时,通 ...
-
使用JPA复合pks的一个简单示例可能是: @Entity class A { @Id Integer id; @OneToMany(mappedBy="a") List
abList; } @Entity class B { @Id Integer id; @OneToMany(mappedBy="b") List abList; } @Entity @IdClass(ABId.class) class AB { @Id @ManyToOne ... -
在您的示例中,您的目标成分列表是[1,50,60,7,3] 。 想象一下,你有一种需要成分的鸡尾酒[1, 2] 。 根据您当前的逻辑: has('ingredients', '<=', count($ids))将匹配,因为2 <= 5 ,和 whereHas('ingredients', function($q) use ($ids) { $q->whereIn('ingredients.id', $ids); })将匹配,因为子查询将返回id为1的记录,并且默认情况下, whereHas仅查找至少一条记录 ...