[英]Symfony Messaging Bundle choices
我試圖弄清楚如何將數據庫字段映射到symnfony表單選擇。 該表格使用的是symfony捆綁包的朋友,這是我的第一個symfony項目(更多的是Laravel / CI人員)。
我需要根據數據庫中的字段填充表單選擇,問題是,我不知道該項目在哪里是Doctrine ORM的實體管理器(它是一個真正無組織的文件結構),我不知道如何僅運行本機查詢進行測試,直到我能弄清楚。
該項目使用消息包,表單模型/處理程序/類型等。我已經正確生成了表單,我只需要弄清楚將數據放在何處以及如何做即可。
到目前為止,我有這個:
<?php
namespace Application\FOS\MessageBundle\FormType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use NeaceLukens\Bundle\Entity\File;
use Doctrine\ORM\EntityManager;
// use NeaceLukens\Bundle\Entity\FileCollection;
use FOS\MessageBundle\FormModel\NewThreadMessage;
class NewThreadMessageBulkFormType extends AbstractType
{
public function buildForm(FormBuilderInterface $builder, array $options)
{
// $choices = $this->getGroups();
$builder
->add(
'recipient', 'choice', array(
'empty_value' => false,
'multiple'=>true,
'choices'=>array('1'=>'test1', '2'=>'test2', '3'=>'test3'),
'attr'=>array('class'=>'form-control'),
)
)
->add('subject', 'text', array('attr'=>array('class'=>'form-control')))
->add('body', 'textarea', array('attr'=>array('class'=>'form-control')));
}
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'intention' => 'message'
));
}
public function getName()
{
return 'fos_message_new_thread';
}
public function getGroups(){
// build the mapping here?
/*
$stmt = $this->getEntityManager()
->getConnection()
->prepare('SELECT t.id, t.name FROM fos_user_group');
$r = $stmt->execute();
return $r; */
}
}
名為getGroups的函數會出錯。 我收到一條錯誤消息,內容如下:
在第63行中調用未定義的方法Application \\ FOS \\ MessageBunde \\ FormType \\ NewThreadMessageBulkFormType :: getEntityManager()
當您要使用數據庫中的值填充選擇字段時,應使用此處描述的“實體”字段類型-http: //symfony.com/doc/current/reference/forms/types/entity.html
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.