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:validate. 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
- inverseBy must be inversed-by
- 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.