0
基于数据库值选项列表是否有可能在configureformfields从数据库,而不是手动配置这样的映射选项值添加一个选择列表:奏鸣曲
->add('testfield', 'choice', array('choices' => array(
'1' => 'choice 1',
'2' => 'choice 2',)))
基于数据库值选项列表是否有可能在configureformfields从数据库,而不是手动配置这样的映射选项值添加一个选择列表:奏鸣曲
->add('testfield', 'choice', array('choices' => array(
'1' => 'choice 1',
'2' => 'choice 2',)))
如果实体正确映射那么你可以使用:
->add('testfield')
和索纳塔管理员将完成这项工作。
比方说,你有一个链接到一个类别类的产品类:
<?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
希望这有助于!