所以我在用户表和用户债权人之间有一对多的关联。在编写报告时,用户需要选择要连接的债权人。我遇到的问题是我想只返回使用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并返回特定数据?
答案 0 :(得分:0)
这是我的解决方案。
我做了以下事情:
我的字段集:
$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;
}
}
如果您遇到困难或收到错误,请转储存储库数据并确保它返回正确的对象。