Doctrine ODM and schema without design

Continuing my question about EAV , I am considering using MongoDB to store product attributes.

I will save part of the catalog of this application - Categories, Products and all their relevant information - with MongoDB (or another document database).

My question is that when using ODM, every object has a schema that essentially ignores the advantage of the schema without using a NoSQL database, right?

If this is correct, why would anyone use ODM?

EDIT: I found a question, can I implement the functionality of product attributes using Hash?

+5
source share
2 answers

The solution is to use @Hash

Here is the VERY basic example I made:

<?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 run the query, find the product with the color "Black" and a screen size greater than 20:

<?php

$query = $dm->createQueryBuilder('Catalogue\Product');
$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 the best way to do this, and my research is still ongoing.

+5
source

Although it does not impose anything, it is good practice to have a basic scheme for the collection. Almost all ODMs allow you to add fields not specified in the class. Assuming the application allows this, you can also leave the field values.

, , . , , . ODM Object , .

. SQL- , . . .

+1

All Articles