3. VariantGenerator¶
This is used to create all possible combinations of an object’s options and to create Variant models from them.
Example:
If an object such as a t-shirt has 2 options - Color and Size - with 3 possible values per option, then the generator will create 9 variants and assign them to the object.
The generator will ignore invalid variants or variants that already exist.
T-Shirt Options
| Colors | Size |
| Black | Small |
| White | Medium |
| Red | Large |
Possible T-Shirt Variants
These variants should be generated by the VariantGenerator from the options above.
- Black, Small
- Black, Medium
- Black, Large
- White, Small
- White, Medium
- White, Large
- Red, Small
- Red, Medium
- Red, Large
The Code
use Sylius\Component\Resource\Repository\RepositoryInterface;
use Sylius\Component\Variation\Generator\VariantGenerator;
use Sylius\Component\Variation\Model\VariableInterface;
/** @var RepositoryInterface $variantRepository */
$variantRepository = /*...*/
/**
* From the example above, this would be the T-Shirt product.
*
* @var VariableInterface $variable
*/
$variable = /*...*/
$generator = new VariantGenerator($variantRepository);
// Generate all possible variants if they don't exist currently.
$generator->generate($variable)
Примечание
The variant generator implements Sylius\Component\Variation\Generator\VariantGeneratorInterface.