2016-05-19 6 views
0

にデータベースの値に基づいて、選択リストではなく、このようにそれを手動で設定するのデータベースからマッピングされた選択肢の値を持つconfigureformfieldsに選択肢のリストを追加することが可能です:エンティティが正しくマッピングされている場合ソナタ

->add('testfield', 'choice', array('choices' => array(
        '1' => 'choice 1', 
        '2' => 'choice 2',))) 

答えて

0

->add('testfield') 

とSonataの管理者がこの仕事を行います。

<?php 

namespace AppBundle\Entity; 

use Doctrine\ORM\Mapping as ORM; 

/** 
* Product 
* 
* @ORM\Table(name="product") 
* 
*/ 
class Product 
{ 
    /** 
    * @var int 
    * 
    * @ORM\Column(name="id", type="integer") 
    * @ORM\Id 
    * @ORM\GeneratedValue(strategy="AUTO") 
    */ 
    private $id; 

    /** 
    * @ORM\ManyToOne(targetEntity="Category", inversedBy="products") 
    */ 
    protected $category; 

    /** 
    * Get id 
    * 
    * @return int 
    */ 
    public function getId() 
    { 
     return $this->id; 
    } 

    /** 
    * Set category 
    * 
    * @param Category $category 
    * 
    * @return Product 
    */ 
    public function setCategory(Category $category = null) 
    { 
     $this->category = $category; 

     return $this; 
    } 

    /** 
    * Get category 
    * 
    * @return Category 
    */ 
    public function getCategory() 
    { 
     return $this->category; 
    } 
} 

単純に使用して::

->add('category') 

は、すべてのカテゴリを選択し、フォームフィールドを提供します

あなたが製品カテゴリクラスにリンクされているクラスがあるとしましょう。あなたは、より高度な何かしたい場合にもSONATA_TYPE_MODELを使用することができます

<?php 
// src/AppBundle/Admin/ProductAdmin.php 

class ProductAdmin extends AbstractAdmin 
{ 
    protected function configureFormFields(FormMapper $formMapper) 
    { 
     $imageFieldOptions = array(); // see available options below 

     $formMapper 
      ->add('category', 'sonata_type_model', $imageFieldOptions) 
     ; 
    } 
} 

ドキュメントは、このページにあります:Form Types

・ホープ、このことができます!

関連する問題