Javascript 打开具有动态值[symfony2]的模式

Javascript 打开具有动态值[symfony2]的模式,javascript,php,symfony,twig,Javascript,Php,Symfony,Twig,我有一个显示用户产品的页面,用户可以编辑产品。如果用户点击编辑按钮,他将被重定向到编辑页面:http://127.0.0.1/symfony/web/app_dev.php/post/125 我想在模式中显示编辑表单,这样用户就不会被重定向 我有以下错误: 在呈现模板期间引发了异常(“ 查询Xxx\XxxxxBundle\Entity\Post时缺少标识符id) 在第行的src\Xxx\XxxxxBundle\Resources\views\Post\index.html.twig中 404 我

我有一个显示用户产品的页面,用户可以编辑产品。如果用户点击编辑按钮,他将被重定向到编辑页面:
http://127.0.0.1/symfony/web/app_dev.php/post/125
我想在模式中显示编辑表单,这样用户就不会被重定向

我有以下错误:

在呈现模板期间引发了异常(“ 查询Xxx\XxxxxBundle\Entity\Post时缺少标识符id) 在第行的src\Xxx\XxxxxBundle\Resources\views\Post\index.html.twig中 404

我的链接是这样的:

<div class="action">
<a href="{{ path('post_edit', { 'id': entity.id }) }}" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>
<div class="action">
<a href="#" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>
<div class="container">
    <div class="row">
        <div class="cd-user-modal">
            <div class="cd-user-modal-container"> 
                <div id="cd-signup">
                    <div class="col-sm-12 col-sm-offset-2 form-box">
                        <div class="form-bottom">  

                      {{render(controller('FLYBookingsBundle:Post:edit')) }}

                        </div>
                    </div> 
                </div> 
            </div> 
        </div>
    </div>
</div>
<form action="{{ path('post_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
    {{ form_errors(edit_form) }}
    <div class="row">

        <div class="col-md-3">

            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-phone input-icon"></i>
                <label>Phone number</label>
                {{ form_widget(edit_form.telephone, {'type':'phone' ,'attr': {'id': 'phone','class': 'form-control phone',} }) }} {{ form_errors(edit_form.telephone) }}
            </div>
        </div>
        <div class="col-md-7">
            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-map-marker input-icon"></i>
                <label>Address</label>
                {{ form_widget(edit_form.address, { 'attr': {'class': 'form-control',} }) }} {{ form_errors(edit_form.address) }}
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-md-3">
            {{ form(edit_form) }}
        </div>
    </div>
</form>
public function indexAction($user)
{
    $user = $this->container->get('security.token_storage')->getToken()->getUser();
    $em = $this->getDoctrine()->getManager();

    $findEntities = $em->getRepository('FLYBookingsBundle:Post')->findByBusId($user);
    $entities = $this->get('knp_paginator')->paginate($findEntities, $this->get('request')->query->get('page', 1), 5
    );

    return array(
        'entities' => $entities,
        'user' => $user,
    );

}
/**
 * Displays a form to edit an existing Post entity.
 *
 * @Route("/{id}/edit", name="post_edit")
 * @Method("GET")
 * @Template()
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $this->getDoctrine()->getManager()->getRepository('FLYBookingsBundle:Post')->findBy(array('user' => $user));
    $entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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


    $editForm = $this->createEditForm($entity);

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
    );
}
编辑控制器:

<div class="action">
<a href="{{ path('post_edit', { 'id': entity.id }) }}" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>
<div class="action">
<a href="#" class="buttonModal button btn-small full-width">EDIT</a>                                                                                       
</div>
<div class="container">
    <div class="row">
        <div class="cd-user-modal">
            <div class="cd-user-modal-container"> 
                <div id="cd-signup">
                    <div class="col-sm-12 col-sm-offset-2 form-box">
                        <div class="form-bottom">  

                      {{render(controller('FLYBookingsBundle:Post:edit')) }}

                        </div>
                    </div> 
                </div> 
            </div> 
        </div>
    </div>
</div>
<form action="{{ path('post_update', { 'id': entity.id }) }}" method="post" {{ form_enctype(edit_form) }}>
    {{ form_errors(edit_form) }}
    <div class="row">

        <div class="col-md-3">

            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-phone input-icon"></i>
                <label>Phone number</label>
                {{ form_widget(edit_form.telephone, {'type':'phone' ,'attr': {'id': 'phone','class': 'form-control phone',} }) }} {{ form_errors(edit_form.telephone) }}
            </div>
        </div>
        <div class="col-md-7">
            <div class="form-group form-group-lg form-group-icon-left">
                <i class="fa fa-map-marker input-icon"></i>
                <label>Address</label>
                {{ form_widget(edit_form.address, { 'attr': {'class': 'form-control',} }) }} {{ form_errors(edit_form.address) }}
            </div>
        </div>
    </div>
    <div class="row">
        <div class="col-xs-12 col-md-3">
            {{ form(edit_form) }}
        </div>
    </div>
</form>
public function indexAction($user)
{
    $user = $this->container->get('security.token_storage')->getToken()->getUser();
    $em = $this->getDoctrine()->getManager();

    $findEntities = $em->getRepository('FLYBookingsBundle:Post')->findByBusId($user);
    $entities = $this->get('knp_paginator')->paginate($findEntities, $this->get('request')->query->get('page', 1), 5
    );

    return array(
        'entities' => $entities,
        'user' => $user,
    );

}
/**
 * Displays a form to edit an existing Post entity.
 *
 * @Route("/{id}/edit", name="post_edit")
 * @Method("GET")
 * @Template()
 */
