原则 ODM 和无模式设计 [英] Doctrine ODM and schema-less design

查看:20
本文介绍了原则 ODM 和无模式设计的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

继续我关于 EAV 的问题,我正在考虑使用 MongoDB存储产品属性.<​​/p>

我将使用 MongoDB(或其他文档数据库)存储此应用程序的目录部分 - 类别、产品及其所有相关信息.

我的问题是,当使用 ODM 时,每个实体都有一个架构,这基本上忽略了使用 NoSQL 数据库的无模式优势,不是吗?

如果这是正确的,为什么有人会使用 ODM?

我发现了一个相关问题,我可以使用哈希实现产品属性功能吗?

解决方案

解决方案是使用@Hash

这是我做的一个非常基本的例子:

id;}公共函数 setName($name){$this->name = $name;}公共函数 getName(){返回 $this->name;}公共函数 addAttribute($name, $value){$key = preg_replace('/[^a-z0-9 \_]/i', '', $name);$key = preg_replace('/s+/i', '_', $key);$key = strtolower($key);$this->attributes[$key] = array('value' =>$value, 'label' => $name);}公共函数 getAttribute($name){返回 $this->attributes[$name];}公共函数 getAttributes(){返回 $this-> 属性;}}

添加一些数据:

setName('酷笔');$pen->addAttribute('重量', 12);$pen->addAttribute('墨水颜色', '红色');$pen->addAttribute('颜色', '黑色');$tv = 新产品();$tv->setName('LED液晶电视');$tv->addAttribute('体重', 12550);$tv->addAttribute('屏幕尺寸', 32);$tv->addAttribute('颜色', '黑色');$dm->persist($pen);$dm->persist($tv);$dm->flush();

然后查询,找到颜色为黑色"且屏幕尺寸大于20的产品:

createQueryBuilder('CatalogueProduct');$products = $query->field('attributes.colour.value')->equals('Black')->field('attributes.screen_size.value')->gte(20)->getQuery()->execute();

我仍然不确定这是否是最好的方法,我的研究仍在进行中.

Continuing on from my question about EAV, I'm considering using MongoDB to store product attributes.

I will store the catalogue part of this app - Categories, Products and all their related information - with MongoDB (or another document database).

My question is, when using an ODM, each entity has a schema, which essentially disregards the schema-less advantage of using a NoSQL database, does it not?

If this is correct, why would anyone use an ODM?

EDIT: I found a related question, could I achieve the product attributes functionality using a Hash?

解决方案

The solution is to use a @Hash

Here is a VERY basic example I did up:

<?php

/**
 * @Document
 */
class Product
{

    /**
     * @Id
     */
    private $id;

    /**
     * @String
     */
    private $name;

    /**
     * @Hash
     */
    private $attributes = array();

    public function getId()
    {
        return $this->id;
    }

    public function setName($name)
    {
        $this->name = $name;
    }

    public function getName()
    {
        return $this->name;
    }

    public function addAttribute($name, $value)
    {
        $key = preg_replace('/[^a-z0-9 \_]/i', '', $name);
        $key = preg_replace('/s+/i', '_', $key);
        $key = strtolower($key);
        $this->attributes[$key] = array('value' =>$value, 'label' => $name);
    }

    public function getAttribute($name)
    {
        return $this->attributes[$name];
    }

    public function getAttributes()
    {
        return $this->attributes;
    }

}

Add some data:

<?php

$pen = new Product();
$pen->setName('Cool Pen');
$pen->addAttribute('Weight', 12);
$pen->addAttribute('Ink Colour', 'Red');
$pen->addAttribute('Colour', 'Black');

$tv = new Product();
$tv->setName('LED LCD TV');
$tv->addAttribute('Weight', 12550);
$tv->addAttribute('Screen Size', 32);
$tv->addAttribute('Colour', 'Black');

$dm->persist($pen);
$dm->persist($tv);

$dm->flush();

Then query, find a product with the colour "Black" and a Screen Size greater than 20:

<?php

$query = $dm->createQueryBuilder('CatalogueProduct');
$products = $query->field('attributes.colour.value')->equals('Black')
                ->field('attributes.screen_size.value')->gte(20)
                ->getQuery()->execute();

I'm still not sure if this is a best way to do this and my research is still ongoing.

这篇关于原则 ODM 和无模式设计的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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