Symfony / Doctrine:实体的变化 [英] Symfony/Doctrine: variations of entities

查看:124
本文介绍了Symfony / Doctrine:实体的变化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对不起,对于模糊,但是我对如何描述这个数据库问题感到困惑。它的一部分是数据库设计问题,另一部分是如何有效地使用Doctrine ORM和Symfony的Form Builder引擎。让我试着解释一下:



我的数据库包含手机维修。每个维修只能用于一个手机。然而,一些手机有不同的颜色。虽然一些修理(如电池更换)适用于所有设备,但有些(如显示器更换)根据设备的颜色具有不同的价格。在修复菜单中,可以指定每个修复的颜色(选择一个,许多或全部)。



我当前的数据库设计如下所示:





但是这不允许我从修复实体访问设备的颜色,因为这里颜色与设备有直接的关系,不能修复。所以我需要一个更复杂的数据库,它仍然将iphone视为一个单一的产品,但是可以将各种修复与各种变种进行映射,以及将所有变体的通用修复映射到。



我应该使用由3个ID组成的连接表(repair_device_color)吗?似乎不漂亮而与Doctrine ORM映射的最有效的方法是什么?我应该使用数据继承

解决方案

我想给你一个很好的答案,我有一点时间来创建我的答案。这可能不是完美的,但它会给你一个想法。你给出的digram不是ER图。



我在Excel中创建了这个,并复制并保存为图片。表名和ORM注释很重要。





例如,您将以标准方式创建一个设备实体,其文件名称为src / AppBundle / Entity / Device.php。对于数组,您需要添加到Entity类中的__construct()函数。



作为一个例子,这就是设备实体的代码:

 <?php 

// src / AppBundle / Entity / Device.php
命名空间AppBundle\Entity;

使用Doctrine\Common\Collections\ArrayCollection;
使用Doctrine\ORM\Mapping作为ORM;

/ **
* @ ORM\Entity
* @ ORM\Table(name =device)
* /
类设备
{
/ **
* @ ORM\Id
* @ ORM\Column(name =device_id,type =integer)
* @ ORM\GeneratedValue(strategy =AUTO)
* /
protected $ device_id;

/ **
* @ ORM\Column(name =model,type =string,unique = true)
* /
protected $模型;

/ **
* @ ORM\OneToMany(targetEntity =Part,mappedBy =part_device)
* /
protected $ parts = null;

...

public function __construct(){
//这是一个零件数组。
$ this-> parts = new ArrayCollection();

//也创建其他数组。
$ this-> repairs = new ArrayCollection();
$ this-> customers = new ArrayCollection();
}

/ *
*这个命令自动生成。
* php bin / console doctrine:generate:entities AppBundle / Entity / Device
* /
public function addPart($ part){
$ this-> parts [] = $一部分;
}
}

希望你能弄清楚其余的。但我认为这是一个好的开始。


Sorry for the vagueness, but I'm confused as to how to describe this database problem. Part of it is a database design problem, the other part is how to efficiently use the Doctrine ORM and Symfony's Form builder engine for this. Let me try to explain:

My database contains repairs for cell phones. Each repair is for one cellphone only. However, some cellphones come in different colours. While some repairs (like battery replacement) are applicable to all devices, some (like display replacement) have different prices depending on the colour of the device. It should be possible to, in the repair menu, specify the colour of each repair (choose one, many or all).

My current DB design looks like this:

But this does not allow me to access the colour of the device from the repair Entity, as here colour has a direct relationship to device, not to repair. So I need a more complex database which still treats the iphone as a single product, but can map various repairs to its many variants as well as map universal repairs to all variants.

Should I use a join table (repair_device_color) consisting of 3 ids? Seems not pretty. And what's the most efficient way to map this with the Doctrine ORM? Should I use data inheritance?

解决方案

I wanted to give you a good answer, so it took me a bit of time to create my answer. This may not be perfect, but it will give you an idea. The digram you gave is not an ER Diagram by the way.

I created this in Excel, and copied and saved as a picture. The table names and ORM annotation are important.

For example you will create an "Device" Entity with the file name as "src/AppBundle/Entity/Device.php" in the standard way. For the arrays, you need to add to a __construct() function in the Entity class.

As an example, this is what the code would look like for the Device Entity:

<?php

// src/AppBundle/Entity/Device.php
namespace AppBundle\Entity;

use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;

/**
 * @ORM\Entity
 *  @ORM\Table(name="device")
 */
class Device
{
    /**
     * @ORM\Id
     * @ORM\Column(name="device_id", type="integer")
     * @ORM\GeneratedValue(strategy="AUTO")
     */
    protected $device_id;

        /**
     * @ORM\Column(name="model", type="string", unique=true)
     */
    protected $model;

    /**
     * @ORM\OneToMany(targetEntity="Part", mappedBy="part_device")
     */
    protected $parts = null;

...

    public function __construct(){
        // This is an array of parts.
        $this->parts = new ArrayCollection();

        // Create the other arrays too.
        $this->repairs = new ArrayCollection();
        $this->customers = new ArrayCollection();
    }

    /*
     * This should automatically be generated by the command.
     * php bin/console doctrine:generate:entities AppBundle/Entity/Device
     */
    public function addPart($part){
        $this->parts[] = $part;
    }
}

Hopefully you can figure out the rest. But I think this is a good start.

这篇关于Symfony / Doctrine:实体的变化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