孩子数取决于考试的方法



在尝试自引用ManyToOne实体时,我发现子实体的数量取决于用于查找父实体的方法。如果使用存储库,则测试将失败,并显示Failed asserting that 1 matches expected 2。如果使用DQL,则测试成功。参见下面测试段中的testTwoKids()。我已经在调试中确认,在这两种情况下,数据库中的实际计数都是2。

我的猜测是实体的定义有问题。

实体:

class Category
{
    /**
     * @var integer
     *
     * @ORMColumn(name="id", type="integer", nullable=false)
     * @ORMId
     * @ORMGeneratedValue(strategy="IDENTITY")
     */
    protected $id;
    /**
     * @ORMColumn(name="name", type="string", nullable=false)
     */
    private $name;
    /**
     * @ORMOneToMany(targetEntity="Category", mappedBy="parent")
     **/
    private $children;
    /**
     * @ORMManyToOne(targetEntity="Category", inversedBy="children", cascade={"persist","remove"})
    *  @ORMJoinColumn(name="parent_id", referencedColumnName="id")
     **/
    private $parent;
    public function __construct() {
        $this->children = new ArrayCollection();
    }
    public function addChildren($child)
    {
        $this->children[] = $child;
        return $this;
    }
    public function setName($name)
    {
        $this->name = $name;
    }
    public function getName()
    {
        return $this->name;
    }
    public function setParent($parent)
    {
        $this->parent = $parent;
    }
    public function getParent()
    {
        return $this->parent;
    }
    /**
     * Get id
     *
     * @return integer 
     */
    public function getId()
    {
        return $this->id;
    }
    /**
     * Add children
     *
     * @param AppBundleEntityCategory $children
     * @return Category
     */
    public function addChild($child)
    {
        $this->children->add($child);
        $child->setParent($this);
        return $this;
    }
    /**
     * Remove children
     *
     * @param AppBundleEntityCategory $children
     */
    public function removeChild(AppBundleEntityCategory $children)
    {
        $this->children->removeElement($children);
    }
    /**
     * Get children
     *
     * @return DoctrineCommonCollectionsCollection 
     */
    public function getChildren()
    {
        return $this->children;
    }
}

测试

class CategoryTest extends KernelTestCase
{
    /**
     * @var DoctrineORMEntityManager
     */
    private $em;
    /**
     * {@inheritDoc}
     */
    public function setUp()
    {
        self::bootKernel();
        $this->em = static::$kernel->getContainer()
                ->get('doctrine')
                ->getManager()
        ;
        $product = new AppBundleEntityProduct();
        $product->setName('foo');
        $this->em->persist($product);
        $cat = new AppBundleEntityCategory();
        $cat->setName('Hook');
        $child = new AppBundleEntityCategory();
        $child->setName('Dry');
        $child->setParent($cat);
        $this->em->persist($child);
        $this->em->flush();
    }
    public function testTwoKids()
    {
        $c = $this->em->getRepository('AppBundle:Category')->findOneBy(['name' => 'Hook']);
        $cat = new AppBundleEntityCategory();
        $cat->setName('Wet');
        $this->em->persist($cat);
        $c->addChild($cat);
        $this->em->persist($c);
        $this->em->flush();
        /* this returns 1 */
        $c2 = $this->em->getRepository('AppBundle:Category')->findOneBy(['name' => 'Hook']);
        $children = $c2->getChildren();
        $this->assertEquals(2, count($children));
        /* this returns 2 */
//        $id = $c2->getId();
//        $children = $this->em->createQuery("SELECT c FROM AppBundle:Category c WHERE c.parent = $id")
//                ->getResult();
//       $this->assertEquals(2, count($children));
            }

在上次刷新后也尝试运行$this->em->clear()。它可能是由条令实体管理器缓存或结果缓存引起的。

相关内容

  • 没有找到相关文章

最新更新