Doctrine ODM和无架构设计

Cob*_*bby 5 mongodb nosql mongomapper doctrine-orm doctrine-odm

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

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

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

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

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

Cob*_*bby 5

解决方案是使用@Hash

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

<?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;
    }

}
Run Code Online (Sandbox Code Playgroud)

添加一些数据:

<?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();
Run Code Online (Sandbox Code Playgroud)

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

<?php

$query = $dm->createQueryBuilder('Catalogue\Product');
$products = $query->field('attributes.colour.value')->equals('Black')
                ->field('attributes.screen_size.value')->gte(20)
                ->getQuery()->execute();
Run Code Online (Sandbox Code Playgroud)

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