簡體   English   中英

Symfony2-實體集合選擇限制

[英]Symfony2 - Entity Collection Choice Restriction

我有以下設置:

Entity: Customer
Entity: Account
Entity: Message

現在想象以下問題:

帳戶“ Mark”負責兩個客戶“ Ben”和“ Lili”。

帳戶“ Tim”負責另外兩個客戶“ Tom”和“ Ronny”。

現在,“ Ben”帳戶希望向其客戶發送消息。 他可以用某種形式選擇他想發送消息的客戶。 這些將被保存為Message實體中的ArrayCollection(與實體Customer相關)。

但是,稍后,“ Tim”帳戶可以查看此消息並將其發送給他的客戶,方法是將他的客戶添加到收件人列表中。

問題是:當“蒂姆”添加他的接受者時,他不應該看到“本”的接受者,因為這與他無關。

視覺說明: http : //jsfiddle.net/q0nn62o5/

到目前為止我的解決方案:

我創建了一個自定義FormType,稱為“ AccountCustomerType”。 此FormType是一個實體,其中包括一個特定帳戶的客戶作為選擇:

$builder
    ->add('customer', 'entity', array(
        'class' => 'AppBundle:Customer',
        'choices' => $this->customers,
    ));

此FormType在主窗體中用作集合:

$form->add('recepients', 'collection', array(
    'type' => new AccountCustomerType($customers),
    'allow_add' => true,
    'allow_delete' => true,
    'delete_empty' => true,
    'by_reference' => false,
));

打印表格...:

<div class="recepients" data-prototype="{{ form_widget(form.recepients.vars.prototype)|e }}">
    {% for customer in form.recepients %}
        <div>
            {{ form_widget(customer) }}
        </div>
    {% endfor %}
</div>

剩下一個問題:

我現在可以從負責一個帳戶的客戶中選擇。 但是,我不負責的收件人仍顯示為空白選擇字段。 我怎么藏起來? 我不想重復消息以分隔收件人,因為與此相關的功能有很多。

您可以通過限制查詢結果來過濾表單中的集合。

例如:

$accountData = $this->getEntityManager()
    ->createQueryBuilder()->select('a, c')
    ->from('YourAccountBundle:Account', 'a')
    ->join('a.customers', 'c') // assuming there is a relationship like this
    ->where('a = :yourAccountManager')
    ->setParameter('yourAccountManager', $accountEntity) 
    ->getQuery()->getResult();

然后在您的父窗體中使用$ accountData。

這會將表格中顯示的Customer實體限制為僅與$ accountEntity鏈接的實體。

請注意,這必須是頁面加載中此關系的首次獲取,如果您使用准則延遲加載該關系,則它將不考慮過濾而返回所有Customer實體。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM