我在PostCategory和SubCategory之间有一个多对一的关系,如下所示:
子类别实体:
namespace VisualImmersionAdminBundleEntity;
use DoctrineORMMapping as ORM;
/**
* SubCategory
*
* @ORMTable(name="post_category_subcategory")
* @ORMEntity(repositoryClass="VisualImmersionAdminBundleEntityRepositorySubCategoryRepository")
*/
class SubCategory
{
/**
* @var integer
*
* @ORMColumn(name="ID", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=255)
*/
private $name;
/**
* @ORMManyToOne(targetEntity="VisualImmersionAdminBundleEntityPostCategory", cascade={"remove", "persist"}, inversedBy="subCategories")
* @ORMJoinColumn(referencedColumnName="ID")
*/
private $postCategory;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return SubCategory
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Set category
*
* @param VisualImmersionAdminBundleEntityPostCategory $category
*/
public function setPostCategory(VisualImmersionAdminBundleEntityPostCategory $category)
{
$this->postCategory = $category;
}
/**
* Get category
*
* @return VisualImmersionAdminBundleEntityPostCategory
*/
public function getPostCategory()
{
return $this->postCategory;
}
}
帖子类别实体:
<?php
namespace VisualImmersionAdminBundleEntity;
use DoctrineORMMapping as ORM;
/**
* PostCategory
*
* @ORMTable(name="post_category")
* @ORMEntity(repositoryClass="VisualImmersionAdminBundleEntityRepositoryPostCategoryRepository")
*/
class PostCategory
{
/**
* @var integer
*
* @ORMColumn(name="ID", type="integer", nullable=false)
* @ORMId
* @ORMGeneratedValue(strategy="IDENTITY")
*/
private $id;
/**
* @var string
*
* @ORMColumn(name="name", type="string", length=45, nullable=false)
*/
private $name;
/**
* @ORMManyToMany(targetEntity="Post", mappedBy="posts")
*/
private $posts;
/**
* @ORMOneToMany(targetEntity="SubCategory", mappedBy="postCategory", cascade={"remove", "persist"})
*/
protected $subCategories;
public function __construct()
{
$this->posts = new DoctrineCommonCollectionsArrayCollection();
$this->subCategories = new DoctrineCommonCollectionsArrayCollection();
}
public function __toString()
{
return (String) $this->id;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return PostCategory
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName()
{
return $this->name;
}
/**
* Add post
*
* @param VisualImmersionAdminBundleEntityPost $post
*/
public function addPost(VisualImmersionAdminBundleEntityPost $post)
{
$this->posts[] = $post;
}
/**
* Get posts
*
* @return DoctrineCommonCollectionsArrayCollection()
*/
public function getPosts()
{
return $this->posts->toArray();
}
/**
* Remove post
*
* @param VisualImmersionAdminBundleEntityPost $post
*/
public function removePost(VisualImmersionAdminBundleEntityPost $post)
{
$this->posts->removeElement($post);
}
/**
* Add subCategory
*
* @param VisualImmersionAdminBundleEntitySubCategory $subCategory
*/
public function addSubCategory(VisualImmersionAdminBundleEntitySubCategory $subCategory)
{
$this->subCategories[] = $subCategory;
$subCategory->setPostCategory($this);
}
/**
* Get subCategories
*
* @return DoctrineCommonCollectionsArrayCollection()
*/
public function getSubCategories()
{
return $this->subCategories;
}
/**
* Set subCategories
*
* @param DoctrineCommonCollectionsArrayCollection $subCategories
*/
public function setSubCategories(DoctrineCommonCollectionsArrayCollection $subCategories)
{
foreach ($subCategories as $subCategory) {
$subCategory->setPostCategory($this);
}
$this->subCategories = $subCategories;
}
/**
* Remove subCategory
*
* @param VisualImmersionAdminBundleEntitySubCategory $subCategory
*/
public function removeSubCategory(VisualImmersionAdminBundleEntitySubCategory $subCategory)
{
$this->subCategories->removeElement($subCategory);
}
}
我有嵌入式表单来添加子类别集合 在帖子类别表单上:
子类别类型:
<?php
namespace VisualImmersionAdminBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
class SubCategoryType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
;
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'VisualImmersionAdminBundleEntitySubCategory'
));
}
/**
* @return string
*/
public function getName()
{
return 'visualimmersion_adminbundle_subcategorytype';
}
}
帖子类别类型:
<?php
namespace VisualImmersionAdminBundleForm;
use SymfonyComponentFormAbstractType;
use SymfonyComponentFormFormBuilderInterface;
use SymfonyComponentOptionsResolverOptionsResolverInterface;
class PostCategoryType extends AbstractType
{
/**
* @param FormBuilderInterface $builder
* @param array $options
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name')
->add('subCategories', 'collection', array(
'type' => new SubCategoryType(),
'required' =>false,
'allow_add' => true,
'allow_delete' => true,
'prototype' => true,
'prototype_name' =>'subCategory__name__',
'by_reference' => 'false'
));
}
/**
* @param OptionsResolverInterface $resolver
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver->setDefaults(array(
'data_class' => 'VisualImmersionAdminBundleEntityPostCategory'
));
}
/**
* @return string
*/
public function getName()
{
return 'visualimmersion_adminbundle_postcategorytype';
}
}
我的问题是在数据库的子类别表中,未添加 postCategory 外键:
我拼命地试图找到解决这个问题的方法,所以我寻求你的帮助。
编辑:
我的控制器:帖子类别
<?php
namespace VisualImmersionAdminBundleController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;
use VisualImmersionAdminBundleEntityPostCategory;
use VisualImmersionAdminBundleFormPostCategoryType;
/**
* PostCategory controller.
*
* @Route("/postcategory")
*/
class PostCategoryController extends Controller
{
/**
* Lists all PostCategory entities.
*
* @Route("/", name="postcategory")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new PostCategory entity.
*
* @Route("/create", name="postcategory_create")
* @Method("POST")
* @Template("VisualImmersionAdminBundle:PostCategory:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new PostCategory();
$form = $this->createForm(new PostCategoryType(), $entity);
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('postcategory_index', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Displays a form to create a new PostCategory entity.
*
* @Route("/new", name="postcategory_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$entity = new PostCategory();
$form = $this->createForm(new PostCategoryType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a PostCategory entity.
*
* @Route("/{id}", name="postcategory_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find PostCategory entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing PostCategory entity.
*
* @Route("/edit/{id}", name="postcategory_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find PostCategory entity.');
}
$editForm = $this->createForm(new PostCategoryType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Edits an existing PostCategory entity.
*
* @Route("/update/{id}", name="postcategory_update")
* @Method("PUT")
* @Template("VisualImmersionAdminBundle:PostCategory:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find PostCategory entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new PostCategoryType(), $entity);
$editForm->submit($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('postcategory_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a PostCategory entity.
*
* @Route("/delete/{id}", name="postcategory_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find PostCategory entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('postcategory_index'));
}
/**
* Creates a form to delete a PostCategory entity by id.
*
* @param mixed $id The entity id
*
* @return SymfonyComponentFormForm The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
子类别:
<?php
namespace VisualImmersionAdminBundleController;
use SymfonyComponentHttpFoundationRequest;
use SymfonyBundleFrameworkBundleControllerController;
use SensioBundleFrameworkExtraBundleConfigurationMethod;
use SensioBundleFrameworkExtraBundleConfigurationRoute;
use SensioBundleFrameworkExtraBundleConfigurationTemplate;
use VisualImmersionAdminBundleEntitySubCategory;
use VisualImmersionAdminBundleFormSubCategoryType;
/**
* SubCategory controller.
*
* @Route("/subcategory")
*/
class SubCategoryController extends Controller
{
/**
* Lists all SubCategory entities.
*
* @Route("/", name="subcategory")
* @Method("GET")
* @Template()
*/
public function indexAction()
{
$em = $this->getDoctrine()->getManager();
$entities = $em->getRepository('VisualImmersionAdminBundle:SubCategory')->findAll();
return array(
'entities' => $entities,
);
}
/**
* Creates a new SubCategory entity.
*
* @Route("/", name="subcategory_create")
* @Method("POST")
* @Template("VisualImmersionAdminBundle:SubCategory:new.html.twig")
*/
public function createAction(Request $request)
{
$entity = new SubCategory();
$form = $this->createForm(new SubCategoryType(), $entity);
$form->submit($request);
$logger = $this->get('logger');
$logger->info('Nous sommes dans le Create Action de SubCategory');
if ($form->isValid()) {
$logger->info('Le formulaire semble valide dans le Create Action de SubCategory');
$em = $this->getDoctrine()->getManager();
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('subcategory_show', array('id' => $entity->getId())));
}
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Displays a form to create a new SubCategory entity.
*
* @Route("/new", name="subcategory_new")
* @Method("GET")
* @Template()
*/
public function newAction()
{
$logger = $this->get('logger');
$logger->info('Nous sommes dans le New Action de SubCategory');
$entity = new SubCategory();
$form = $this->createForm(new SubCategoryType(), $entity);
return array(
'entity' => $entity,
'form' => $form->createView(),
);
}
/**
* Finds and displays a SubCategory entity.
*
* @Route("/{id}", name="subcategory_show")
* @Method("GET")
* @Template()
*/
public function showAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:SubCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find SubCategory entity.');
}
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'delete_form' => $deleteForm->createView(),
);
}
/**
* Displays a form to edit an existing SubCategory entity.
*
* @Route("/{id}/edit", name="subcategory_edit")
* @Method("GET")
* @Template()
*/
public function editAction($id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:SubCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find SubCategory entity.');
}
$editForm = $this->createForm(new SubCategoryType(), $entity);
$deleteForm = $this->createDeleteForm($id);
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Edits an existing SubCategory entity.
*
* @Route("/{id}", name="subcategory_update")
* @Method("PUT")
* @Template("VisualImmersionAdminBundle:SubCategory:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:SubCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find SubCategory entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new SubCategoryType(), $entity);
$editForm->submit($request);
if ($editForm->isValid()) {
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('subcategory_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
/**
* Deletes a SubCategory entity.
*
* @Route("/{id}", name="subcategory_delete")
* @Method("DELETE")
*/
public function deleteAction(Request $request, $id)
{
$form = $this->createDeleteForm($id);
$form->submit($request);
if ($form->isValid()) {
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:SubCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find SubCategory entity.');
}
$em->remove($entity);
$em->flush();
}
return $this->redirect($this->generateUrl('subcategory'));
}
/**
* Creates a form to delete a SubCategory entity by id.
*
* @param mixed $id The entity id
*
* @return SymfonyComponentFormForm The form
*/
private function createDeleteForm($id)
{
return $this->createFormBuilder(array('id' => $id))
->add('id', 'hidden')
->getForm()
;
}
}
我想我看到这可能是这里的问题。
您有以下@ManyToOne
关系:
/**
* @ORMManyToOne(targetEntity="VisualImmersionAdminBundleEntityPostCategory", cascade={"remove", "persist"}, inversedBy="subCategories")
* @ORMJoinColumn(referencedColumnName="ID")
*/
private $postCategory;
。但关系双方都不知道该外键列的名称是什么。尝试以这种方式更改它:
@ORMJoinColumn(name="postCategory_id", referencedColumnName="ID")
你为什么不尝试使用ORM创建实体呢?这也将有助于创建数据库。
我找到了一个解决方案:
/**
* Edits an existing PostCategory entity.
*
* @Route("/update/{id}", name="postcategory_update")
* @Method("PUT")
* @Template("VisualImmersionAdminBundle:PostCategory:edit.html.twig")
*/
public function updateAction(Request $request, $id)
{
$em = $this->getDoctrine()->getManager();
$entity = $em->getRepository('VisualImmersionAdminBundle:PostCategory')->find($id);
if (!$entity) {
throw $this->createNotFoundException('Unable to find PostCategory entity.');
}
$deleteForm = $this->createDeleteForm($id);
$editForm = $this->createForm(new PostCategoryType(), $entity);
$editForm->submit($request);
if ($editForm->isValid()) {
foreach ($entity->getSubCategories() as $subCategory)
{
$subCategory->setPostCategory($entity);
$em->persist($subCategory);
}
$em->persist($entity);
$em->flush();
return $this->redirect($this->generateUrl('postcategory_edit', array('id' => $id)));
}
return array(
'entity' => $entity,
'edit_form' => $editForm->createView(),
'delete_form' => $deleteForm->createView(),
);
}
我不确定这是最好的解决方案。 必须实际更改控制器。我宁愿只修改这些东西的实体。你有更好的主意吗?