したがって、私はユーザーテーブルとユーザー債権者の間に1対多の関連性を持っています。報告書を書くときに、ユーザは自分の債権者のどれに接続するかを選択する必要があります。私が抱えている問題は、データベースのすべての債権者ではなく、DoctrineSelectを使ってログインしたユーザーの債権者のみを返却したいということです。 DoctrineSelectをログインしているユーザーのIDに制限する方法がわかりません。Doctrine Select - 特定のユーザーデータを選択する方法
これは私がこれまでにやっていることです:
顧客 - 私は、次のを持っている私のフィールドセットで>債権者
:
$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',
]
]
);
これは、残念ながら、フォームにすべての債権者を添付ユーザーに属しているものに並んでいる。それは顧客を返した場合、私のクラスは、customElement探し
$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', <-*****************
]
]
);
:私は今、これを持って私のフィールドセットで
<?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);
}
}
:
だからこれを解決するために、私は以下のクラスを作成しました債権者サービスで次のコードを使用している債権者:
/**
* @return array
*/
public function findUserCreditors()
{
$creditors = $this->findByUserAuth();
$creditorArray = [];
foreach ($creditors AS $creditorObject)
{
$creditorArray[$creditorObject->getId()] = $creditorObject->getName();
}
return $creditorArray;
}
このwo 1つの小さな問題で治療する。私はレポートを編集すると、フォームは、事前に選択されていないドロップダウンで、以前に選択された債権を...
が私の質問はしかしこれです:
DoctrineSelectを使用する方法はありますし、私を使用してAuthenticationServiceは、ユーザーIDでログインして取得し、特定のデータを返しますか?
[カスタム選択要素を作成しない](http://stackoverflow.com/questions/34825701/how-to-load-select-option-from-database-in-zf2/34829272#answer-34829272)必要なのは、フォーム要素マネージャーに登録されたサービスファクトリーだけです。このファクトリはオプションを設定し、認証されたユーザをフェッチすることができます。 select要素が非常にシンプルでない限り、私は個人的にDoctrine 'ObjectSelect'をカスタム要素に使う必要はないと個人的に感じています。 – AlexP