Doctrine Select - 如何 select 特定用户数据

Doctrine Select - how to select specific users data

所以我在用户 table 和用户债权人之间建立了一对多的关联。在撰写报告时,用户需要 select 联系他或她的哪些债权人。我遇到的问题是我只想 return 使用 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',
            ]
        ]
    );

不幸的是,这会将所有债权人附加到表格,而不是属于用户的债权人。

因此,为了解决这个问题,我创建了以下 class:

<?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',  <-*****************
                ]
            ]
        );

我的 class 查找 customElement,如果找到它 return 客户债权人在我的债权人服务中使用以下代码:

 /**
     * @return array
     */
    public function findUserCreditors()
    {
        $creditors = $this->findByUserAuth();
        $creditorArray = [];

        foreach ($creditors AS $creditorObject)
        {
            $creditorArray[$creditorObject->getId()] = $creditorObject->getName();
        }

        return $creditorArray;

    }

这解决了一个小问题。当我编辑报告时,表格没有预先 selecting 下拉列表中以前 selected 的债权人...

不过我的问题是:

有没有办法使用 DoctrineSelect 并使用我的 AuthenticationService,获取登录用户 ID 和 return 具体数据?

这是我的解决方案。

使用 here and the suggestion from this post 中的示例 3。

我做了以下事情:

  1. 创建了存储库
  2. 将我的身份验证服务注入到我的表单中,并将 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
{

最后添加了存储库 class:

<?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;
    }
} 

如果您遇到困难或遇到错误,请转储存储库数据并确保它返回正确的对象。