Php Symfony2/Twig:如何将允许的值设置为dropDownField
我有3个实体:Php Symfony2/Twig:如何将允许的值设置为dropDownField,php,symfony,doctrine-orm,twig,Php,Symfony,Doctrine Orm,Twig,我有3个实体:User、Report和ReportCategory。 用户可以将报表放在一个报表类别中。在用户实体中有一个列表,该列表允许用户使用ReportCategories。这一切都很好——我用一个具有userID和reportCategoryId的connectionTable创建了它 现在,我在Controller中创建一个数组,以获取当前登录用户的所有ReportCategories: public function newAction() { $entity
User
、Report
和ReportCategory
。
用户可以将报表放在一个报表类别中。在用户实体中有一个列表,该列表允许用户使用ReportCategories。这一切都很好——我用一个具有userID和reportCategoryId的connectionTable创建了它
现在,我在Controller中创建一个数组,以获取当前登录用户的所有ReportCategories:
public function newAction()
{
$entity = new Report();
$form = $this->createCreateForm($entity);
$userId = $this->get('security.context')->getToken()->getUser()->getId();
$user = $this->getDoctrine()->getRepository('MyBundle:User')->find($userId);
$userReportCategories = array();
foreach($user->getReportCategories() as $reportCategory)
{
$userReportCategories[] = $reportCategory->getId();
}
return array(
'entity' => $entity,
'form' => $form->createView(),
'userReportCategories' => $userReportCategories
);
}
如何仅将这些值设置为我的细枝模板字段?当我创建一个自己的领域时,它不是以教义的形式管理的
{{ form_row(form.reportCategory, {'attr': {'class': 'form-control'}, 'label': 'Category'}) }}
谢谢你的帮助
更新:
我的报告类型如下所示:
class ReportType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('reportCategory')
->add('creationDate', 'date', array(
'data' => new \DateTime()
))
->add('headline')
->add('text')
->add('user')
;
}
....
创建一个表单类并添加一个事件侦听器,以便表单能够感知用户。以下是对Symfony文档中的的的改编。[不能保证准确捕获您的需求] 形式类 新的诉讼
但是我应该如何执行Select语句呢?在我的connectionTable中,“users\u reportCategories”是用户id和CategoryID,在我的用户实体中有一个allwoed类别的ArrayCollection。这可能吗?因为在我的用户实体表中没有类别字段。如果我理解正确,您的用户和报表类别之间有许多字段。大概连接表没有在
../Entity
中定义。如果实体配置正确,条令将建立连接。当您尝试选择categories->(user=$user)
时会发生什么情况?没错,“users\u reportCategories”没有实体。它是由学说管理的。但是在我的用户实体中,它看起来是这样的:/***@ORM\manytomy(targetEntity=“ReportCategory”,inversedBy=“users”)*@ORM\JoinTable(name=“users\u reportCategories”)***/private$reportCategories;使用带有事件订阅服务器的表单类时会发生什么?
use Symfony\Component\Security\Core\SecurityContext;
use Doctrine\ORM\EntityRepository;
// ...
class ReportFormType extends AbstractType
{
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
public function buildForm(FormBuilderInterface $builder, array $options)
{
// grab the user, do a quick sanity check that one exists
$user = $this->securityContext->getToken()->getUser();
if (!$user) {
throw new \LogicException(
'The ReportFormType cannot be used without an authenticated user!'
);
}
$builder->addEventListener(
FormEvents::PRE_SET_DATA,
function (FormEvent $event) use ($user) {
$form = $event->getForm();
$formOptions = array(
'class' => 'Acme\DemoBundle\Entity\ReportCategory',
'property' => 'category',
'query_builder' => function (EntityRepository $er) use ($user) {
// build a custom query
// return $er->createQueryBuilder('c')
->select('category')
->where('user = $user);
},
);
// create the field, this is similar the $builder->add()
// field name, field type, data, options
$form->add('userReportCategories', 'entity', $formOptions);
}
);
}
// ...
}
public function newAction()
{
$entity = new Report();
$form = $this->createForm(new ReportFormType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}