Symfony2:如何在FormType中调用实体的存储库

2024-04-23

我尝试调用我的实体的存储库Category以我的实体的类形​​式BonCommande,但是出现了这个错误:

注意:未定义的属性:C:\wamp\www\Symfony_test\src\Application\VehiculeBundle\Form\BonCommandeType.php第74行中的Application\VehiculeBundle\Form\BonCommandeType::$em

这是我的代码:

班上BonCommandeType.php:

namespace Application\VehiculeBundle\Form;

use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Application\VehiculeBundle\Entity\Category;

class BonCommandeType extends AbstractType
{
        /**
     * @param FormBuilderInterface $builder
     * @param array $options
     */
       public function buildForm(FormBuilderInterface $builder, array $options)
    {
        // Name of the user
        $builder->add('observation', 'text');


    /* Add additional fields... */

    $builder->add('save', 'submit');

    // Add listeners
    $builder->addEventListener(FormEvents::PRE_SET_DATA, array($this, 'onPreSetData'));
    $builder->addEventListener(FormEvents::PRE_SUBMIT, array($this, 'onPreSubmit'));
}


protected function addElements(FormInterface $form, Category $categorie = null) {
    // Remove the submit button, we will place this at the end of the form later
    $submit = $form->get('save');
    $form->remove('save');


    // Add the province element
    $form->add('categorie', 'entity', array(
        'data' => $categorie,
        'empty_value' => '-- Choose --',
        'class' => 'ApplicationVehiculeBundle:Category',
        'property' => 'intitule',
        'mapped' => false)
    );

    // Cities are empty, unless we actually supplied a province
    $vehicules = array();
    if ($categorie) {
        // Fetch the cities from specified province
        $repo = $this->em->getRepository('ApplicationVehiculeBundle:Vehicule');
        $cities = $repo->findByCategory($categorie);
    }

    // Add the city element
    $form->add('vehicule', 'entity', array(
        'empty_value' => '-- Select a categorie first --',
        'class' => 'ApplicationVehiculeBundle:Vehicule',
        'choices' => $vehicules,
    ));

    // Add submit button again, this time, it's back at the end of the form
    $form->add($submit);
}


function onPreSubmit(FormEvent $event) {
    $form = $event->getForm();
    $data = $event->getData();

    // Note that the data is not yet hydrated into the entity.
    $categorie = $this->em->getRepository('ApplicationVehiculeBundle:Category')->find($data['categorie']);
    $this->addElements($form, $categorie);
}


function onPreSetData(FormEvent $event) {
    $account = $event->getData();
    $form = $event->getForm();

    // We might have an empty account (when we insert a new account, for instance)
    $categorie = $account->getVehicule() ? $account->getVehicule()->getCategorie() : null;
    $this->addElements($form, $categorie);
}
...
}

这是导致错误的指令:

$categorie = $this->em->getRepository('ApplicationVehiculeBundle:Category')->find($data['categorie']);

FormComponent是一个独立的组件,它不提供任何entityManager可供使用。你必须注入它或传递它$options如果你想使用它..

在你的情况下,如果你直接将它传递给类型是正确的__construct或路过$options数组或将您的类型声明为服务并向其注入实体管理器:

class BonCommandeType extends AbstractType
{
    private $em;

    public function __construct(EntityManagerInterface $em)
    {
        $this->em = $em;
    }

    ...
}

or

$this->createForm(TYPE, DATA, ['em' => $em]);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Symfony2:如何在FormType中调用实体的存储库 的相关文章

