【问题标题】:Symfony - Set an Entity to a ManyToOneSymfony - 将实体设置为多对一
【发布时间】:2019-01-04 14:39:48
【问题描述】:

好的,我问你一个简单的问题,我如何设置一个不在参数中的实体

我正在尝试这个方法:

    $em_category = $this->getDoctrine()->getManager();
    $em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category'))));
    $userInterest->setUser($user);

    $userInterest->setCategory($em_category);

但是当我这样做时,我有这个错误:

关联字段“AppBundle\Entity\UserInterest#$category”的类型为“AppBundle\Entity\Category”的预期值,改为“Doctrine\ORM\EntityManager”。

那么我怎样才能将我想要的类别设置为用户兴趣...? 感谢所有将尝试回答的人:p

【问题讨论】:

    标签: php symfony json-api


    【解决方案1】:

    变量$em_category 是您的实体管理器,该类执行从数据库存储和检索数据。您需要使用它来获取类别,然后将该值传递给setCategory。像这样。

    $cat = $em_category->getRepository('AppBundle:Category')->findOneBy(array('id' => ($request->get('id_category'))));
    $userInterest->setCategory($cat);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2020-05-08
      • 1970-01-01
      • 2019-03-08
      • 2021-01-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多