I have 3 classes : Category
, Parameter
and Product
.
Category
has a one-to-many relationship withParameters
.Product
has a one-to-many relationship withCategory
.Parameters
are attributes for a product (color, weight, size, brand, etc.).
When I select a category and create a new product I want to create a form with these parameters. How can I do this? Is it possible with symfony form framework? I hope for your help.
我试图做这样的事情:
class ProductRepository extends EntityRepository
{
public function getParameters()
{
$em = $this->getEntityManager();
$parameters = $em->getRepository( ShopProductBundle:CatParameter )->findAll();
$data = array();
foreach ($parameters as $k => $value) {
$name = $value->getId();
$data[$name] = array("label" => $value->getName());
}
return $data;
}
}
表格
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder->add( name , text , array( label => Name ));
$data = $options[ data ];
foreach($data as $k => $item){
$builder->add((string)$k, text , array( label => $item[ label ]));
}
}
public function getName()
{
return shop_productbundle_categorytype ;
}
public function getDefaultOptions(array $options){
return array( data_class => ShopProductBundleEntityProduct );
}
}
形成行动:
$parameters = $em->getRepository( ShopProductBundle:Product )->getParameters();
$form = $this->createForm(new ProductType(), $parameters);
最终是例外:
Expected argument of type "ShopProductBundleEntityProduct", "array" given