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 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100