学说2多对多翻译

时间:2022-09-11 14:09:58

I have problem with doctrine 2. I have following db tables : So, Doctrine generate entities which retrive data from desk settings for site, but I need, to retrieve all settings from desk_settings table and overwrite its values from desk_settings_values table using desk_id

我对doctrine 2有问题。我有以下数据库表:所以,Doctrine生成的实体从站点的桌面设置中检索数据,但我需要从desk_settings表中检索所有设置,并使用desk_id从desk_settings_values表覆盖其值

DB image -> https://docs.google.com/file/d/0B7rOFTJGfJwTWEQ3bXZFU1hXZlU/edit?usp=sharing

数据库图像 - > https://docs.google.com/file/d/0B7rOFTJGfJwTWEQ3bXZFU1hXZlU/edit?usp=sharing

Doctrine entities which was generate with script:

使用脚本生成的Doctrine实体:

/**
 * Desk
 *
 * @ORM\Table(name="desk")
 * @ORM\Entity
 */
class Desk
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="code", type="string", length=100, nullable=false)
     */
    private $code;

    /**
     * @var string
     *
     * @ORM\Column(name="description", type="string", length=255, nullable=false)
     */
    private $description;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="PayBox\Entity\DeskSettings", mappedBy="desk")
     */
    private $deskSettings;
}


use Doctrine\ORM\Mapping as ORM;

/**
 * DeskSettings
 *
 * @ORM\Table(name="desk_settings")
 * @ORM\Entity
 */
class DeskSettings
{
    /**
     * @var integer
     *
     * @ORM\Column(name="id", type="integer", nullable=false)
     * @ORM\Id
     * @ORM\GeneratedValue(strategy="IDENTITY")
     */
    private $id;

    /**
     * @var string
     *
     * @ORM\Column(name="setting_key", type="string", length=100, nullable=false)
     */
    private $settingKey;

    /**
     * @var string
     *
     * @ORM\Column(name="setting_value", type="string", length=255, nullable=false)
     */
    private $settingValue;

    /**
     * @var \DateTime
     *
     * @ORM\Column(name="created", type="datetime", nullable=false)
     */
    private $created;

    /**
     * @var \Doctrine\Common\Collections\Collection
     *
     * @ORM\ManyToMany(targetEntity="PayBox\Entity\Desk", inversedBy="deskSettings")
     * @ORM\JoinTable(name="desk_settings_values",
     *   joinColumns={
     *     @ORM\JoinColumn(name="desk_settings_id", referencedColumnName="id")
     *   },
     *   inverseJoinColumns={
     *     @ORM\JoinColumn(name="desk_id", referencedColumnName="id")
     *   }
     * )
     */
    private $desk;
 }

1 个解决方案

#1


1  

From Doctrine documentation:

来自Doctrine文档:

Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.

为什么多对多关联不太常见?因为经常要将其他属性与关联关联,所以在这种情况下引入关联类。因此,直接的多对多关联消失,并被3个参与类之间的一对多/多对一关联所取代。

You need to change your class structure to one-to-many/many-to-one. See this blog.

您需要将类结构更改为一对多/多对一。看到这个博客。

#1


1  

From Doctrine documentation:

来自Doctrine文档:

Why are many-to-many associations less common? Because frequently you want to associate additional attributes with an association, in which case you introduce an association class. Consequently, the direct many-to-many association disappears and is replaced by one-to-many/many-to-one associations between the 3 participating classes.

为什么多对多关联不太常见?因为经常要将其他属性与关联关联,所以在这种情况下引入关联类。因此,直接的多对多关联消失,并被3个参与类之间的一对多/多对一关联所取代。

You need to change your class structure to one-to-many/many-to-one. See this blog.

您需要将类结构更改为一对多/多对一。看到这个博客。