我必须从Product和集合创建表单,文本字段为TechChar
中的数组实体
产品
<?php
namespace Homework\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Homework\Bundle\Entity\TechChar;
use Doctrine\Common\Collections\ArrayCollection;
/**
* @ORM\Entity
* @ORM\Table(name="products")
*/
class Product {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", name="name")
*/
protected $name;
/**
* @ORM\Column(type="string", name="brand")
*/
protected $brand;
/**
* @ORM\Column(type="decimal", name="price")
*/
protected $price;
/**
* @ORM\Column(type="integer", name="is_active")
*/
protected $isActive;
/**
* @ORM\ManyToMany(targetEntity="TechChar")
* @ORM\JoinTable(name="products_tech_char",
* joinColumns={@ORM\JoinColumn(name="product_id", referencedColumnName="id")},
* inverseJoinColumns={@ORM\JoinColumn(name="tech_char_id", referencedColumnName="id")}
* )
*/
protected $techChar;
public function __construct() {
$this->techChar = new ArrayCollection();
}
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function getBrand() {
return $this->brand;
}
public function getPrice() {
return $this->price;
}
public function getIsActive() {
return $this->isActive;
}
public function getTechChar() {
return $this->techChar;
}
public function setName($name) {
$this->name = $name;
}
public function setBrand($brand) {
$this->brand = $brand;
}
public function setPrice($price) {
$this->price = $price;
}
public function setIsActive($isActive) {
$this->isActive = $isActive;
}
public function setTechChar($techChar) {
$this->techChar = $techChar;
}
}
TechChar
<?php
namespace Homework\Bundle\Entity;
use Doctrine\ORM\Mapping as ORM;
/**
* @ORM\Entity
* @ORM\Table(name="tech_char")
*/
class TechChar {
/**
* @ORM\Column(type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", name="name")
*/
protected $name;
/**
* @ORM\OneToMany(targetEntity="Product", mappedBy="id")
*/
protected $product;
public function getId() {
return $this->id;
}
public function getName() {
return $this->name;
}
public function setName($name) {
$this->name = $name;
}
public function getProduct() {
return $this->product;
}
public function setProduct($product) {
$this->product = $product;
}
}
我也创建了FormType
namespace Homework\Bundle\Forms;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
class FormType extends AbstractType {
public function buildForm(FormBuilderInterface $builder, array $options) {
$em = $this->getDoctrine()->getManager();
$techChar = $em->getRepository("HomeworkBundle:TechChar")->findAll();
echo '<pre>';
print_r($techChar);
echo '</pre>';
$builder->add('Namesss');
}
public function setDefaultOptions(OptionsResolverInterface $resolver) {
$resolver->setDefaults(array(
'data_class' => 'HomeWork\Bundle\Entity\TechChar',
));
}
public function getName() {
return 'technicals';
}
}
和PanelColntroller
class PanelController extends Controller {
public function addProducts(Request $request) {
$product = new Product();
$em = $this->getDoctrine()->getManager();
// $techChars = $em->getRepository("HomeworkBundle:TechChar")->findAll();
$form = $this->createFormBuilder($product)
->add('Name', 'text')
->add('Brand', 'choice', array(
'choices' => array('hitachi' => 'Hitachi', 'midea' => 'Midea', 'panasonic' => 'Panasonic', 'ligi' => 'LG')))
->add('Price', 'text')
->add('isActive', 'checkbox')
->add('techChar','collection',array('type'=>new FormType()) )
->add('save', 'submit')
->getForm();
if ($form->isValid()) {
$em->persist($form->getData());
$em->flush();
}
return $this->render('HomeworkBundle:Default:add.html.twig', array('form' => $form->createView()));
}
}
我必须在TechChar中创建标签为'name'的文本字段 例: http://centur.bg/Form.png
答案 0 :(得分:0)
我会在3个实体中分离你的问题:
class Product {
/** @ORM\OneToMany(targetEntity="Product") */
protected $productTechChars;
}
class ProductTechChar {
/** @ORM\ManyToOne(targetEntity="Product") */
protected $product;
/** @ORM\ManyToOne(targetEntity="Product") */
protected $techChar;
/** @ORM\ManyToOne(type="string") */
protected $textField;
}
class TechChar {
/** @ORM\OneToMany(targetEntity="Product") */
protected $productTechChars;
}
像这样,您拥有“TechChar”实体,其中包含技术特征名称列表,然后您使用“ProductTechChar”在这两个实体之间建立桥梁并设置文本字段。