Symfony2,渲染嵌入表单的集合,用于与属性的多对一关系

时间:2014-08-07 23:15:54

标签: php symfony doctrine-orm

我有两个实体,产品功能(例如:宽度,高度,构图......)和另一个中间实体 ProductFeature 两个其他实体之间的关系。

我想在添加新产品时显示表单中的所有功能:

form

table

当我们在 PrestaShop 中添加新产品时,它看起来有点像。我试图使用该集合,但我失败了。

ProductType

class ProductType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{

    $builder
        //.....
        ->add('productFeatures', 'collection', array(
        'type' => new ProductFeatureType(),
        ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project\StoreBundle\Entity\Product'
    ));
}

}

ProductFeatureType

class ProductFeatureType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('value', 'text');
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project\StoreBundle\Entity\ProductFeature'
    ));
}

}

类型特征

class FeatureType extends AbstractType
{

public function buildForm(FormBuilderInterface $builder, array $options)
{
    $builder
        ->add('name', 'text', array(
         'required' => true,
         'label' => 'Feature',
         'attr' => array('class' =>'form-control'),
         ))
    ;
}

public function setDefaultOptions(OptionsResolverInterface $resolver)
{
    $resolver->setDefaults(array(
        'data_class' => 'Project\StoreBundle\Entity\Feature'
    ));
}


}

form.html.twig

                    {% for feature in form.productFeatures %}
                        <div class="form-group">
                            {{ form_label(form.feature, null, { 'label_attr': {'class': 'col-sm-3 control-label'} }) }}
                            <div class="col-sm-7">

                            {{ form_row(feature.name) }}

                            </div>
                            {{ form_errors(form.feature) }}
                        </div>
                    {% endfor %}                            

实体\产品

class Product
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255, nullable=false)
 */
private $name;

 //....

 /**
 * @ORM\OneToMany(targetEntity="Project\StoreBundle\Entity\ProductFeature", mappedBy="product", cascade={"persist"})
 * @ORM\JoinColumn(nullable=true)
 */
 private $productFeatures ;

public function __construct()
{
    $this->categories = new \Doctrine\Common\Collections\ArrayCollection();
    $this->productFeatures = new \Doctrine\Common\Collections\ArrayCollection();
}

//........

/**
 * Add productFeatures
 *
 * @param \Project\StoreBundle\Entity\ProductFeature $productFeatures
 * @return Product
 */
public function addProductFeature(\Project\StoreBundle\Entity\ProductFeature $productFeatures)
{
    $this->productFeatures[] = $productFeatures;

    return $this;
}

/**
 * Remove productFeatures
 *
 * @param \Project\StoreBundle\Entity\ProductFeature $productFeatures
 */
public function removeProductFeature(\Project\StoreBundle\Entity\ProductFeature $productFeatures)
{
    $this->productFeatures->removeElement($productFeatures);
}

/**
 * Get productFeatures
 *
 * @return \Doctrine\Common\Collections\Collection 
 */
public function getProductFeatures()
{
    return $this->productFeatures;
}

实体\特征

class Feature
{
/**
 * @var integer
 *
 * @ORM\Column(name="id", type="integer")
 * @ORM\Id
 * @ORM\GeneratedValue(strategy="AUTO")
 */
private $id;

/**
 * @var string
 *
 * @ORM\Column(name="name", type="string", length=255)
 */
private $name;


/**
* @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Store", inversedBy="features", cascade={"persist"})
* @ORM\JoinColumn(nullable=true)
*/
private $store ;


/**
 * Constructor
 */
public function __construct()
{
    $this->products = new \Doctrine\Common\Collections\ArrayCollection();
}


/**
 * Get id
 *
 * @return integer 
 */
public function getId()
{
    return $this->id;
}

/**
 * Set name
 *
 * @param string $name
 * @return Feature
 */
public function setName($name)
{
    $this->name = $name;

    return $this;
}

/**
 * Get name
 *
 * @return string 
 */
public function getName()
{
    return $this->name;
}


/**
 * Set store
 *
 * @param \Project\StoreBundle\Entity\Store $store
 * @return Feature
 */
public function setStore(\Project\StoreBundle\Entity\Store $store = null)
{
    $this->store = $store;

    return $this;
}

/**
 * Get store
 *
 * @return \Project\StoreBundle\Entity\Store 
 */
public function getStore()
{
    return $this->store;
}

}

实体\ ProductFeature

class ProductFeature
{
/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Product", inversedBy="productFeatures")
 */
private $product;

/**
 * @ORM\Id
 * @ORM\ManyToOne(targetEntity="Project\StoreBundle\Entity\Feature")
 */
private $feature;


/**
 * @var string
 *
 * @ORM\Column(name="value", type="string", length=255)
 */
private $value;


/**
 * Set value
 *
 * @param string $value
 * @return ProductFeature
 */
public function setValue($value)
{
    $this->value = $value;

    return $this;
}

/**
 * Get value
 *
 * @return string 
 */
public function getValue()
{
    return $this->value;
}

/**
 * Set product
 *
 * @param \Project\StoreBundle\Entity\Product $product
 * @return ProductFeature
 */
public function setProduct(\Project\StoreBundle\Entity\Product $product)
{
    $product->addProductFeature($this);

    $this->product = $product;

    return $this;
}

/**
 * Get product
 *
 * @return \Project\StoreBundle\Entity\Product 
 */
public function getProduct()
{
    return $this->product;
}

/**
 * Set feature
 *
 * @param \Project\StoreBundle\Entity\Feature $feature
 * @return ProductFeature
 */
public function setFeature(\Project\StoreBundle\Entity\Feature $feature)
{
    $this->feature = $feature;

    return $this;
}

/**
 * Get feature
 *
 * @return \Project\StoreBundle\Entity\Feature 
 */
public function getFeature()
{
    return $this->feature;
}
}

请帮助

0 个答案:

没有答案