0

I had trouble formulating the title of my question. Apologies for that.

I have a OneToMany association between "Catalog" and "Category". I also have a self referencing bidirectional association on Category (since a category can contain multiple categories).

The problem is that when I fetch my catalog, the categories of my catalog are empty. I thought that Doctrine could return a full result since there is a foreign key in Category.

Here's two code snippet of what I did :

My entities :

Entity Catalog
/**
 * @ORM\OneToMany(targetEntity="Category", mappedBy="catalog")
 **/
private $catalogCategories;

Entity Category
/**
 * @ORM\ManyToOne(targetEntity="Catalog", inversedBy="catalogCategories")
 * @ORM\JoinColumn(name="catalog_id", referencedColumnName="catalog_id")
 **/
private $catalog;

/**
 * @ORM\OneToMany(targetEntity="Category", mappedBy="categoryParent")
 **/
private $categoryChildren;

/**
 * 
 * @ORM\ManyToOne(targetEntity="Category", inversedBy="categoryChildren")
 * @ORM\JoinColumn(name="category_parent_id", referencedColumnName="category_id")
 **/
private $categoryParent;

My Controller

 /**
 * @return array
 * @View()
 * @ParamConverter("catalog", class="TestRESTfulAPIBundle:Catalog") 
 */
public function getCatalogAction(Catalog $catalog)
{        
    return array('catalog' => $catalog);
}

Note that I'm using FOSRestBundle. When I simulate a HTTP request I get my Catalog in a JSON object but my catalog_categories is empty.

My question is divided in two parts : Is it possible to retrieve a complete record from my database, so my catalog contains all the categories in a recursive way.

If it is (which I think it's true), can anyone point my mistake?

I'm new to Symfony2 and english is not my native language so be indulgent with me.

Cheers.

Michael Villeneuve
  • 3,933
  • 4
  • 26
  • 40
  • It's probably because by default, referenced object are lazy loaded. Give a look at this question to see if it helps: http://stackoverflow.com/questions/17954591/avoid-doctrine-to-return-all-entities – cheesemacfly Nov 11 '13 at 20:14
  • Apologies, but I don't understand your answer. You are stating the documentation. If it's on by default shouldn't my code work? What am I missing. – Michael Villeneuve Nov 11 '13 at 20:33
  • What I am saying it that by default, `$catalog->Categories` is empty because of the [lazy loading](http://en.wikipedia.org/wiki/Lazy_loading) and that's the behavior you seem to see. – cheesemacfly Nov 11 '13 at 20:36
  • Look at the example here for a clear and easy to understand example: http://docs.doctrine-project.org/projects/doctrine-orm/en/latest/reference/working-with-objects.html#entity-object-graph-traversal (pay special attention to the second code snippet and the comments) – cheesemacfly Nov 11 '13 at 20:37
  • Hey, thanks for helping. In the end it was my mistake. My foreign key was actually null. Should of check that before posting. Thanks anyway for the links, help me understand what happen behind the scenes. – Michael Villeneuve Nov 11 '13 at 20:55

0 Answers0