I have 4 entities as Product
, ProductFeatures
, Goods
, GoodsFeaturesValue
and relations between them.
I add some Features
for Product
and then I whant create form with static fields Goods + some new Features
from Product
for this Goods
.
Values for each Goods
saved in GoodsFeaturesValue
.
如何以“匿名方式”建立这种形式?
附文
I use Collection for other Features
and this work mini, but how i can setbell from ProductFeatures
relation for each Value? 在使寺庙化时,我能够这样做,但这不好:
//GoodsFormType class
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add( name )
//other property...
->add( values , collection , array(
required => true,
type => new GoodsFeaturesValueFormType(),
allow_add => false,
allow_delete => false,
by_reference => false,
))
;
}
//GoodsFeaturesValueFormType
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add( value , text )
;
}
//controller
public function saveAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$product = $em->getRepository( ShopCoreBundle:Product )->find($id);
if (!$product)
throw $this->createNotFoundException(sprintf( Product with id %s not found , $id));
$features = $em->getRepository( ShopCoreBundle:ProductFeatures )->findByProduct($id);
$goods = new Goods();
$goods->setProduct($product);
foreach ($features as $feature) {
$entity = new GoodsFeaturesValue();
$entity->setFeatures($feature);
$entity->setGoods($goods);
$entity->setProduct($product);
$goods->addGoodsFeaturesValue($entity);
}
$request = $this->getRequest();
$form = $this->createForm(new GoodsFormType(), $goods);
$form->bindRequest($request);
if ($form->isValid()) {
$em->persist($goods);
$em->flush();
return $this->redirect($this->generateUrl( core_product_index ));
}
return array(
form => $form->createView(),
goods => $goods,
product => $product,
features => $features,
);
}