被简单的多对一关联问题所困扰



我正在遵循Symfony网站上的实体关系/关联示例,似乎无法使关联正常工作。

在本例中,Product具有Category,但Category不具有Products。(编辑)除非我特别将产品与类别关联,并将类别与产品关联。根据文件,这种关联应该是隐含的。

我创建了Category类:

class Category
{
    // ...
    /**
     * @ORMOneToMany(targetEntity="Product", mappedBy="category")
     */
    protected $products;
    public function __construct()
    {
        $this->products = new ArrayCollection();
    }
}

产品类别:

class Product
{
    // ...
    /**
     * @ORMManyToOne(targetEntity="Category", inversedBy="products")
     * @ORMJoinColumn(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("nnFetched 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("nnFetched 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,并且已经多次清除缓存。

您还需要将产品添加到控制器中的类别中,如…

$category->addProduct($product)

或者你可以在addProductsetter中添加类别setter,就像这样…

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

相关内容

  • 没有找到相关文章

最新更新