在一对多关系Symfony中自动设置相关实体​​ID

时间:2016-01-18 02:40:08

标签: symfony yaml one-to-many

我有多个相关的实体,每当我创建一个与Project相关的新注释时,我想自动保存已实现的project_id。

 comment
    id
    comment
 manyToOne
    project:
        targetEntity: Project
        cascade: {  }
        mappedBy: null
        inversedBy: comments
        joinColumn:
            name:  project_id
            referencedColumnName: id
        orphanRemoval: false

  project
      id
      projectName
  oneToMany:
    comments:
        targetEntity: Comment
        mappedBy: project  

使用Annotation时,可以使用ParamConverter轻松完成,但在这种情况下我使用的是Yaml格式。我使用Symfony nice Crud命令自动生成表单和模板以及控制器。

我在控制器中尝试过这种方式

public function createAction(Request $request, Project $project)//Project
{
    $entity = new Comment();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);


    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
       // $entity->setProject($projectName->getProject());
        $entity->setProject($project);
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('comment_show', array('id' => $entity->getId())));
    }

然后是表格,

   {{ form(form) }}

这个问题是它会在注释project_id字段中生成一个包含数百个project_id的下拉列表

然后提交时

  

无法猜测如何从请求信息中获取Doctrine实例。

我正在考虑编写一个jquery自动完成来解决这个问题,但是如果有更多的快捷方式,比如注释Paramconverter,我很高兴使用它

你会怎样做才能自动保存相关的project_id?

更新

这正是在控制台

中运行Symfony2 nice crud命令时的代码
<?php

namespace EdgeWeb\Project\EmployeeBundle\Controller;

use Symfony\Component\HttpFoundation\Request;
use Symfony\Bundle\FrameworkBundle\Controller\Controller;

use EdgeWeb\Project\EmployeeBundle\Entity\Comment;
use EdgeWeb\Project\EmployeeBundle\Form\CommentType;

/**
* Comment controller.
*
*/
class CommentController extends Controller
{

/**
 * Lists all Comment entities.
 *
 */
public function indexAction()
{
    $em = $this->getDoctrine()->getManager();

    $entities = $em->getRepository('EmployeeBundle:Comment')->findAll();

    return $this->render('EmployeeBundle:Comment:index.html.twig', array(
        'entities' => $entities,
    ));
}
/**
 * Creates a new Comment entity.
 *
 */
public function createAction(Request $request)
{
    $entity = new Comment();
    $form = $this->createCreateForm($entity);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $em->persist($entity);
        $em->flush();

        return $this->redirect($this->generateUrl('comment_show', array('id' => $entity->getId())));
    }

    return $this->render('EmployeeBundle:Comment:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
 * Creates a form to create a Comment entity.
 *
 * @param Comment $entity The entity
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createCreateForm(Comment $entity)
{
    $form = $this->createForm(new CommentType(), $entity, array(
        'action' => $this->generateUrl('comment_create'),
        'method' => 'POST',
    ));

    $form->add('submit', 'submit', array('label' => 'Create'));

    return $form;
}

/**
 * Displays a form to create a new Comment entity.
 *
 */
public function newAction()
{
    $entity = new Comment();
    $form   = $this->createCreateForm($entity);

    return $this->render('EmployeeBundle:Comment:new.html.twig', array(
        'entity' => $entity,
        'form'   => $form->createView(),
    ));
}

/**
 * Finds and displays a Comment entity.
 *
 */
public function showAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('EmployeeBundle:Comment')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Comment entity.');
    }

    $deleteForm = $this->createDeleteForm($id);

