Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8921651
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T06:35:58+00:00 2026-06-15T06:35:58+00:00

Hey guys I’m looking for help again. My problem is that two related ORM

  • 0

Hey guys I’m looking for help again.

My problem is that two related ORM entities not getting stored.

I run the following Unit test and it fails:

public function testCategoryRelation()
{
    $doctrine = $this->client->getContainer()->get('doctrine');
    $itemRepo = $doctrine->getRepository("AppBundle:Item");
    $item = $itemRepo->find(230045);
    $this->assertTrue($item instanceof \Acme\TestBundle\Entity\Item);

    $categories = $item->getCategories();
    $this->assertGreaterThan(0, $categories->count());
    foreach ($categories as $category) {
        $this->assertTrue($category instanceof \Acme\TestBundle\Entity\Category);
    }

    $count = $categories->count();
    $categoryRepo = $doctrine->getRepository("AppBundle:Category");
    $categories = $categoryRepo->findBy(array(), null, 2);
    $this->assertEquals( 2, count($categories) );

    foreach ($categories as $category) {
        $this->assertTrue($category instanceof \Acme\TestBundle\Entity\Category);
        $category->addItem($item);
        $item->addCategory($category);
    }
    $this->assertGreaterThan($count, $item->getCategories()->count());

    $em = $doctrine->getManager();
    foreach ($categories as $category) {

        $em->persist($category);
    }
    $em->persist($item);
    $em->flush();
    $em->clear($item);

    $item = $itemRepo->find(230045);
    $categories = $item->getRelatedItems();
    $this->assertGreaterThan($count, $categories->count()); /* Here it fails */
}

I have no clue why the last test fails. Could you please help me find my mistake?

I run Symfony 2.1.4 together with Doctrine 2.3

I have two ORM Objects.

1. Item

namespace Acme\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="item")
 */
class Item
{

  /**
   * @ORM\ManyToMany(targetEntity="Category", mappedBy="items")
   */
  private $categories;

  public function __construct()
  {
      $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
  }

  /**
   * @param \Acme\TestBundle\Entity\Category $category
   */
  public function addCategory(\Acme\TestBundle\Entity\Category $category)
  {
      $this->categories[] = $category;
      return $this;
  }

  /**
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getCategories()
  {
      return $this->categories;
  }
}

2. Category

namespace Acme\TestBundle\Entity;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Table(name="category")
 * @ORM\Entity(repositoryClass="Acme\TestBundle\Repository\CategoryRepository")
 */
class Category
{
  /**
   * @var \Doctrine\Common\Collections\ArrayCollection
   *
   * @ORM\ManyToMany(targetEntity="Item", inversedBy="categories")
   * @ORM\JoinTable(name="category_item",
   *   joinColumns={
   *     @ORM\JoinColumn(name="cat_id", referencedColumnName="id")
   *   },
   *   inverseJoinColumns={
   *     @ORM\JoinColumn(name="item_id", referencedColumnName="id")
   *   }
   * )
   */
  private $items;

  /**
   * @param \Acme\TestBundle\Entity\Item $item
   * @return Category
   */
  public function addItem(\Acme\TestBundle\Entity\Item $item)
  {
      $this->items[] = $item;
      return $this;
  }

  /**
   * @return \Doctrine\Common\Collections\Collection
   */
  public function getItems()
  {
      return $this->items;
  }
}

Thank you in advance for your help!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-15T06:35:59+00:00Added an answer on June 15, 2026 at 6:35 am

    Hm, $categories = $item->getRelatedItems();. Maybe it is better to write $categories = $item->getCategories();?

    The other suggestion is to add __construct method to Category entity class and initialize $items with new ArrayCollection

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

hey guys was hoping you could help me out, Not sure if I always
Hey guys I'm getting three things right now that I'm trying to take care
Hey guys I'm not getting any post data coming out here, and am pretty
hey guys hope you can help me out, I am having a weird problem.
hey guys having this really simple problem but cant seem to figure out have
Hey guys (and gals) I'm having a problem using Assembly GetExportedTypes() in .NET 4.0.
Hey guys was hoping you could help me out. basically i'm working on a
Hey guys I just noticed that my join statement here SELECT * FROM reports
Hey guys, I want to make a function loop over all elements that have
Hey guys, so I have two models in my project, grinders, and votes. So

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.