के साथ इकाई फॉर्म फ़ील्ड में मेरे पास एक फॉर्म परिभाषा है जो अब तक के महान क्षेत्र प्रकार entity
का उपयोग करती है। विकल्प query_builder
के साथ मैं अपने मानों का चयन करता हूं और प्रदर्शित होता है।सिम्फनी 2: रिक्त मूल्य
दुखद बात यह है कि, मुझे null
डिफ़ॉल्ट मान, जैसे all
(यह एक फ़िल्टर फ़ॉर्म है) प्रदर्शित करने की आवश्यकता है। मुझे entity
विकल्प पसंद नहीं है क्योंकि मेरे पास डेटाबेस मान हैं और FormType
डेटाबेस से क्वेरी नहीं करना चाहिए।
मेरा दृष्टिकोण अब तक एक कस्टम फ़ील्ड प्रकार को लागू करना था जो entity
बढ़ाता है और सूची के शीर्ष पर एक शून्य प्रविष्टि जोड़ता है। फ़ील्ड प्रकार लोड और उपयोग किया जाता है लेकिन दुर्भाग्य से डमी मान प्रदर्शित नहीं होता है।
क्षेत्र परिभाषा:
$builder->add('machine', 'first_null_entity', [
'label' => 'label.machine',
'class' => Machine::ident(),
'query_builder' => function (EntityRepository $repo)
{
return $repo->createQueryBuilder('m')
->where('m.mandator = :mandator')
->setParameter('mandator', $this->mandator)
->orderBy('m.name', 'ASC');
}
]);
प्रपत्र प्रकार परिभाषा:
class FirstNullEntityType extends AbstractType
{
/**
* @var unknown
*/
private $doctrine;
public function __construct(ContainerInterface $container)
{
$this->doctrine = $container->get('doctrine');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setRequired('query_builder');
$resolver->setRequired('class');
}
public function buildView(FormView $view, FormInterface $form, array $options)
{
$class = $options['class'];
$repo = $this->doctrine->getRepository($class);
$builder = $options['query_builder']($repo);
$entities = $builder->getQuery()->execute();
// add dummy entry to start of array
if($entities) {
$dummy = new \stdClass();
$dummy->__toString = function() {
return '';
};
array_unshift($entities, $dummy);
}
$options['choices'] = $entities;
}
public function getName()
{
return 'first_null_entity';
}
public function getParent()
{
return 'entity';
}
}
आप $ विकल्प [''] = 'All' का उपयोग कर सकते हैं; आपके फॉर्म प्रकार में परिभाषा –