我有两张桌子:
分支机构:
+-------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+-------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
+-------------+--------------+------+-----+---------+----------------+
分支元:
+------------+--------------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+------------+--------------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| branch_id | int(11) | YES | MUL | NULL | |
| metaname | varchar(255) | NO | | NULL | |
| metavalue | varchar(255) | NO | | NULL | |
+------------+--------------+------+-----+---------+----------------+
我希望有不同的元字段,例如“电话”、“电子邮件”等,也可能是多个。
目前我有这个代码(省略了设置器/获取器):
// 实体/分支.php
<?php
namespace Acme\Bundle\ConsysBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Branch
*
* @ORM\Entity(repositoryClass="Acme\Bundle\ConsysBundle\Entity\BranchRepository")
* @ORM\Table(name="branch")
*/
class Branch
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
*
* @ORM\OneToMany(targetEntity="BranchMeta", mappedBy="branch")
*/
private $metadata;
public function __construct()
{
$this->metadata = new ArrayCollection();
}
}
?>
// 实体/BranchMeta.php
<?php
namespace Acme\Bundle\ConsysBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* BranchMeta
*
* @ORM\Entity(repositoryClass="Acme\Bundle\ConsysBundle\Entity\BranchMetaRepository")
* @ORM\Table(name="branchmeta")
*/
class BranchMeta
{
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\ManyToOne(targetEntity="Branch", inversedBy="metadata")
* @ORM\JoinColumn(name="branch_id", referencedColumnName="id")
*/
private $branch;
/**
* @var string
*
* @ORM\Column(name="metaname", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="metavalue", type="string", length=255)
*/
private $value;
?>
然后我需要构建一个表单来添加这样的分支:
Branch Name: [_________]
Branch Phone: [_________] [-]
Branch Phone: [_________] [+]
Branch Email: [_________] [-]
Branch Email: [_________] [+]
[Submit]
其中“Branch Phone”字段指向带有元名称“phone”和对应值的分支元表,“Branch Email”字段指向带有元名称“email”的分支元表。两者都可以动态添加/删除。
如何使用 Symfony2 的 FormBuilder 构建它?我像这样创建了 BranchType:
class BranchType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
}
public function getName()
{
return 'branch';
}
}
和 BranchMetaType:
class BranchMetaType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder->add('name');
$builder->add('value');
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'Acme\Bundle\ConsysBundle\Entity\BranchMeta',
));
}
public function getName()
{
return 'branch_meta';
}
}
但后来我被卡住了......如何正确使用 BranchMetaType 在我的情况下构建所需的表单?也许我在数据映射中遗漏了一些东西?