我必须从 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;
}
}
技术字符
<?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';
}
}
和面板控制器
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 示例中创建标签为“名称”的文本字段:http: //centur.bg/Form.png