1

another Symfony form Question. I have an entity "Product". This entity has related featureTypes. E.g. Product "iPhone" has Featuretpyes "RAM, Display, Prozessor". Also I have an entity "Feature". Each Feature has a featureType.

Well, what do i want to know?

I need a form for my product, where i can choose one feature for each related featureTypes to my product.

Class Product:

/**
 * @var FeatureType[]|ArrayCollection
 *
 * @ORM\ManyToMany(targetEntity="AppBundle\Entity\FeatureType", inversedBy="products", cascade={"persist"})
 * @ORM\JoinTable(name="products_featureTypes")
 */
private $featureTypes;

public function __construct()
{
    $this->variants = new ArrayCollection();
    $this->featureTypes = new ArrayCollection();
}

Class FeatureType:

 private $products;

/**
 * @var Feature[]
 *
 * @ORM\OneToMany(targetEntity="AppBundle\Entity\Feature", mappedBy="featureType")
 */
private $features;

public function __construct()
{
    $this->features = new ArrayCollection();
    $this->products = new ArrayCollection();
}

Class Feature:

/**
 * @var FeatureType
 *
 * @ORM\ManyToOne(targetEntity="AppBundle\Entity\FeatureType", inversedBy="features")
 * @ORM\JoinColumn(name="featureType_id", referencedColumnName="id")
 */
private $featureType;

Maybe one of you knows, how to create a for-loop in my formBuilder or any other way?

Sorry for my bad english..

EDIT: My Controller looks like this now:

$featureTypes = $product->getFeatureTypes();
    $featureRepo = $this->getDoctrine()->getRepository(Feature::class);
    $features = $featureRepo->getFeaturesByFeatureType($featureTypes)->getResult();

    $form = $this->createForm(VariantType::class, $variant, [
        'action' => $this->generateUrl('edit_variant', [
            'product' => $product->getId(),
            'variant' => $variant->getId(),
        ]),
        'features' => $features
    ]);

My Form:

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', TextType::class)
        ->add('price', MoneyType::class)
        ->add('stock', IntegerType::class)
        ->add('visible', CheckboxType::class, [
            'required' => false,
            'mapped' => false,
        ]);

        foreach($options['features'] as $index=>$feature){
            $builder->add('feature' . $index, EntityType::class, [
                'class' => Feature::class,
                'choice_label' => function(Feature $feature) {
                    return $feature->getFeatureType()->getName();
                },
                'mapped' => false,
            ]);
        }
    $builder
        ->add('submit', SubmitType::class)
    ;
}

I need to sort my array, that all features with the same type, are in one key. Can someone help?

My current array with the features looks like this:

array (size=4)  0 => 
object(AppBundle\Entity\Feature)[855]
  private 'id' => int 9
  private 'unit' => string 'GB' (length=2)
  private 'value' => string '8' (length=1)
  private 'featureType' => 
    object(AppBundle\Entity\FeatureType)[834]
      private 'id' => int 1
      private 'name' => string 'Arbeitsspeicher' (length=15)
      private 'products' => 
        object(Doctrine\ORM\PersistentCollection)[846]
          ...
      private 'features' => 
        object(Doctrine\ORM\PersistentCollection)[847]
          ...
  public 'variant' => null 1 => 
  object(AppBundle\Entity\Feature)[857]
  private 'id' => int 10
  private 'unit' => string 'GB' (length=2)
  private 'value' => string '16' (length=2)
  private 'featureType' => 
    object(AppBundle\Entity\FeatureType)[834]
      private 'id' => int 1
      private 'name' => string 'Arbeitsspeicher' (length=15)
      private 'products' => 
        object(Doctrine\ORM\PersistentCollection)[846]
          ...
      private 'features' => 
        object(Doctrine\ORM\PersistentCollection)[847]
          ...
  public 'variant' => null 2 => 
object(AppBundle\Entity\Feature)[858]
  private 'id' => int 13
  private 'unit' => string 'Test1' (length=5)
  private 'value' => string 'Test1' (length=5)
  private 'featureType' => 
    object(AppBundle\Entity\FeatureType)[849]
      private 'id' => int 7
      private 'name' => string 'FeatureType' (length=11)
      private 'products' => 
        object(Doctrine\ORM\PersistentCollection)[850]
          ...
      private 'features' => 
        object(Doctrine\ORM\PersistentCollection)[852]
          ...
  public 'variant' => null 3 => 
object(AppBundle\Entity\Feature)[859]
  private 'id' => int 14
  private 'unit' => string 'Test2' (length=5)
  private 'value' => string 'Test2' (length=5)
  private 'featureType' => 
    object(AppBundle\Entity\FeatureType)[849]
      private 'id' => int 7
      private 'name' => string 'FeatureType' (length=11)
      private 'products' => 
        object(Doctrine\ORM\PersistentCollection)[850]
          ...
      private 'features' => 
        object(Doctrine\ORM\PersistentCollection)[852]
          ...
  public 'variant' => null

0 Answers0