我正在使用 Symfony2 和 Doctrine2 创建一个电子商务捆绑包。我正在将 EAV 方法应用于产品功能和无限功能的产品价值。为此,我有三个基本实体:Product、FeatureKind 和 FeatureValues。
- FeatureKind 与具有 OneToMany 单向关系的 FeatureValues 连接。
- 产品通过多对多关系连接到 FeatureKind。
问题是我需要 FeatureType 作为标签,它的各种值作为产品表单中的选择字段。我已经设法在产品表单中获得了特征种类和相关值,但我不知道如何将它们变成选择字段。
以下是所有三个实体、控制器和表单代码以及我的代码的结果。
注意:我已经从代码中删除了多余的东西以保持简短。
产品.php
namespace Webmuch\ProductBundle\Entity;
/**
* @ORM\Table()
* @ORM\Entity
*/
class Product
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @ORM\Column(name="title", type="string", length=255)
*/
private $title;
/**
* @ORM\ManyToMany(targetEntity="FeatureKind", inversedBy="product", cascade={"persist"})
* @ORM\JoinTable(name="product_featurekind")
**/
private $featurekind;
}
FeatureKind.php
namespace Webmuch\ProductBundle\Entity;
/**
* @ORM\Table(name="feature_kind")
* @ORM\Entity
*/
class FeatureKind
{
/**
* @ORM\Id
* @ORM\Column(name="id", type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="name", type="string", length=50)
*/
protected $name;
/**
* @ORM\ManyToMany(targetEntity="FeatureValue")
* @ORM\JoinTable(name="feature_kind_value",
* joinColumns={@ORM\JoinColumn(name="kind_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="value_id", referencedColumnName="id", unique=true)}
* )
**/
protected $values;
}
特征值.php
namespace Webmuch\ProductBundle\Entity;
/**
* @ORM\Table()
* @ORM\Entity
*/
class FeatureValue
{
/**
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(name="value", type="string", length=100)
*/
protected $value;
}
产品控制器.php
public function newAction(Request $request)
{
$entity = new Product();
$em = $this->getDoctrine()->getEntityManager();
$features = $em->getRepository('ProductBundle:FeatureKind')->findAll();
foreach($features as $feature)
{
$featurekind = new FeatureKind();
$featurekind->setTitle($feature->getTitle());
foreach($feature->getValue() as $value ){
$featurekind->getValue()->add($value);
}
$entity->getFeaturekind()->add($featurekind);
}
$form = $this->createForm(new ProductType(), $entity);
if ('POST' === $request->getMethod()) {
$form->bindRequest($request);
if ($form->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('product_show', array(
'id' => $entity->getId()
)));
}
}
return $this->render('ProductBundle:Product:new.html.twig', array(
'form' => $form->createView()
));
}
产品类型.php
namespace Webmuch\ProductBundle\Form;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilder;
class ProductType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('featurekind', 'collection', array('type' => new FeatureKindType()))
->getForm();
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Webmuch\ProductBundle\Entity\Product',
'required' => true
);
}
public function getName()
{
return 'product';
}
}
FeatureKindType.php
namespace Webmuch\ProductBundle\Form;
class FeatureKindType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title')
->add('value','collection', array(
'type' => new FeatureValueType(),
'allow_add'=>true))
->getForm();
}
public function getDefaultOptions(array $options)
{
return array(
'data_class' => 'Webmuch\ProductBundle\Entity\FeatureKind',
);
}
public function getName()
{
return 'featurekind';
}
}
编辑:
经过几天的工作,我现在陷入了一系列简单的功能及其各自的多个值:
Array
(
[Color] => Array
(
[Red] => Red
[Green] => Green
)
[Size] => Array
(
[Large] => Large
[Medium] => Medium
[Small] => Small
)
[Sleeve Style] => Array
(
[Half Sleeved] => Half Sleeved
[Full Sleeved] => Full Sleeved
[Cut Sleeves] => Cut Sleeves
)
)
我尝试按如下方式创建表单:$this->choices包含数组。
$builder
->add('name')
->add('slug')
->add('active')
;
foreach ($this->choices as $choice) {
$builder->add('featurekind', 'choice', array(
'required' => 'false',
'choices' => $choice,
'empty_value' => 'Choose an option',
'empty_data' => null
));
}
$builder->getForm();
以上不适用于属性$featurekind。我得到错误:
Notice: Object of class Doctrine\Common\Collections\ArrayCollection could not be converted to int in /vagrant/project/vendor/symfony/symfony/src/Symfony/Component/Form/Extension/Core/ChoiceList/ChoiceList.php line 457
尽管如果表单字段附加到任何未关联的属性,例如:$name,它仍然只为循环的最后一次迭代创建一个表单字段。
我没有选择。