Doctrine ORM将产品绑定到多个实体

时间:2015-07-31 11:51:19

标签: php symfony orm doctrine-orm doctrine

我有业务,类别和产品。类别分配给企业,产品分配到类别和业务。

原因是,可以将一种产品分配到不同业务的不同类别。

业务和类别实体工作正常,但我不确定如何编写产品实体来实现我的需求......

商家实体:

namespace Raiel\AFMage\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="businesses")
 */
class Business {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", name="id")
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=64)
     */
    private $name;

    /**
     *
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="businesses")
     * @ORM\JoinTable(
     *  name="business_categories",
     *  joinColumns={
     *      @ORM\JoinColumn(name="business_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *  }
     * )
     */
    private $categories;

    /**
     * Default constructor, initializes collections
     */
    public function __construct() {
        $this->categories = new ArrayCollection();
    }
}

类别实体:

<?php

namespace Raiel\AFMage\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;

/**
 * @Gedmo\Tree(type="nested")
 * @ORM\Table(name="categories")
 * @ORM\Entity(repositoryClass="Gedmo\Tree\Entity\Repository\NestedTreeRepository")
 */
class Category {
    /**
     * @ORM\Column(name="id", type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

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

    /**
     * @Gedmo\TreePath
     * @ORM\Column(name="path", type="string", length=3000, nullable=true)
     */
    private $path;

    /**
     * @ORM\Column(name="title", type="string", length=64)
     */
    private $title;

    /**
     * @Gedmo\TreeLevel
     * @ORM\Column(name="lvl", type="integer")
     */
    private $lvl;

    /**
     * @Gedmo\TreeLeft
     * @ORM\Column(name="lft", type="integer")
     */
    private $lft;

    /**
     * @Gedmo\TreeRight
     * @ORM\Column(name="rgt", type="integer")
     */
    private $rgt;

    /**
     * @Gedmo\TreeRoot
     * @ORM\Column(name="root", type="integer", nullable=true)
     */
    private $root;

    /**
     * @Gedmo\TreeParent
     * @ORM\ManyToOne(targetEntity="Category", inversedBy="children")
     * @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
     */
    private $parent;

    /**
     * @ORM\OneToMany(targetEntity="Category", mappedBy="parent")
     * @ORM\OrderBy({"lft" = "ASC"})
     */
    private $children;
    /**
     * Constructor
     */
    public function __construct() {
        $this->children   = new ArrayCollection();
        $this->businesses = new ArrayCollection();
    }
}

此外,$ business-&gt; addCategory工作正常但$ category-&gt; addBusiness不会保存到DB。

1 个答案:

答案 0 :(得分:0)

您的产品实体可能类似于:

namespace Raiel\AFMage\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity()
 * @ORM\Table(name="products")
 */
class Product {
    /**
     * @ORM\Id()
     * @ORM\GeneratedValue(strategy="AUTO")
     * @ORM\Column(type="integer", name="id")
     */
    private $id;

    /**
     * @ORM\Column(name="name", type="string", length=64)
     */
    private $name;

    /**
     *
     * @ORM\ManyToMany(targetEntity="Category", inversedBy="products")
     * @ORM\JoinTable(
     *  name="product_categories",
     *  joinColumns={
     *      @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="category_id", referencedColumnName="id")
     *  }
     * )
     */
    private $categories;

    /**
     *
     * @ORM\ManyToMany(targetEntity="Business", inversedBy="products")
     * @ORM\JoinTable(
     *  name="product_businesses",
     *  joinColumns={
     *      @ORM\JoinColumn(name="product_id", referencedColumnName="id")
     *  },
     *  inverseJoinColumns={
     *      @ORM\JoinColumn(name="business_id", referencedColumnName="id")
     *  }
     * )
     */
    private $businesses;
}

或多或少地为您的Business实体提供了什么,但使用不同的连接表使用ManyToMany关系。

要使$category->addBusiness()正常工作,您可能需要在类别中添加cascade选项,因此:

/**
 *
 * @ORM\ManyToMany(targetEntity="Business", mappedBy="categories", cascade="persist")
 */
private $businesses;

这告诉Doctrine应该保留添加到$businesses数组集合中的任何新内容,请查看documentation以获取详细信息。