Symfony2 - 实体集合选择限制
Symfony2 - Entity Collection Choice Restriction
我有以下设置:
Entity: Customer
Entity: Account
Entity: Message
现在想象以下问题:
The account 'Mark' is in charge of two customers, 'Ben' and 'Lili'.
The account 'Tim' is in charge of two other customers, 'Tom' and 'Ronny'.
The account 'Ben' now wants to send a message to his customers. In a form he can choose the customers he would like to send the message to. Those will be saved as an ArrayCollection in the Message entity (in relation with entity Customer).
However, later on account 'Tim' can view this message and also send it to his customers the same way - by adding his customers to the list of recepients.
Problem is: When 'Tim' adds his recepients, he should not see the recepients of 'Ben' as this is none of his concern.
视觉解释:http://jsfiddle.net/q0nn62o5/
目前我的解决方案:
我创建了一个名为 'AccountCustomerType' 的自定义 FormType。此 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>
还剩一个问题:
I can now choose from the customers that one account is in charge of. However, the recepients I am not in charge of are still shown as blank select fields. How can I hide these? I don't want to duplicate messages to seperate recepients as there are a couple more features connected to this.
您可以通过限制查询结果来过滤表单中的集合。
例如类似于:
$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。
这会将表单中显示的客户实体限制为仅链接到 $accountEntity 的实体。
请注意,这需要是您页面加载时第一次获取此关系,如果您使用 doctrine 延迟加载它,那么它将 return 所有客户实体而不考虑过滤。
我有以下设置:
Entity: Customer
Entity: Account
Entity: Message
现在想象以下问题:
The account 'Mark' is in charge of two customers, 'Ben' and 'Lili'.
The account 'Tim' is in charge of two other customers, 'Tom' and 'Ronny'.
The account 'Ben' now wants to send a message to his customers. In a form he can choose the customers he would like to send the message to. Those will be saved as an ArrayCollection in the Message entity (in relation with entity Customer).
However, later on account 'Tim' can view this message and also send it to his customers the same way - by adding his customers to the list of recepients.
Problem is: When 'Tim' adds his recepients, he should not see the recepients of 'Ben' as this is none of his concern.
视觉解释:http://jsfiddle.net/q0nn62o5/
目前我的解决方案:
我创建了一个名为 'AccountCustomerType' 的自定义 FormType。此 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>
还剩一个问题:
I can now choose from the customers that one account is in charge of. However, the recepients I am not in charge of are still shown as blank select fields. How can I hide these? I don't want to duplicate messages to seperate recepients as there are a couple more features connected to this.
您可以通过限制查询结果来过滤表单中的集合。
例如类似于:
$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。
这会将表单中显示的客户实体限制为仅链接到 $accountEntity 的实体。
请注意,这需要是您页面加载时第一次获取此关系,如果您使用 doctrine 延迟加载它,那么它将 return 所有客户实体而不考虑过滤。