Symfony2 - how to create form based on dynamic parameters from db? (EAV)

那年仲夏 提交于 2019-11-29 20:31:38

I recently had to do something similar. I will share my experience and try an incorporate your point of view. You may have to modify this some to fit your need.

I had my Company entity similar to yours. I wanted each Company stored to have a dynamic amount of parameters and values associated with them and able to edit these in forms using the symfony2 form framework.

I first created an entity called CompanyParameter. Add namespace and save somewhere in your bundle.

    class CompanyParameter {    
        protected $data;    
        public function __construct($parameters,$edit=false)
        {
               if ($edit==false) {
                foreach ($parameters as $k => $value) {
                        $name = $value->getId();
                        $this->data[$name] = array("label"=>$value->getName(),"value"=>"");
                        $this->{$name} = "";                    
                }
                } else {
                foreach ($parameters as $k => $value) {
                        $name = $value->getParameterid();
                        $pvalue = $value->getValue();
                        $this->data[$name] = array("label"=>$value->getName(),"value"=>$pvalue);
                        $this->{$name} = $pvalue;                    
                }
}
        }    
        public function get() { return $this->data; }    
    }

Create a new variable with the class and send your parameters to it.

$parameters = $em->getRepository("YourBundle:Parameter")->findAll();
$companyparameter = new CompanyParameter($parameters);

You now have an entity with all the dynamic parameters you want to manage. (If you want to load the CompanyParameter with already stored values for edit purpose, just send CompanyParameter an array of ParameterValue entities instead and set $edit=true in constructor.

Create a CompanyParameterFormType like described at http://symfony.com/doc/current/book/forms.html#creating-form-classes Make sure data_class points to CompanyParameter

Create a new form in your controller:

$form = $this->createForm(new CompanyParameterTypeBundle(), $companyparameter);

Inside the CompanyParameterFormType:

    public function buildForm(FormBuilder $builder, array $options)
    {
        $data = $options["data"]->get();      
        foreach ($data as $k => $value) {
             $builder->add($k,"text",array("label"=>$value["label"]));
        }           
    }

If we would $form->createView() we would now have a form with the two fields "Company name" and "CEO".

Fill in the form and send the submit of the form back to the same action.

In the action check for post and $form->bindRequest($this->getRequest()).

CompanyParameter now contains all the values for the parameters belonging to current company. To persist this data:

$data = $companyparameter->get();
$counter = 0;
foreach($data as $k => $value) {
$parameter = new ParameterValue();
$parameter->setCompany($company);
$parameter->setParameter($parameters[$counter]);
$parameter->setValue($value["value"]);
$em->persist($parameter);
$counter++;
}

If You are instead editing the parameters

$data = $companyparameter->get();
foreach ($parameters as $k => $value) {
$value->setValue($data[$value->getParameterid()]["value"]);
$em->persist($value);
}

Hope this helps some. My first time posting and I'm not that used to "explaining" code yet so keep in mind please.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!