创建教义关联



我需要用适当的名称和字段创建 3 个 heaths,我知道 SQL 查询应该是什么样子,但我不知道如何在原则中做到这一点。请告诉我如何实施。

CREATE TABLE item (id INT AUTO_INCREMENT NOT NULL, name VARCHAR(30) NOT NULL, PRIMARY KEY(id));
CREATE TABLE attribute (id INT AUTO_INCREMENT NOT NULL, alias VARCHAR(30) NOT NULL, name VARCHAR(30) NOT NULL, PRIMARY KEY(id));
CREATE TABLE attr_value (id INT AUTO_INCREMENT NOT NULL, item_id INT NOT NULL, attribute_id INT NOT NULL, value VARCHAR(30), PRIMARY KEY(id), FOREIGN KEY (item_id) REFERENCES item (id), FOREIGN KEY (attribute_id) REFERENCES attribute (id));

底线是你需要制作一个表,其中将有一个项目名称,一个带有属性名称的表和一个链接它们的表,其中有字段:item_id、attribute_id和属性的值。我希望你能帮忙!

更新:

我的项目实体:

class Item
{
    private $id;
    private $name;
    protected $itemValues;
    public function __construct()
    {
        $this->itemValues = new ArrayCollection();
    }
//getters and setters
}

属性实体:

class Attribute
{
    private $id;
    private $alias;
    private $name;
    protected $attrValues;
//getters and setters
}

AttrValue 实体:

class AttrValue
{
    private $id;
    private $value;
    protected $attribute;
    protected $item;
//getters and setters
}

Item.orm.yml:

AppBundleEntityItem:
    type: entity
    oneToMany:
        itemValues:
            targetEntity: AppBundleEntityAttrValue
            mappedBy: item
    table: items
    repositoryClass: AppBundleRepositoryItemRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        name:
            type: string
            length: 255

Attribute.orm.yml:

AppBundleEntityAttribute:
    type: entity
    oneToMany:
            attrValues:
                targetEntity: AppBundleEntityAttrValue
                mappedBy: attribute
    table: attribute
    repositoryClass: AppBundleRepositoryAttributeRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        alias:
            type: string
            length: 255
        name:
            type: string
            length: 255

AttrValue.orm.yml:

AppBundleEntityAttrValue:
    type: entity
    manyToOne:
        item:
            targetEntity: AppBundleEntityItem
            inversedBy: itemValues
            joinColumn:
                nullable: true
        attribute:
            targetEntity: AppBundleEntityAttribute
            inversedBy: attrValues
            joinColumn:
                nullable: true
    table: null
    repositoryClass: AppBundleRepositoryAttrValueRepository
    id:
        id:
            type: integer
            id: true
            generator:
                strategy: AUTO
    fields:
        value:
            type: string
            length: 255

但是当我想从一个项目(或所有项目(中获取所有属性时,我会得到这个 JSON 数组:

{
    "id": 1,
    "name": "bike",
    "item_values": [
        {
            "id": 1,
            "value": "30",
            "attribute": {
                "id": 1,
                "alias": "size",
                "name": "size",
                "attr_values": []
            }
        }
    ]
}

但是我想得到这样的东西,怎么能做到这一点?

{
    "id": 1,
    "name": "bike",
    "attributes": [
            {
                "id": 1,
                "alias": "size",
                "name": "size",
                "attr_values": [
                          {
                          "id": 1,
                          "value": "30"
                          }
                  ]
            }
    ]
}

这看起来像是项目和属性之间的多对多关系,但项目不直接与属性链接,有一个交汇点实体/表 (attr_value( 它使用原则链接项目和属性,您需要定义您的关系,例如

Items -> One to Many -> AttrValue
Attributes -> One to Many -> AttrValue
AttrValue -> Many To One -> Items 
AttrValue -> Many To One -> Attributes 

示例实体定义如下所示

class Items
{
    // ...
    /**
     * @ORMOneToMany(targetEntity="AppEntityAttrValue", mappedBy="item")
     */
    private $itemValues;
}
class Attributes
{
    // ...
    /**
     * @ORMOneToMany(targetEntity="AppEntityAttrValue", mappedBy="attribute")
     */
    private $attributeValues;
}
class AttrValue
{
    /**
     * @ORMManyToOne(targetEntity="AppEntityItems", inversedBy="itemValues")
     * @ORMJoinColumn(nullable=true)
     */
     private $item;
    /**
     * @ORMManyToOne(targetEntity="AppEntityAttributes", inversedBy="attributeValues")
     * @ORMJoinColumn(nullable=true)
     */
     private $attribute
}

有关更多信息,我建议查看文档

相关内容

  • 没有找到相关文章