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 metadata提取的PDF元数据,如何转换为一个Excel
  • ¥15 关于arduino编程toCharArray()函数的使用
  • ¥100 vc++混合CEF采用CLR方式编译报错
  • ¥15 coze 的插件输入飞书多维表格 app_token 后一直显示错误,如何解决?
  • ¥15 vite+vue3+plyr播放本地public文件夹下视频无法加载
  • ¥15 c#逐行读取txt文本,但是每一行里面数据之间空格数量不同
  • ¥50 如何openEuler 22.03上安装配置drbd
  • ¥20 ING91680C BLE5.3 芯片怎么实现串口收发数据
  • ¥15 无线连接树莓派,无法执行update,如何解决?(相关搜索:软件下载)
  • ¥15 Windows11, backspace, enter, space键失灵