1

Can anybody tell me what Im doing wrong.

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

because of symfony docs says we have to make relashions one-to-many by hands i added this line to 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> ...

my 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>..

and here is the code in Entity/Article.php:

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

i also have addArticlelang and remove methods generated by symfony

In my controller:

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

and dump func shows me:

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
}

Im expecting to get an objects with my articleLangs, I can do this with querybuilder, but i dont understand why it wont work such way..

My 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.

but I have the 'inversedBy="articlelang" attribute in my ArticleLang many-to-one relashionship..

okay, i fix error that was showing above in console doctrine:schema:vali‌​date. Its very strange but inversedBy="articlelang" -> I've got this in my xml file. but it has to be like this in order to /doctrine/orm/lib/Doctrine/ORM/Mapping/Driver/XmlDriver.php 456 line inversed-by="articlelang". but anyway i'm getting the same result, but without error on schema validate)))

I set fetch="EAGER" parameter in my article.orm.xml and its working now)


So I solved my problem. there was 2 errors

  1. inverseBy must be inversed-by
  2. fetching mode (lazy is default in doctrine orm).. here is the info about What is the difference between fetch="EAGER" and fetch="LAZY" in doctrine,

still dont understand why its not working with lazy mode.

Community
  • 1
  • 1
timosh
  • 81
  • 8
  • Do you know about `php bin/console doctrine:schema:validate`? What does it tell you? Also, how many entries are in the ArticleLang table for the Article with ID 1? What query does the SQL debugger output and what is returned if you just run the query? – YetiCGN Aug 15 '16 at 11:54
  • and what is exactly your problem? your getter is giving you PersistentCollection, which can be iterated, or if you like, can be transformed to array simply by using ->toArray() function (not recommended) – omxv Aug 15 '16 at 15:55
  • no, i didnt use schema:validate, thank you for show it to me) now i know, where is the error. [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. but even with this knowlinkg i cant fix it. I got the inversedBy="articlelang" in my ArticleLang.orm.xml – timosh Aug 16 '16 at 17:50
  • okay, i fix error doctrine:schema:vali‌​date. – timosh Aug 16 '16 at 20:06
  • I set fetch="EAGER" parameter in my article.orm.xml and its working now) Dont tou know why is it not fetching lazy? EAGER is fine to me, but its just interesting.. – timosh Aug 16 '16 at 20:32

0 Answers0