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 matlab答疑 关于海上风电的爬坡事件检测
  • ¥88 python部署量化回测异常问题
  • ¥30 酬劳2w元求合作写文章
  • ¥15 在现有系统基础上增加功能
  • ¥15 远程桌面文档内容复制粘贴,格式会变化
  • ¥15 关于#java#的问题:找一份能快速看完mooc视频的代码
  • ¥15 这种微信登录授权 谁可以做啊
  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