困惑于简单的多对一关联问题

时间:2013-12-09 20:45:56

标签: php symfony doctrine-orm

我正在关注Symfony网站上的Entity Relationships/Associations example,似乎无法让协会工作。

在此示例中,Product有一个Category,但Category没有Products。 (编辑)除非我特别将产品与类别和产品类别相关联。根据文件,应该暗示这种关联。

我已经创建了Category类:

class Category
{
    // ...

    /**
     * @ORM\OneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;

    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
}

产品类:

class Product
{
    // ...

    /**
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="products")
     * @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     */
    protected $category;
}

以下是我测试关联的代码:

public function simpleTestAction()
{
    $category = new Category();
    $category->setName('Main Products');

    $product = new Product();
    $product->setName('Foo');

    // relate this product to the category
    $product->setCategory($category);

    // EDIT: also relate the category to the product.
    // WHY does this fix the problem? WHY is this necessary?
    // associating both the Category to the Product AND
    // Product to the Category is required.
    // Uncomment the line below, and this will work.

    // $category->addProduct($product);

    $em = $this->getDoctrine()->getManager();
    $em->persist($category);
    $em->persist($product);
    $em->flush();

    $categoryId = $category->getId();
    $productId = $product->getId();

    print_r('Created product id: '.$productId.' and category id: '.$categoryId);

    // re-fetch the product to test
    $product = $this->getDoctrine()
        ->getRepository('AcmeStoreBundle:Product')
        ->find($productId);

    // prove that the product exists.
    print_r("\n\nFetched product: " . $product->getId());

    // fetch the category directly from the product
    $category = $product->getCategory();

    // prove that the product has a category.
    print_r("\nCategory name: " . $product->getCategory()->getName()); 

    // get the products from the category.
    // this should have at least one product, since we got the category
    // object directly from the product.
    $products = $category->getProducts();

    // prove we have a category
    print_r("\n\nFetched category: " . $category->getId());

    // issue: there are NO PRODUCTS belonging to this category,
    // even though the source product has this category.
    print_r("\nCount of products: " . count($products));

    die();
}

这是输出。如您所见,Product有一个Category,但Category没有Products

Created product id: 1 and category id: 1

Fetched product: 1
Category name: Main Products

Fetched category: 1
Count of products: 0

以下是取消注释互惠$category->addProduct($product);行之后的输出:

Created product id: 1 and category id: 1

Fetched product: 1
Category name: Main Products

Fetched category: 1
Count of products: 1

我正在使用Symfony 2.3.7并已多次清除缓存。

1 个答案:

答案 0 :(得分:2)

您还需要在控制器中将产品添加到类别中,如此...

$category->addProduct($product)

或者您可以在类别中的addProduct setter中添加类别设置器,如此...

public function addProduct(Product $product)
{
    $this->products->add($product);
    $product->setCategory($this);

    return $this;
}