Symfony2嵌入式表单(Symfony2 Embedded Forms)
我试图理解symfony2中的嵌入表单。
我有两个实体
icon
和asset
它们之间存在多对多的关系,以及$icon->imageLarge
和icon->imageThumb
$icon->imageLarge
上的OneToOne
。注意:(资产是类名,但在图标内引用为
$image...
)我最终想要的是一个与图标相关的图像库和图标上的两列,以反映哪个图像是主缩略图和主要大图像......
我创建了一个IconType:
class IconType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options){ $builder->add('name'); $builder->add('imageThumb', new AssetType()); $builder->add('imageLarge', new AssetType()); } public function getName(){ return 'icon'; } public function setDefaultOptions(OptionsResolverInterface $resolver){ $resolver->setDefaults(array( 'data_class' => 'SixString\PearBundle\Entity\Icon', )); } }
和一个AssetType:
class AssetType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options){ $builder->add('file'); } public function getName(){ return 'asset'; } public function setDefaultOptions(OptionsResolverInterface $resolver){ $resolver->setDefaults(array( 'data_class' => 'SixString\PearBundle\Entity\Asset', )); } }
我在我的控制器中创建我的表单:
$icon = new Icon; $form = $this->createForm(new IconType(), $icon); if($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { .... } } return $this->render('SixStringPearBundle:Icon:form.html.twig', array("form" => $form->createView()));
到目前为止,所有内容似乎都在进行交互,我在模板中创建了我的表单,它显示了图标名称字段以及两个资产文件上传者。
我迷路的地方是如何访问上传的
assets
。 我需要将这两个assets
持久保存到db,然后设置$icon->addImage($asset1); $icon->addImage($asset2);
$icon->addImage($asset1); $icon->addImage($asset2);
对于多对多和$icon->setImageThumb($asset1); $icon->setImageLarge($asset2);
$icon->setImageThumb($asset1); $icon->setImageLarge($asset2);
我不确定这一切是否有意义,但任何帮助搞清楚逻辑或只是如何访问这些资产将不胜感激!
I am trying to understand the embeded forms in symfony2.
I have two entities
icon
andasset
There is a many to many relationship between them as well as aOneToOne
on$icon->imageLarge
andicon->imageThumb
.note: (Asset is the class name but referenced as
$image...
inside the icon)What I am trying to end up with is a gallery of images related to an icon and two columns on the icon to reflect which image is the main thumbnail and the main large image...
I have created an IconType:
class IconType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options){ $builder->add('name'); $builder->add('imageThumb', new AssetType()); $builder->add('imageLarge', new AssetType()); } public function getName(){ return 'icon'; } public function setDefaultOptions(OptionsResolverInterface $resolver){ $resolver->setDefaults(array( 'data_class' => 'SixString\PearBundle\Entity\Icon', )); } }
and an AssetType:
class AssetType extends AbstractType{ public function buildForm(FormBuilderInterface $builder, array $options){ $builder->add('file'); } public function getName(){ return 'asset'; } public function setDefaultOptions(OptionsResolverInterface $resolver){ $resolver->setDefaults(array( 'data_class' => 'SixString\PearBundle\Entity\Asset', )); } }
I create my form in my controller:
$icon = new Icon; $form = $this->createForm(new IconType(), $icon); if($request->isMethod('POST')) { $form->bind($request); if ($form->isValid()) { .... } } return $this->render('SixStringPearBundle:Icon:form.html.twig', array("form" => $form->createView()));
Everything appears to be interacting so far, I have created my form in my template and it shows the icon name field as well as the two asset file uploaders.
Where I am getting lost is how to access the uploaded
assets
. I need to persist these twoassets
to db and then set$icon->addImage($asset1); $icon->addImage($asset2);
for the many to many and also$icon->setImageThumb($asset1); $icon->setImageLarge($asset2);
I'm not sure if this all makes sense, but any help figuring out the logic or simply how to access these assets would be appreciated!
原文:https://stackoverflow.com/questions/16978991