PHP code example of ufo-tech / doctrine-eav-bundle
1. Go to this page and download the library: Download ufo-tech/doctrine-eav-bundle library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
use Doctrine\ORM\Mapping as ORM;
use Ufo\EAV\Entity\EavEntity;
#[ORM\Entity]
#[ORM\Table(name: 'product')]
class Product extends EavEntity
{
}
use Ufo\EAV\Entity\Value;
use Ufo\EAV\Services\EavFactoryService;
$product = new Product('T-shirt', article: 'TS-001');
$spec = $product->getMainSpecification();
// $factory is EavFactoryService (autowired)
$spec->setValue($factory->valueForParam($spec, 'color', 'red'));
$spec->setValue($factory->valueForParam($spec, 'weight', 0.25));
// option values
$size = $factory->getParam('size');
$spec->setValue(Value::create($size, [$factory->getOption($size, 'M')]));
// a variant that inherits all values from the main specification
$variant = $product->addSpecification('T-shirt XL', article: 'TS-001-XL');
$em->persist($product);
$em->flush();
// localized value (after the base value is saved)
$spec->setValue($factory->valueForParam($spec, 'color', 'червоний', locale: 'uk'));
$em->flush();
use Ufo\EAV\Services\FilterDataFactory;
use Ufo\EAV\Services\FilterService;
// "param1=value1,value2;param2=value3"
$filterData = $filterDataFactory->fromFilterRow('color=red,blue;size=M');
// or: $filterDataFactory->fromArray(['color' => ['red', 'blue']]);
$specs = $filterService->applyFilterData($filterData)->getSpecs(page: 1, limit: 20);
$filters = $filterService->getCommonFilters(); // available params, values and counts
use Ufo\EAV\Services\SearchService;
$results = $searchService->search('red', $filterData);