随机推荐

  • 如何在 Razor Pages 中返回带有对象的不同视图?

    在 NET Framework MVC中 我可以使用以下代码来完成 public class HomeController Controller public ActionResult Index Student student new S
  • ssrs 报告中的格式编号

    argh 受不了我自己也想不通 我在报告中的数字格式中使用了它 0 00 0 00 格式为 1 212 89 这不完全是我想要的 因为我想要 1 212 89 无论服务器的区域设置如何 所以我尝试了这个 0 00 0 00 但这给了我这个
  • 从 FeedParser 获取 Feed 并导入到 Pandas DataFrame

    我正在学习Python 作为实践 我正在使用 feedparser 构建一个 RSS 抓取器 将输出放入 pandas 数据帧并尝试使用 NLTK 进行挖掘 但我首先从多个 RSS 提要获取文章列表 我用这篇文章来了解如何传递多个提要 ht
  • CGridView 使用 Tab 时过滤重复的 Ajax 请求

    在我的 Web 应用程序中 我使用选项卡并根据 Ajax 加载部分视图 所以有可能我确实加载了相同的内容CGridView多次 无需重新加载页面 如果发生这种情况 如果我使用 我会变成重复的 Ajax 请求CGridView过滤器 过滤器和
  • 无法对 @KafkaListener 带注释的方法进行单元测试

    我正在尝试在 Spring 中对 kafka 消费者类进行单元测试 我想知道如果 kafka 消息发送到它的主题 则侦听器方法被正确调用 我的消费者类注释如下 KafkaListener topics kafka topics myTopi
  • 将浮点数向下舍入到最接近的整数?

    我想获取一个浮点数并将其向下舍入到最接近的整数 然而 如果它不是一个整体 我always想要向下舍入变量 无论它与下一个整数的接近程度如何 有没有办法做到这一点 int x 转换为整数将截断 朝向 0 0 例如math trunc 对于非负
  • 如何在VC++/MFC中替换字符串中两个子字符串之间的字符串?

    假设我有一个 CString 对象 strMain AAAABBCCCCCCDDBBCCCCCCCCDDDAA 我还有两个较小的字符串 例如 strSmall1 BB strSmall2 DD 现在 我想替换 strMain 中 strSm
  • 使用 Spring beans 作为带有 @Cacheable 注释的键

    如何使以下工作发挥作用 一个 spring bean 该 bean 具有应使用 Cacheable 注释进行缓存的方法 另一个为缓存创建密钥的 spring bean KeyCreatorBean 所以代码看起来像这样 Inject pri
  • 在 Angular X 的子模块中使用 AppModule 中的组件(X 代表 2+)

    我创建了一个小组件 LoadingComponent 在我的应用程序的根目录中并在我的应用程序中 显然 声明了它AppModule 该组件在我的应用程序加载时使用 并且应该显示一些精美的加载动画 现在我想在保存某些内容时在子模块中使用它 但
  • 在 vm 脚本上下文中传递函数

    假设我有一个如下所示的库模块 module exports increment function count 我想在动态生成的脚本中使用它 如下所示 function lib increment 通过将其传递到沙箱中 var sandbox
  • 如何在 MKMapView 中保持图钉和地图在移动叠加层上方居中

    当我在地图上垂直移动 通过平移手势 另一个视图时 如何使图钉保持在地图的中心 以便图钉保持在覆盖层 而不是实际的 MapKit 覆盖层 上方 请参阅随附的第一个和最终状态的屏幕截图 当用户向上 向下平移时 我得到了覆盖层和屏幕顶部之间的空间
  • 如何将 WPF 复选框置于其可点击区域的中心?

    如果我在 WPF 中创建一个 CheckBox 控件 没有内容 我只需要选中 取消选中部分 它会放置 框 视觉对象 其中有或没有复选标记的 3D 矩形 位于控件的左上角 我可以将 盒子 视觉效果放在center而是使用 CheckBox 控
  • Emacs Windows 拼写检查 - aspell 或 hunspell

    我在Windows XP操作系统上使用emacs 23 1 50版本 我无法使用 emacs wiki 提供的示例将 hunspell 或 aspell 设置为 emacs 的一部分 任何人都有 Windows XP 的工作配置 请帮助我
  • 条件 DataGridView 格式设置

    我有一个 DataGridView 我将其 DataSource 属性设置为我自己的对象的 BindingList BindingList
  • 合并数据框,保留所有项目熊猫

    如何合并两个不同的数据帧 保留每个数据帧的所有行 同时填充空白 DF1 Name Addr Num Parent Parent Addr Matt 123H 8 James 543F Adam 213H 9 James 543F James
  • “Docker 子网”有什么用?

    docker desktop 中有一个选项允许更改 Docker 子网 我没有看到这个默认子网192 168 65 0 28被用在任何地方 我尝试过了docker network inspect在每个 Docker 内部网络上 检查了 do
  • Cordova config.xml 文件被重写

    我设置了一个基本的 Cordova 项目 每当我运行 cordova build 时 IOS 中的 config xml 文件都会被重写为默认值 并且我在项目文件夹的 config xml 中添加的任何首选项都会简单地附加到配置中 IOS平
  • SQL Server Management Studio 无法连接到 Sql Server

    我已经使用 MS Web Platform Installer 2 0 安装了 Visual Web Developer 2010 SQL Server 2008 R2 和 SQL Management Studio 2008 但每当我想登
  • Java 泛型(通配符)

    我有几个关于 Java 中通用通配符的问题 有什么区别List 基本上意味着
  • Symfony2:如何在FormType中调用实体的存储库

    我尝试调用我的实体的存储库Category以我的实体的类形 式BonCommande 但是出现了这个错误 注意 未定义的属性 C wamp www Symfony test src Application VehiculeBundle Fo