我有一个关于使用 Doctrine ORM 的类表继承 (CTI) 和 ZF2 的问题Zend\Form
。在我们的系统中,每个用户可以有多个角色,这些角色是通过类表继承方案定义的,因此我们可以定义角色特定的配置文件字段(即:“年级”仅适用于学生帐户)。为了构造它,有一个与(CTI“基”类)Account
有 OneToMany 关联的实体:Account\Role
<?php
namespace CdliPortal\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="account")
*/
class Account implements AccountInterface
{
// Other Fields Omitted
/**
* @ORM\OneToMany(targetEntity="CdliPortal\Entity\Account\Role", mappedBy="account", cascade={"ALL"})
*/
protected $roles;
}
我已经设置了一个Zend\Form
绑定Account
实体的实例(我按照文档中的示例进行DoctrineModule
操作),并且一切似乎都正常工作...除了 CTI 集合。在我的 Form 对象中,我添加了一个Zend\Form\Element\Collection
成员:
$roles = $user->getRoles();
if ( count($roles) > 0 ) {
$formAccount->add(array(
'type' => 'Zend\Form\Element\Collection',
'name' => 'roles',
'options' => array(
'count' => count($roles),
'target_element' => $this->serviceLocator->get('cdliportal_form_account_role'),
),
));
}
但是,正如您在上面看到的,在设置roles
字段时target_element
(在本例中为字段集)是在集合级别指定的,这意味着集合必须是统一的。对于我的上述情况,我需要能够为target_element
集合的每个元素单独指定,具体取决于它代表的 CTI 的哪个成员。
关于我如何以一种仍然允许在绑定 Account 对象时自动填充集合元素的方式实现这一点的任何想法/建议?