やあみんなまた助けを探しています。
私の問題は、2つの関連するORMエンティティが保存されないことです。
次の単体テストを実行しましたが、失敗します。
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 */
}
最後のテストが失敗した理由がわかりません。私の間違いを見つけるのを手伝ってくれませんか。
私はDoctrine2.3と一緒にSymfony2.1.4を実行しています
2つのORMオブジェクトがあります。
1.アイテム
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.カテゴリー
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;
}
}
よろしくお願いします!