我有这些实体: - 存储和包
我想构建一个StoreType表单,我在其中添加包ID。
我已经制作了一个StoreType Form类:(我希望从中获取一个选择框,列出所有包)
我通过一对一添加包,但我的问题是当我在商店表单中添加包然后表单是创建但在我的数据库商店表列包是空的。我不知道为什么?
/**
* @ORM\Entity
*/
class Store
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length="255")
*/
protected $title;
/**
* @ORM\Column(type="string", length="255")
*/
protected $domain;
/**
* @ORM\OneToOne(targetEntity="Package",cascade={"persist", "remove"})
* @ORM\JoinColumn(name="package_id", referencedColumnName="id")
*/
protected $package;
}
/**
* @ORM\Entity
*/
class Package
{
/**
* @ORM\Id
* @ORM\Column(type="integer")
* @ORM\GeneratedValue(strategy="AUTO")
*/
protected $id;
/**
* @ORM\Column(type="string", length=255)
*/
protected $title;
/**
* @ORM\Column(type="text", length="4000")
*/
protected $description;
/**
* @ORM\Column(type="boolean")
*/
protected $active;
public function __toString()
{
return $this->getTitle();
}
}
class StoreType extends AbstractType
{
public function buildForm(FormBuilder $builder, array $options)
{
$builder
->add('title')
->add('domain')
->add('package','entity',array(
'class' => 'WebmuchProductBundle:Package',
));
}
public function getName()
{
return 'webmuch_productbundle_storetype';
}
}
/**
* Store controller.
*
* @Route("/store")
*/
class StoreController extends Controller
{
/**
* Lists all Store entities.
*
* @Route("/", name="store")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getEntityManager();
$entities = $em->getRepository('WebmuchProductBundle:Store')->findAll();
return array('entities' => $entities);
}
/**
* Finds and displays a Store entity.
*
* @Route("/{id}/show", name="store_show")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('WebmuchProductBundle:Store')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Store entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(), );
}
/**
* Displays a form to create a new Store entity.
*
* @Route("/new", name="store_new")
* @Template()
*/
public function newAction()
{
$store = new Store();
$form = $this->createForm(new StoreType(), $store);
return array(
'entity' => $store,
'form' => $form->createView()
);
}
/**
* Creates a new Store entity.
*
* @Route("/create", name="store_create")
* @Method("post")
* @Template("WebmuchProductBundle:Store:new.html.twig")
*/
public function createAction()
{
$entity = new Store();
$request = $this->getRequest();
$form = $this->createForm(new StoreType(), $entity);
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('store_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView()
);
}
/**
* Displays a form to edit an existing Store entity.
*
* @Route("/{id}/edit", name="store_edit")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('WebmuchProductBundle:Store')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Store entity.');
}
$editForm = $this->createForm(new StoreType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Edits an existing Store entity.
*
* @Route("/{id}/update", name="store_update")
* @Method("post")
* @Template("WebmuchProductBundle:Store:edit.html.twig")
*/
public function updateAction($id)
{
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('WebmuchProductBundle:Store')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Store entity.');
}
$editForm = $this->createForm(new StoreType(), $entity);
$deleteForm = $this->createDeleteForm($id);
$request = $this->getRequest();
$editForm->bindRequest($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('store_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a Store entity.
*
* @Route("/{id}/delete", name="store_delete")
* @Method("post")
*/
public function deleteAction($id)
{
$form = $this->createDeleteForm($id);
$request = $this->getRequest();
$form->bindRequest($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getEntityManager();
$entity = $em->getRepository('WebmuchProductBundle:Store')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find Store entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('store'));
}
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
答案 0 :(得分:0)
我认为您忘记在createAction中执行此操作:
$request = $this->getRequest();
$form->setData($request->getPost());