【问题标题】:How can I make a Many-To-Many Connection to a bundle entity?如何与捆绑实体建立多对多连接?
【发布时间】:2022-09-30 16:39:13
【问题描述】:

我尝试与我的 Category 类建立 ManyToMany 连接:

事件.php:

class Event
{
   /**
   * @ORM\\ManyToMany(targetEntity=\"myApp\\Bundle\\CategoryBundle\\Entity\\Category\", inversedBy=\"events\")
   */
   private $categories;


   public function addCategory(Category $category): self
    {
        if (!$this->categories->contains($category)) {
            $this->categories[] = $category;
        }
        return $this;
    }

事件控制器.php

$entity->addCategory($category);

但我收到错误消息:

App\\Entity\\Event::addCategory(): 参数 #1 ($category) 必须是 键入 App\\Entity\\Category,myApp\\Bundle\\CategoryBundle\\Entity\\Category 给定的,调用的 /mypage/src/Controller/Admin/EventController.php 在第 153 行

    标签: php symfony entity


    【解决方案1】:

    您需要明确包含您的类别类,否则它将查看相同的命名空间App\Entity 并且找不到它。

    所以添加:

    use myApp\Bundle\CategoryBundle\Entity\Category;
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-03-02
      • 1970-01-01
      • 1970-01-01
      • 2016-06-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多