Symfony2与列的实体关系

时间:2013-12-09 21:51:26

标签: symfony join orm doctrine-orm entities

我正在使用symfony2构建我的第一个应用程序。我想用实体创建这个数据库布局。

#document

document_id | INT | AI | PK

document_name | STRING

服务|关系MANY2MANY

...

#service

service_id | INT | AI | PK

service_name | STRING

...

* #_table_document_service *

document_id | INT

service_id | INT

金额| INT |默认1

创建实体文档和服务不是问题。要创建文档和服务之间的关系,我将使用像这样的ManyToMany-Relation:

 /**
 * @ORM\ManyToMany(targetEntity="Services")
 * @ORM\JoinTable(name="_table_document_service",
 *     joinColumns={@ORM\JoinColumn(name="document_id", referencedColumnName="id")},
 *     inverseJoinColumns={@ORM\JoinColumn(name="service_id", referencedColumnName="id")}
 * )
 */
 private $services;

但是我希望在连接表中还应该有一个名为amount的列。我怎样才能做到这一点?

2 个答案:

答案 0 :(得分:1)

考虑到你想在一个关系上添加额外的字段,那么它不再是一种关系,而是显而易见的 - “设计性”地说 - 一个关联对象。

因此,您在“表格文档服务”表中想要的是

文件-------表文件服务--------服务

这样做意味着两件事:

Document ---- Table文档服务和Table文档服务之间的多对一关联--------服务

基于这些关联的表文档服务实体中的一种“复合键”(然后您的主键成为这些对象之间关系的组合。

以下是一些示例:as you can see in the official doctrine 2 documentation您可以根据多个人使用复合键进行协商,如下所示。

编辑:这里有一堆代码,试图按照你的例子。

use Doctrine\ORM\Mapping\Entity, Doctrine\ORM\Mapping\Id, Doctrine\ORM\Mapping\Column, Doctrine\ORM\Mapping\ManyToOne, Doctrine\ORM\Mapping\OneToMany;

/** @Entity */
class Document
{
    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id;

    /** @OneToMany(targetEntity="OrderItem", mappedBy="document") */
    protected $tableDocumentService;

    /** your attributes here, it isn't important here **/

    public function __construct(Customer $customer)
    {
        $this->tableDocumentService = new ArrayCollection();
        // some logic here if you need it
    }   
}

/** @Entity */
class Service
{
    /** @Id @Column(type="integer") @GeneratedValue */
    protected $id;

    /** @Column(type="string) *§
    protected $name;
}

/** @Entity */
class TableDocumentService
{
    /** @Id @ManyToOne(targetEntity="Document") */
    protected $document;

    /** @Id @ManyToOne(targetEntity="Service") */
    protected $service;

    /** @Column(type="integer") */
    protected $amount = 1;

}

我可以补充一点,我想,你可以在Service类中添加另一个OneToMany关联,就像doc为Document实体做的那样,类似

/** @OneToMany(targetEntity="OrderItem", mappedBy="service") */
protected $tableDocumentService;

答案 1 :(得分:1)

@YoannCh的答案是正确的,但正确的注释是

for Document Entity:

    /**
 * @ORM\Entity
 * @ORM\Table(name="Document")
 */
class Document
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

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

    /**
     * @ORM\OneToMany(
     *  targetEntity="DocumentService", mappedBy="documentId")
     */
    private $documentService;
    //...

服务实体是:

/**
 * @ORM\Entity
 * @ORM\Table(name="service")
 */
class Service
{
    /**
     * @ORM\Column(type="integer")
     * @ORM\Id
     * @ORM\GeneratedValue
     */
    private $id;

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

    /**
     * @ORM\OneToMany(
     *  targetEntity="DocumentService", mappedBy="serviceId", 
     *  )
     */
    private $documentService;
    // ...

DocumentService Entity(文档和服务之间的连接表)是:

/**
 * @ORM\Entity
 * @ORM\Table(name="document_service")
 */
class DocumentService
{ 
    /**
     * @ORM\ManyToOne(targetEntity="Document", inversedBy="documentService")
     * @ORM\JoinColumn(name="document_id", referencedColumnName="id")
     */
    private $documentId;

    /**
     * @ORM\ManyToOne(targetEntity="Service", inversedBy="documentService")
     * @ORM\JoinColumn(name="company_id", referencedColumnName="id")
     */
    private $serviceId;

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

这个article讨论了你用例子提出的同样问题