public function editAction($id)
{
    $em = $this->getDoctrine()->getManager();
    $user = $this->getUser();
    $this->getDoctrine()->getManager()->getRepository('FLYBookingsBundle:Post')->findBy(array('user' => $user));
    $entity = $em->getRepository('FLYBookingsBundle:Post')->find($id);

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


    $editForm = $this->createEditForm($entity);

    return array(
        'entity' => $entity,
        'edit_form' => $editForm->createView(),
    );
}

快速浏览我猜错误给出了一个线索,您正在呈现具有参数/{id}/edit的编辑控制器,您没有传递该参数

{{ render(controller('FLYBookingsBundle:Post:edit', {}, { 'id': id, 'active': true } )) }}
检查

好的,我不确定您的实现,所以我将向您展示一个我没有进行代码预处理或标准化的实现

我的情态动词

{% for meat in meats %}
<div class="modal fade" id="{{ meat[0] }}" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
<div class="modal-dialog" role="document">
    <div class="modal-content">
        <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title">
                {% if waitingProduct[meat[0]]|length > 0 %}
                    {{ waitingProduct[meat[0]][0].itemdescription }}
                {% else %}
                    No Product available, please contact stores
                {% endif %}
            </h4>
        </div>
        <form action="{{ path('formulation_change') }}" method="post">
        <div class="modal-body">
            {% for product in waitingProduct[meat[0]] %}
            <input type="radio" name="pallet" value="{{ product.id }}" > {{ product.typenumber }}
            <br />
            {% endfor %}
            <input type="hidden" name="recipe" value="{{ recipe }}" >
            {% if waitingProduct[meat[0]]|length > 0 %}
            <input type="hidden" name="itemCode" value="{{ waitingProduct[meat[0]][0].itemcode }}" >
            {% endif %}
        </div>
        <div class="modal-footer">
            <button type="button" class="btn btn-default pull-left" data-dismiss="modal">Close</button>
            <button type="submit" class="btn btn-primary">Set Active</button>
        </div>
        </form>
    </div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div> 
{% endfor %}
{%用于肉类中的肉类%}
&时代;
{如果waitingProduct[meat[0]]|长度>0%}
{{waitingProduct[meat[0]][0].itemdescription}
{%else%}
没有可用产品,请与门店联系
{%endif%}
{waitingProduct[meat[0]]%中的产品的百分比}
{{product.typenumber}

{%endfor%} {如果waitingProduct[meat[0]]|长度>0%} {%endif%} 接近 激活 {%endfor%}
用于切换单独模态的按钮

 {% for meat in meats %}
    <td>
        <div class="btn btn-sm btn-success" data-toggle="modal" data-target="#{{ meat[0] }}"  id="map">Set Active</div>
    </td>
{% endfor %}
{%用于肉类中的肉类%}
激活
{%endfor%}

我已尝试使用该参数渲染控制器,但没有效果。我得到了以下错误:
变量“id”在第404行的src\FLY\BookingsBundle\Resources\views\Post\index.html.twig中不存在。
您可以
转储
模板中试图从中获取
id的变量吗<代码>{dump(somevar)}
@LordZed,我正在尝试做类似于这里的事情=>
http://stackoverflow.com/questions/31532805/how-to-display-data-dynamically-in-bootstrap-modal-popup-in-symfony2
我试图获取id的模板我无法获取它,因为在我的控制器索引中显示了所有的产品。为什么要删除
{{{path()}}
?您可以使用它来获取目标URL并获取Javascript代码中的页面。无论如何,
src\Xxx\XxxxxBundle\Resources\views\Post\index.html.twig的第404行代码是什么,正如Hounded所解释的,我应该在渲染中传递参数,所以它应该是这样的:
{render(controller('FLYBookingsBundle:Post:edit',{},{'id':id,'active':true}))}
。但是我有一个错误:
变量“id”在src\FLY\BookingsBundle\Resources\views\Post\index.html.twig第404行不存在。
尝试
{render(controller('FLYBookingsBundle:Post:edit',{'id':id,'active':true}}}}
(包含路由属性的数组是第二个参数)并定义
id
变量。我有相同的错误。产品列表的路径如下所示:
profile\u列表:路径:/{{u locale}/profile/listings默认值:{{u控制器:FLYBookingsBundle:Post:index}