[英]Symfony3: Binding Form to Entity Classes
I'm creating a form using built-in Symfony3 services: 我正在使用内置的Symfony3服务创建表单:
1) creating new class in AppBundle\\Form
which extends AbstractType
1)在AppBundle\\Form
创建扩展AbstractType
新类
2) creating a form object in my controller (using createForm()
) 2)在我的控制器中创建一个表单对象(使用createForm()
)
3) pushing that object directly to twig layer (by createView()
) 3)将对象直接推到树枝层(通过createView()
)
In my Entities direction I've got two classes, already mapped to database by ORM. 在我的实体方向上,我有两个类,已经由ORM映射到数据库。 First one is User
, and the second one is UserAttribute
. 第一个是User
,第二个是UserAttribute
。 User
is related to UserAttribute
by OneToMany annotation. 通过OneToMany注释, User
与UserAttribute
相关。 Relationship looks like: 关系看起来像:
class UserAttr
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="User", inversedBy="userAttr" )
* @ORM\JoinColumn(nullable=false)
*/
private $user;
And from the User
side: 从User
端:
class User
{
/**
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
* @ORM\Column(type="integer")
*/
private $id;
/**
* @ORM\OneToMany(targetEntity="UserAttr", mappedBy="user")
* @ORM\JoinColumn(nullable=false)
*/
private $userAttr;
When I'm adding new fields (using $builder->add()
) everything works fine if they are associated to User
class properties. 当我添加新字段(使用$builder->add()
)时,如果它们与User
类属性相关联,则一切正常。 But if I'm doing the same with UserAttribute
properties - symfony can't find get/set methods for that properties. 但是,如果我对UserAttribute
属性执行相同的操作UserAttribute
无法找到该属性的get / set方法。 I know - I can fix it by class User extends UserAttribute
but probably that's not the point. 我知道-我可以通过class User extends UserAttribute
来修复它,但这可能不是重点。 Symfony must have another solution for that, probably I missed something. Symfony必须为此提供另一种解决方案,可能我错过了一些东西。
Thanks for your time ! 谢谢你的时间 !
// SOLVED | there should be defined an EntityClassType as below:
$builder->add('credit',EntityType::class,array(
'class' => UserAttr::class
));
You have One-To-Many
Association to UserAttr
from User
Entity. 您具有从User
实体到UserAttr
One-To-Many
关联。 Hence, A User might have multiple credit. 因此,一个用户可能有多个积分。
OPTION 1 : 选项1 :
Considering this, you have to use a collection
field type in UserFormType, which is a bit lengthy process. 考虑到这一点,您必须在UserFormType中使用一个collection
字段类型,这是一个冗长的过程。
$builder->add('userAttr', CollectionType::class, array(
'label' => false,
'allow_add' => true,
'allow_delete' => true,
'entry_type' => UserAttrType::class
));
Then create another FormType : UserAttrType
to represent UserAttr
where you can have the credit
field as a property from UserAttr
. 然后创建另一个FormType: UserAttrType
来表示UserAttr
,您可以在其中将credit
字段作为UserAttr
的属性。
$builder
->add('credit', TextType::class, array(
'label' => 'Credit',
))
This way, The form will load and submit accordingly, The credit value will also be updated when User form gets updated. 这样,表单将相应地加载并提交,当用户表单被更新时,信用额度也将被更新。 Here is a link for collection docs . 这是收藏文档的链接。 This is how to embed a collection form . 这就是嵌入收集表格的方法 。
OPTION 2 : 选项2:
But, If you want to simplify it further, add mapped = false
( doc ) to the credit
field. 但是,如果您想进一步简化它,请在credit
字段中添加mapped = false
( doc )。 This will ignore the current error. 这将忽略当前错误。 However, you have to manually collect the credit
value from Form
Object and set value to appropriate UserAttr
Object in Submit Handler. 但是,您必须手动从Form
对象收集credit
值,并将其值设置为“提交处理程序”中的相应UserAttr
对象。
Hope it helps! 希望能帮助到你!
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.