2013-08-28 6 views
8

मैं सोनाटा व्यवस्थापक और मैं उपयोग कर रहा हूँ की सूची श्रेणियों की एक क्षेत्र है और मैं चयन में एक पेड़ की तरह है ताकि उन्हें दिखाने की जरूरत है:कस्टम विकल्प सोनाटा व्यवस्थापक के साथ sonata_type_model क्षेत्र

<select> 
    <option>Category father-1</option> 
    <option>--Category child-1-1</option> 
    <option>--Category child-1-2</option> 
    <option>--Category child-1-3</option> 
    <option>----Category child-1-3-1</option> 
    <option>----Category child-1-3-2</option> 
    <option>--Category child-1-4</option> 
    <option>--...</option> 
    <option>Category father-2</option> 
</select> 

यह संभव है? मैं इसे 'choice_list' में भी शामिल की कोशिश की है एक सरणी getTreeCatsArray विधि में उत्पन्न:

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $tree_cat_array = $this->em->getRepository('MyBundle:Category')->getTreeCatsArray(); 

    $formMapper 
     ->add('category', 'sonata_type_model', array(
       'empty_value' => '', 
       'choice_list' => $tree_cat_array)); 
} 

यह त्रुटि दिखाता है:

The option "choice_list" with value "Array" is expected to be of type "null", "Symfony\Component\Form\Extension\Core\ChoiceList\ChoiceListInterface" 

मुझे यकीन है कि अगर मैं क्षेत्र प्रकार 'sonata_type_model' या 'का उपयोग करना चाहिए नहीं कर रहा हूँ विकल्प '

उत्तर

7

प्रयास करें:

->add('category', 'entity', array(
     'class' => 'Acme\Entity\Category', 
) 

यह केवल यदि आप इकाईहै काम करेंगे।

के लिए Category इकाई के लिए एक पेड़ संपादक बनाने SonataAdminBundle के बारे में this article देखें। Here रूसी में एक ही लेख है, लेकिन पहले संस्करण में गायब कोड है।

25

ठीक है, मैं संबंधित इकाई में शामिल करने श्रेणियों पेड़ में आदेश दिया की सूची मिल गया है इस प्रकार है:

protected function configureFormFields(FormMapper $formMapper) 
{ 
    $em = $this->modelManager->getEntityManager('MyBundle\Entity\Category'); 

    $query = $em->createQueryBuilder('c') 
       ->select('c') 
       ->from('MyBundle:Category', 'c') 
       ->where('c.parent IS NOT NULL') 
       ->orderBy('c.root, c.lft', 'ASC'); 

    $formMapper 
     ... 
     ->add('categoria', 'sonata_type_model', array(
      'required' => true, 
      'query' => $query 
     )) 
     ... 
    ; 
} 

मुझे आशा है कि यह ऊपर जवाब मैं था Afterreading मदद कर सकते हैं किसी को

+0

मुझे 'CompanyMyBundle: Category' का उपयोग करना पड़ा। – Patrick

+0

मैंने कोशिश की है लेकिन यह मेरे मामले में काम नहीं कर रहा है – Kirit

0

कार्यक्षमता प्राप्त करने के लिए निम्न कार्य करने के लिए OP:

protected function configureFormFields(FormMapper $formMapper) 
{ 

$em = $this->modelManager->getEntityManager('YourBundleFile\YourBundleFileBundle\Entity\YourEntity'); 

$qb = $em->createQueryBuilder(); 

$qb = $qb->add('select', 'u') 
     ->add('from', 'YourBundleFile\YourBundleFileBundle\Entity\YourEntity u'); 

$query = $qb->getQuery(); 
$arrayType = $query->getArrayResult(); 

$formMapper 
->add('yourProperty', 'choice', array('choices'=>$arrayType)) 
-end(); 
} 
संबंधित मुद्दे