Automatically set related entity id in one to many relationship Symfony
<p>I have a many to one related entities and everytime I create a new comment which is related to Project, I want to automatically save the realted project_id.</p>

<pre><code> 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  
</code></pre>

<p>When using Annotation, this can be done easily using the ParamConverter, but in this case I am using Yaml format.I am using the Symfony nice Crud command to automatically generate forms and templates as well as the controllers.</p>

<p>I tried in controller this way</p>

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


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

        return $this-&gt;redirect($this-&gt;generateUrl('comment_show', array('id' =&gt; $entity-&gt;getId())));
    }
</code></pre>

<p>Then in form, </p>

<pre><code>   {{ form(form) }}
</code></pre>

<p>The problem with this is it will generate a form with a dropdown with hundreds of project_id in comment project_id field</p>

<p>Then when submitted</p>

<blockquote>
  <p>Unable to guess how to get a Doctrine instance from the request information.</p>
</blockquote>

<p>I am thinking of writing a jquery autocomplete to solve this but if there is more shortcut way of doing this, like the annotation Paramconverter, I am glad to use it</p>

<p>How would you do it so that the related project_id will be automatically saved?</p>

<p><strong>Update</strong></p>

<p>This is exactly the code when running Symfony2 nice crud command in console</p>

<pre><code>&lt;?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-&gt;getDoctrine()-&gt;getManager();

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

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

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

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

    return $this-&gt;render('EmployeeBundle:Comment:new.html.twig', array(
        'entity' =&gt; $entity,
        'form'   =&gt; $form-&gt;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-&gt;createForm(new CommentType(), $entity, array(
        'action' =&gt; $this-&gt;generateUrl('comment_create'),
        'method' =&gt; 'POST',
    ));

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

    return $form;
}

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

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

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

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

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

    $deleteForm = $this-&gt;createDeleteForm($id);

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

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

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

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

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

    return $this-&gt;render('EmployeeBundle:Comment:edit.html.twig', array(
        'entity'      =&gt; $entity,
        'edit_form'   =&gt; $editForm-&gt;createView(),
        'delete_form' =&gt; $deleteForm-&gt;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-&gt;createForm(new CommentType(), $entity, array(
        'action' =&gt; $this-&gt;generateUrl('comment_update', array('id' =&gt; $entity-&gt;getId())),
        'method' =&gt; 'PUT',
    ));

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

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

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

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

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

    if ($editForm-&gt;isValid()) {
        $em-&gt;flush();

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

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

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

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

        $em-&gt;remove($entity);
        $em-&gt;flush();
    }

    return $this-&gt;redirect($this-&gt;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-&gt;createFormBuilder()
        -&gt;setAction($this-&gt;generateUrl('comment_delete', array('id' =&gt; $id)))
        -&gt;setMethod('DELETE')
        -&gt;add('submit', 'submit', array('label' =&gt; 'Delete'))
        -&gt;getForm()
    ;
  }
}
</code></pre>

<p>Then in FormType</p>

<pre><code> $builder
        -&gt;add('comment')
        -&gt;add('createdby')
        -&gt;add('updatedby')
        -&gt;add('datecreated')
        -&gt;add('dateupdated')
        -&gt;add('project')//related entity
    ;
</code></pre>