2016-04-02 1 views
0

したがって、私はユーザーテーブルとユーザー債権者の間に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でログインして取得し、特定のデータを返しますか?

+0

[カスタム選択要素を作成しない](http://stackoverflow.com/questions/34825701/how-to-load-select-option-from-database-in-zf2/34829272#answer-34829272)必要なのは、フォーム要素マネージャーに登録されたサービスファクトリーだけです。このファクトリはオプションを設定し、認証されたユーザをフェッチすることができます。 select要素が非常にシンプルでない限り、私は個人的にDoctrine 'ObjectSelect'をカスタム要素に使う必要はないと個人的に感じています。 – AlexP

答えて

0

これが私の解決策でした。 hereから実施例3このpostから提案を使用

私は次のことを行っている:

  1. リポジトリ
  2. を作成しましたが、私のフォームに自分の認証サービスを注入し、フィールド内のparamとしての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', 
      ] 
     ] 
    ); 

、次のように注釈を更新することにより、私のレポートエンティティにリポジトリを追加しました:

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

/** 
* @ORM\Entity(repositoryClass="CustomRepository") 
* @ORM\Table(name="report") 
*/ 
class Report 
{ 

そして最後には、リポジトリのクラスを追加しました

あなたはデュ、苦労したり、エラーを取得している場合リポジトリデータを融点と、それは正しいオブジェを返していることを確認してください。

関連する問題