Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6546097
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:39:59+00:00 2026-05-25T11:39:59+00:00

I would like to create form based on dynamic parameters that are stored in

  • 0

I would like to create form based on dynamic parameters that are stored in DB.
So I’ve created an entity called Parameter that defines parameter name, which should be displayed as form field label.

/**
 * @ORM\Entity
 */
class Parameter
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length="255")
     * @Assert\NotBlank()
     */
    protected $name; 
    /**
     * @ORM\OneToMany(targetEntity="ParameterValue", mappedBy="parameter")
     */
    protected $values;

Parameter values for specific object (Company object) are about to be stored in ParameterValue tabel.

/**
 * @ORM\Entity
 */
class ParameterValue
{
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\ManyToOne(targetEntity="Parameter", inversedBy="values")
     * @ORM\JoinColumn(name="parameter_id", referencedColumnName="id", nullable=false)
     */
    protected $parameter;
    /**
     * @ORM\ManyToOne(targetEntity="Company", inversedBy="parameters")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id", nullable=false)
     */
    protected $company;

And of course Company entity contains parameters attribute that stores only those parameters that has been specified for Company.

/**
 * @ORM\Entity
 */
class Company
{    
    /**
     * @ORM\Id
     * @ORM\Column(type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $id;
    /**
     * @ORM\Column(type="string", length="255")
     */
    protected $name;
    /**
     * @ORM\OneToMany(targetEntity="ParameterValue", mappedBy="hotel")
     */
    protected $parameters;

How can I create Form that dynamically fetches all Parameters from DB, creates text fields with specific labels (label=Parameter->getName()) and gets ParameterValues for parameters that has already been associated with Company (for edit action)?

I have already created ‘collection’ field that gets ParameterValues but the problem is that I get form with fields for parameters that has been used for Company but no others. And of course I cannot get label because collection field type is ParameterType not Parameter.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T11:39:59+00:00Added an answer on May 25, 2026 at 11:39 am

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Using Zend_Form, how would I create form elements like this: <input type=text name=element[1] value=
I would like to create a stored procedure in MySQL that took a list
I would like to create a single .war that contains both a web based
I would like to create a comment form based on the jquery validation plugin
I would like to create a jQuery function that I can attach to form
I would like to create a form where a user can enter an arbitrary
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
I would like to create a terminal based installer/wizard. Ideally, it'd be like the
I would like to create a multi-step form for entry and editing of student

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.