    return $this->render('EmployeeBundle:Comment:show.html.twig', array(
        'entity'      => $entity,
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
 * Displays a form to edit an existing Comment entity.
 *
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('EmployeeBundle:Comment')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Comment entity.');
    }

    $editForm = $this->createEditForm($entity);
    $deleteForm = $this->createDeleteForm($id);

    return $this->render('EmployeeBundle:Comment:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}

/**
* Creates a form to edit a Comment entity.
*
* @param Comment $entity The entity
*
* @return \Symfony\Component\Form\Form The form
*/
private function createEditForm(Comment $entity)
{
    $form = $this->createForm(new CommentType(), $entity, array(
        'action' => $this->generateUrl('comment_update', array('id' => $entity->getId())),
        'method' => 'PUT',
    ));

    $form->add('submit', 'submit', array('label' => 'Update'));

    return $form;
}
/**
 * Edits an existing Comment entity.
 *
 */
public function updateAction(Request $request, $id)
{
    $em = $this->getDoctrine()->getManager();

    $entity = $em->getRepository('EmployeeBundle:Comment')->find($id);

    if (!$entity) {
        throw $this->createNotFoundException('Unable to find Comment entity.');
    }

    $deleteForm = $this->createDeleteForm($id);
    $editForm = $this->createEditForm($entity);
    $editForm->handleRequest($request);

    if ($editForm->isValid()) {
        $em->flush();

        return $this->redirect($this->generateUrl('comment_edit', array('id' => $id)));
    }

    return $this->render('EmployeeBundle:Comment:edit.html.twig', array(
        'entity'      => $entity,
        'edit_form'   => $editForm->createView(),
        'delete_form' => $deleteForm->createView(),
    ));
}
/**
 * Deletes a Comment entity.
 *
 */
public function deleteAction(Request $request, $id)
{
    $form = $this->createDeleteForm($id);
    $form->handleRequest($request);

    if ($form->isValid()) {
        $em = $this->getDoctrine()->getManager();
        $entity = $em->getRepository('EmployeeBundle:Comment')->find($id);

        if (!$entity) {
            throw $this->createNotFoundException('Unable to find Comment entity.');
        }

        $em->remove($entity);
        $em->flush();
    }

    return $this->redirect($this->generateUrl('comment'));
}

/**
 * Creates a form to delete a Comment entity by id.
 *
 * @param mixed $id The entity id
 *
 * @return \Symfony\Component\Form\Form The form
 */
private function createDeleteForm($id)
{
    return $this->createFormBuilder()
        ->setAction($this->generateUrl('comment_delete', array('id' => $id)))
        ->setMethod('DELETE')
        ->add('submit', 'submit', array('label' => 'Delete'))
        ->getForm()
    ;
  }
}

然后在FormType

 $builder
        ->add('comment')
        ->add('createdby')
        ->add('updatedby')
        ->add('datecreated')
        ->add('dateupdated')
        ->add('project')//related entity
    ;

1 个答案:

答案 0 :(得分:0)

我在你的问题中看到至少3个问题......

对于错误:

  

无法猜测如何从请求中获取Doctrine实例   信息,

我不知道它为什么会发生,但它似乎与关系问题没有直接关系...也许尝试从表单构建器中禁止字段'createdby', 'updatedby', 'datecreated', 'dateupdated',因为它们不在您的映射yaml文件中(或者您可能只是没有显示它们) - 无论如何,您可能不应该在表单中显示它们,而是在控制器中或通过prePersist操作完成这些字段,作为用户我们不必了解它们。

然后,对于表单的项目字段显示带有项目ID的下拉列表的问题:您可以在表单构建器中通过comment指定要显示的choice_label实体的哪个属性选项,像这样:

$builder
    ->add('comment')
    ->add('project', 'entity', array(
        'class' => 'YourBundle:Project',
        'choice_label' => 'projectName',
));

这样,您仍然会有一个下拉列表,但显示项目名称而不是ID。 &#39;实体&#39;文件字段类型有许多其他选项,请参阅the documentation

对于你的上一个问题,你想要一个带有自动完成而不是下拉列表的文本字段,你可以使用数据转换器+ JS自动完成器来管理它。有关详细信息,请参阅this answer