Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
395 views
in Technique[技术] by (71.8m points)

php - Symfony 2 Create a entity form field with 2 properties

I am using symfony2 and have a form to save the relation of one user to some rules. These rules are set by the admin user of the company. In this form, after I selected a user to update, I have to select which rule this user have permission.

The problem is that I may have more then one rule with the same name (it's another entity) but the values are different. So, when I build the selectbox I must show the name and the value like:

  1. Quantity of items - 10
  2. Quantity of items - 20
  3. Value of the item - 200
  4. Value of the item - 500

But now I just can show without the "- $value" using the code bellow:

$form = $this->createFormBuilder()->add('myinput', 'entity', array(
                    'class' => 'myBundle:Rule',
                    'property' => 'childEntity.name',
                    'label' => 'Filas Permitidas',
                    'expanded' => false,
                    'multiple' => true,
                    'choices' => $this->getDoctrine()
                            ->getRepository('MyBundle:Rule')
                            ->findAll(),
                    'required' => true,
                ))->getForm();

So, as property I wanted to get $myEntity->getChildEntity()->getName() and the $myEntity->getValue().

Is there some way to do this?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Yes, define a getUniqueName() method in the entity class like:

public function getUniqueName()
{
    return sprintf('%s - %s', $this->name, $this->value);
}

And edit the property form option:

'property' => 'childEntity.uniqueName',

You also can omit the property option and define the __toString() method same way in order to not repeat the setting of the property option in every form.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...