【问题标题】:Symfony2 entity relations with columnSymfony2与列的实体关系
【发布时间】:2013-12-09 21:51:26
【问题描述】:

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

#文档

document_id |情报 |人工智能 | PK

文档名称 |字符串

服务 |关系多对多

...

#服务

service_id |情报 |人工智能 | PK

服务名称 |字符串

...

*# _table_document_service*

document_id |整数

service_id |整数

金额 |情报 |默认 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;

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

【问题讨论】:

    标签: symfony join orm doctrine-orm entities


    【解决方案1】:

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

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

    文档--------表格文档服务--------服务

    这样做意味着两件事:

    Document ---- Table document service 和 Table document service -------- Service 之间的多对一关联

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

    以下是一些示例: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 关联,就像文档为 Document 实体所做的那样,类似于

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

    【讨论】:

      【解决方案2】:

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

      对于文档实体:

          /**
       * @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 讨论了与示例相同的问题

      【讨论】:

      • 注释仅取决于您的导入(使用语句)。如果您按照use Doctrine\ORM\Mapping as ORM; 导入 ORM 注释,那么您的精度是正确的。如果您将它们单独导入为“使用使用 Doctrine\ORM\Mapping\Entity, Doctrine\ORM\Mapping\Id, Doctrine\ORM\Mapping\Column, Doctrine\ORM\Mapping\ManyToOne, Doctrine\ORM\Mapping\OneToMany;'对于更精确的导入,我的回答效果很好。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2012-08-30
      • 2012-07-23
      • 1970-01-01
      • 2016-07-14
      • 2017-04-20
      相关资源
      最近更新 更多