所以我在用户表和用户债权人之间有一个一对多的关联。在编写报告时,用户需要选择要连接到他或她的债权人中的哪一个。我遇到的问题是,我只想使用DoctrineSelect返回登录的用户债权人,而不是数据库中的每个债权人。我不知道如何将DoctrineSelect限制为登录用户的ID。
这就是我迄今为止所做的:
客户->债权人
在我的字段集中,我有以下内容:
$this->add(
[
'type' => 'DoctrineModule'Form'Element'ObjectSelect',
'name' => 'creditor',
'options' => [
'object_manager' => $this->objectManager,
'target_class' => 'Debtor'Entity'Creditor',
'label_generator' => function($targetEntity) {
return $targetEntity->getTitle() . ' - ' . $targetEntity->getFirstName() . ' - ' . $targetEntity->getLastName();
},
'label' => 'Creditor',
'instructions' => 'Attach creditors to this report'
],
'attributes' => [
'multiple' => true,
'required' => 'required',
]
]
);
不幸的是,这将所有债权人都附加到了属于用户的表格中。
因此,为了解决这个问题,我创建了以下类:
<?php
namespace Application'Form;
use Debtor'Service'CreditorService;
use Zend'Form'View'Helper'FormElement as BaseFormElement;
use Zend'Form'ElementInterface;
class FormElement extends BaseFormElement
{
public function __construct(
CreditorService $creditorService
)
{
$this->creditorService = $creditorService;
}
/**
* This function overrides the ZendFormElement method which allows us to set the format of our own elements if required
* @param ElementInterface $element
* @return string
*/
public function render( ElementInterface $element )
{
$attributes = $element->getAttributes();
if ( isset( $attributes['customElement']) )
{
$customElement = $attributes['customElement'];
switch( $customElement ) {
case 'getCreditors':
$creditors = $this->creditorService->findUserCreditors();
$element->setValueOptions( $creditors );
break;
}
}
return parent::render($element);
}
}
在我的领域中,我现在有了这个:
$this->add(
[
'type' => 'select',
'name' => 'creditor',
'options' => [
'label' => 'Creditor',
'instructions' => 'Attach creditors to this report',
],
'attributes' => [
'multiple' => true,
'required' => 'required',
'class' => 'form-control input-medium select2me',
'customElement' => 'getCreditors', <-*****************
]
]
);
在我的类查找customElement的地方,如果找到它,它会在我的债权人服务中使用以下代码返回客户债权人:
/**
* @return array
*/
public function findUserCreditors()
{
$creditors = $this->findByUserAuth();
$creditorArray = [];
foreach ($creditors AS $creditorObject)
{
$creditorArray[$creditorObject->getId()] = $creditorObject->getName();
}
return $creditorArray;
}
这是对一个小问题的处理。当我编辑报告时,表单不会在下拉列表中预先选择以前选择的债权人。。。
我的问题是:
有没有办法使用DoctrineSelect并使用我的AuthenticationService,获取登录用户ID并返回特定数据?
这是我的解决方案。
使用这里的示例3和本文的建议。
我做了以下工作:
- 创建了存储库
- 将我的auth服务注入到我的表单中,并将authService作为参数包含在字段中
我的字段集:
$this->add(
[
'type' => 'DoctrineModule'Form'Element'ObjectSelect',
'name' => 'creditor',
'options' => [
'label' => 'Creditor: ',
'object_manager' => $this->objectManager,
'target_class' => 'Debtor'Entity'Report',
'property' => 'Report',
'is_method' => true,
'find_method' => [
'name' => 'findUserCreditors',
'params' => [
'authService' => $this->authService
],
]
],
'attributes' => [
'multiple' => true,
'required' => 'required',
'class' => 'form-control input-medium select2me',
]
]
);
并通过更新注释向我的报告实体添加了一个存储库,如下所示:
/**
* @ORM'Entity(repositoryClass="CustomRepository")
* @ORM'Table(name="report")
*/
class Report
{
最后添加了存储库类:
<?php
namespace Debtor'Entity;
use Customer'Service'AuthenticationService;
use Doctrine'ORM'EntityRepository;
class CreditorRepository extends EntityRepository
{
public function findUserCreditors(AuthenticationService $authService)
{
$userObject = $authService->getIdentity();
$qb = $this->createQueryBuilder('c')
->orderBy('c.created', 'ASC');
$qb->where('c.user = :user')
->setParameter('user' , $userObject );
$result = $qb->getQuery()->getResult();
return $result;
}
}
如果您遇到困难或出现错误,请转储存储库数据,并确保它返回正确的对象。