Symfony主义一对多的关系

时间:2016-08-14 19:14:54

标签: xml symfony doctrine-orm

任何人都可以告诉我我做错了什么。

  "symfony/symfony": "3.1.*",
        "doctrine/orm": "^2.5",

因为symfony docs说我们必须手工制作一对多关系,我将这一行添加到article.orm.xml

my Article.orm.xml

... <one-to-many field="articlelang" target-entity="ArticleLang" mapped-by="article">
            <cascade>
                <cascade-persist/>
            </cascade>
    </one-to-many> ...

我的ArticleLang.orm.xml

  ..<many-to-one field="article" target-entity="Article"
 inversedBy="articlelang" fetch="EAGER">
       <join-columns>
         <join-column name="article_id" referenced-column-name="id"/>
       </join-columns>
     </many-to-one>..

,这是Entity / Article.php中的代码:

  private $articlelang;
    public function __construct()
    {
        $this->articlelang = new ArrayCollection();
    }  public function getArticlelang()
    {
        return $this->articlelang;
    }

我也有addArticlelang并删除symfony生成的方法

在我的控制器中:

  $article =  $em->getRepository('HelloSiteBundle:Article')->findOneByUrl($slug);
        dump($article->getArticlelang());
        exit();

和dump func告诉我:

PersistentCollection {#348 ▼
  -snapshot: []
  -owner: Article {#332 ▼
    -author: 1
    -updatedAt: DateTime {#328 ▶}
    -createdAt: DateTime {#329 ▶}
    -url: "contact_us"
    -status: true
    -id: 1
    -articlelang: PersistentCollection {#348}
  }
  -association: array:15 [ …15]
  -em: EntityManager {#284 …11}
  -backRefFieldName: "article"
  -typeClass: ClassMetadata {#334 …}
  -isDirty: false
  #collection: ArrayCollection {#343 ▼
    -elements: []
  }
  #initialized: false
}

我希望用我的articleLangs得到一个对象,我可以用querybuilder做到这一点,但我不明白为什么它不会这样工作..

我的bin / console doctrine:schema:validate:

[Mapping]  FAIL - The entity-class 'SiteBundle\Entity\Article' mapping is invalid:
* The field SiteBundle\Entity\Article#articlelang is on the inverse side of a bi-directional relationship, but the specified mappedBy association on the target-entity SiteBundle\Entity\ArticleLang#article does not contain the required 'inversedBy="articlelang"' attribute.

但我在ArticleLang多对一关系中有'inversedBy =“articlelang”属性..

好的,我修复了控制台原则中显示的错误:schema:vali date。它非常奇怪,但 inversedBy =“articlelang” - &gt;我在我的xml文件中有这个。但它必须是这样的,以便/doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php 456行反转 =“articlelang”。但无论如何我得到相同的结果,但没有错误的架构验证)))

我在article.orm.xml中设置了fetch="EAGER"参数,现在正在工作)

所以我解决了我的问题。有2个错误

  1. inverseBy必须反转
  2. 抓取模式(懒惰是doctrine orm中的默认设置)..这是关于What is the difference between fetch="EAGER" and fetch="LAZY" in doctrine的信息,
  3. 仍然不明白为什么它不使用懒惰模式。

0 个答案:

没有答案