dqrq93879 2015-07-31 11:51
浏览 56
已采纳

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

I have businesses, categories and products. Categories are assigned to businesses and products are assigned to categories and businesses.

Reason being, one product can be assigned to different categories in different businesses.

Business and Category entities are working fine, but I am not sure how to write a Product entity to achieve what I need...

Business Entity:

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();
    }
}

Category Entity:

<?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();
    }
}

Also, $business->addCategory works fine but $category->addBusiness doesn't save to DB.

  • 写回答

1条回答 默认 最新

  • douyan4958 2015-07-31 12:45
    关注

    Your product entity will probably look something like:

    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;
    }
    

    So more or less what you have for your Business entity but with the ManyToMany relation using a different join table.

    And to make $category->addBusiness() work then you'll probably want to add a cascade option to category, thusly:

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

    This informs Doctrine that anything new added to the $businesses array collection should be persisted, take a look at the documentation for details.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 cv::resize不同线程时间不同
  • ¥15 web课程,怎么做啊😭没好好听课 根本不知道怎么下手
  • ¥15 做一个关于单片机的比较难的代码,然后搞一个PPT进行解释
  • ¥15 python提取.csv文件中的链接会经常出现爬取失败
  • ¥15 数据结构中的数组地址问题
  • ¥15 maya的mel里,怎样先选择模型A,然后利用mel脚本自动选择有相同名字的模型B呢。
  • ¥15 Python题,根本不会啊
  • ¥15 会会信号与系统和python的来
  • ¥15 关于#python#的问题
  • ¥20 oracle RAC 怎么配置啊,配置