[英]Symfony2 form data_class factory for creating the data object
Is it possible to use kind of a factory method/class for the forms data object, instead of using the data_class
option. 是否有可能对表单数据对象使用某种工厂方法/类,而不是使用
data_class
选项。 I need this for embedded forms, where I do not control the data object anymore in my client code. 对于嵌入式表单,我不需要它,而在我的客户端代码中不再控制数据对象。 Since can't use the simple empty object created by the PHP
new
operator, I need some kind of factory creation mechanism. 由于无法使用PHP
new
运算符创建的简单空对象,因此需要某种工厂创建机制。
Something like this (pseudo): 像这样的东西(伪):
// ... some form type
$resolver->setDefaults(array(
'data_class' => '\MyModel', // this the simple way, which is not enough for me.
'data_factory' => \MyFactory::create('\MyModel') // or something like that.
));
I hope my problem is clear, is something like this possible? 我希望我的问题很清楚,这可能吗?
Thanks! 谢谢!
What I was looking for was this (same idea as I described in my question): http://symfony.com/doc/current/cookbook/form/use_empty_data.html 我正在寻找的是这个(与我在问题中描述的想法相同): http : //symfony.com/doc/current/cookbook/form/use_empty_data.html
So, the above code of me would look like this: 因此,我上面的代码如下所示:
// ... some form type
$resolver->setDefaults(array(
'data_class' => '\MyModel',
'empty_data' => \MyFactory::create('\MyModel'), // now it will create the object through my factory
));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.