Reputation: 693
Please, could you help me?
I´m looking for the best way, how to add to entity (Product) some parametrs with values.
For example: Product T-Shirt would have parametrs: size: XXL, color: red, material: cotton. How to make tables to have the best result - easy adding parametrs to product and easy filtering products by parametrs.
Thank you for your opinions.
Upvotes: 0
Views: 89
Reputation: 822
You have two options:
A OneToMany relationship with another entity (recommended)
You may create a new entity called ProductProperty and declare a OneToMany relationship from Product to ProductProperty, like this:
The Product entity
/**
* @ORM\Entity
*/
class Product
{
/**
* @ORM\OneToMany(targetEntity="ProductProperty", mappedBy="product", cascade={"remove"})
*/
public $properties;
}
The ProductProperty entity
/**
* @ORM\Entity
*/
class ProductProperty
{
/**
* @ORM\ManyToOne(targetEntity="Product", inversedBy="properties")
* @ORM\JoinColumn(name="product_id", referencedColumnName="id")
*/
public $product;
}
Create an array property in entity Product
Doctrine 2 supports arrays (it serializes the array into a TEXT column). Create a property that behaves like an array:
/**
* @ORM\Entity
*/
class Product
{
/** @ORM\Column(type="array") */
public $properties;
public function __construct()
{
$this->properties = []; //new PHP array notation, if using older PHP use array()
}
}
Upvotes: 1