【问题标题】:Symfony2 Entity ManyToMany Relation Data StructureSymfony2 实体多对多关系数据结构
【发布时间】:2023-03-05 19:52:01
【问题描述】:

我有以下设置:

Entity: 'Customers'
Entity: 'Accounts'

这些实体处于@ManyToMany 关系中。

所以我有以下数据库表:

customers
accounts
customers_accounts

现在我需要为每个帐户的每个客户保存数据。示例:

有一位客户“汤姆”。两个帐户“Ben”和“Eric”负责客户“Tom”。现在我需要保存帐户“Ben”是否已经与客户“Tom”通话。我还需要为帐户“Eric”保存相同的状态。

在这里组织数据库的最佳方式是什么?最好的事情是表“customers_accounts”中有一个额外的列。那可能吗?还有哪些其他选择?

感谢您的帮助!


只是为了向您展示实体如何相互链接:

/**
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\Customer", inversedBy="accounts", cascade={"persist"})
 * @ORM\JoinTable(name="customers_accounts")
 **/
protected $customers;

【问题讨论】:

    标签: database postgresql symfony doctrine-orm


    【解决方案1】:

    ManyToMany 中,您不能在联结表中添加其他字段,即customers_accounts,要为联结表添加其他字段,您必须调整映射,因为创建一个联结实体将指向您的客户和帐户ManyToOne 方式的实体,您的客户和帐户实体将以OneToMany 方式指向您的联结实体

            OneToMany                 OneToMany
          ----------->            <------------
    Customer       CustomerHasAccounts       Accounts
          <----------             ------------>
            ManyToOne                ManyToOne
    

    客户实体

    /**
     * Customer
     * @ORM\Table(name="customer")
     * @ORM\Entity
     */
    class Customer
    {
        /**
         * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="customers",cascade={"persist","remove"} )
         */
        protected $hasAccounts;
    
    }
    

    账户实体

    /**
     * Accounts
     * @ORM\Table(name="accounts")
     * @ORM\Entity
     */
    class Accounts 
    {
        /**
         * @ORM\OneToMany(targetEntity="NameSpace\YourBundle\Entity\CustomerHasAccounts", mappedBy="acccounts",cascade={"persist","remove"} )
         */
        protected $hasCustomers;
    
    }
    

    CustomerHasAccounts 实体

    /**
     * CustomerHasAccounts
     * @ORM\Table(name="customers_accounts")
     * @ORM\Entity
     */
    class CustomerHasAccounts
    {
    
        /**
         * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Accounts", cascade={"persist"}, fetch="LAZY")
         * @ORM\JoinColumn(name="acccount_id", referencedColumnName="id")
         */
        protected $acccounts;
    
        /**
         * @ORM\ManyToOne(targetEntity="NameSpace\YourBundle\Entity\Customer", cascade={"persist","remove"} , fetch="LAZY" )
         * @ORM\JoinColumn(name="customers_id", referencedColumnName="id",nullable=true)
         */
        protected $customers;
    
    
        /**
         * 
         * @ORM\Column(name="status", type="string")
         */
        protected $status;
    
    }
    

    【讨论】:

    • 我明白了:所以我必须实现一个'客户->客户帐户数据
    • @Chris。看看我更新的答案,我希望它能清除这个概念
    • 感谢您的努力,一旦我得到它(可能是今天晚些时候),我会测试一切并接受答案,以防一切正常。
    【解决方案2】:

    您必须通过 OneToMany-ManyToOne 实体转换您的实体之间的多对多关系,从而在您的 2 个多对多实体之间建立链接,该实体将拥有与该关系相关的所有数据。

    客户

    Class Customer
    {
        /**
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="societe", cascade={"Persist"})
         */
        private $customerAccounts;
    }
    

    帐户

    Class Account
    {
        /**
         * @ORM\OneToMany(targetEntity="AppBundle\Entity\CustomerAccount", mappedBy="account", cascade={"Persist"})
         */
        private $customerAccounts;
    }
    

    客户帐户

    Class CustomerAccount
    {
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Customer", inversedBy="customerAccounts")
         */
        private $societe;
    
        /**
         * @ORM\Id
         * @ORM\ManyToOne(targetEntity="AppBundle\Entity\Account", inversedBy="customerAccounts")
         */
        private $contact;
    
        /**
         * @var boolean
         *
         * @ORM\Column(name="alreadySpoken", type="boolean")
         */
        private $alreadySpoken;
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2015-10-08
      • 2013-04-25
      • 2013-09-29
      • 1970-01-01
      • 2015-12-22
      • 1970-01-01
      • 2014-11-20
      相关资源
      最近更新 更多