Symfony2如何过滤查询

时间:2015-03-05 21:21:36

标签: symfony

更新

想想我可能已经离开了这里,但我仍然得到了相同的结果,整个页面显示在表格中。

所以我正在为视图和状态页面执行此操作。 “视图”是显示所有活动警报的默认页面。状态是将显示已过滤警报的页面。所以这些页面中的每一个都有一个控制器

public function viewAction()
{
    $repository = $this
        ->getDoctrine()
        ->getManager()
        ->getRepository('NickAlertBundle:Alert');

    $alerts = $repository->getAllActiveAlerts();

    return $this->render('NickAlertBundle:Page:view.html.twig', array(
        'alerts' => $alerts,
    ));
}

public function getActivatedAction(Request $request)
{
    $alertStatus = $request->request->get('status', 'active');
    $alerts = $this->getDoctrine()->getRepository('NickAlertBundle:Alert')->getAlertByStatus($alertStatus);

    return $this->render('NickAlertBundle:Page:status.html.twig', array("alerts"=>$alerts));
}

然后每个页面都有一个路径

NickAlertBundle_view:
    pattern:  /view-alerts
    defaults: { _controller: NickAlertBundle:Alert:view }
    requirements:
       _method:  GET

NickAlertBundle_status:
    pattern:  /view-alerts
    defaults: { _controller: NickAlertBundle:Alert:getActivated }
    requirements:
       _method:  POST

NickAlertBundle_view调用视图操作并呈现包含所有活动警报的页面。 NickAlertBundle_status调用getActivated操作。

这两种观点都是一样的,其内容如下

{% extends 'NickAlertBundle::layout.html.twig' %}

{% block main %}

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

<section class="panel panel-default">
    <header class="panel-heading">
        <h3 class="panel-title">View Alerts</h3>
        <select name="alerts" id="alerts" data-url="{{ path('NickAlertBundle_status') }}">
            <option value="active">Active</option>
            <option value="inactive">Inactive</option>
        </select>
    </header>
    <div class="panel-body">

        <div class="row" id="alert-container">
            <table width="100%" cellpadding="0" cellspacing="0" border="0" id="datatable" class="table">
                <thead>
                <tr>
                    <th>Id</th>
                    <th>Search Command</th>
                    <th>Flight Number</th>
                    <th>Booking Class</th>
                    <th>Alert Pseudo</th>
                    <th>Alert Status</th>
                </tr>
                </thead>
                <tbody>
                    {% for alert in alerts %}
                        <tr>
                            <td>{{ alert[0].id }}</td>
                            <td>{{ alert[0].searchCommand }}</td>
                            <td>{{ alert.flight_number }}</td>
                            <td>{{ alert.classes }}</td>
                            <td>{{ alert.pseudos }}</td>
                            <td>{{ alert[0].alertStatus }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</section>
    </div>

{% endblock %}

因此数据网址应该调用状态路由。两个视图都有这个,因为它们应该始终能够更改任一页面上的选择。

然后javascript和你给我看的几乎一样。

3 个答案:

答案 0 :(得分:2)


已更新


问题基本上是当你实际必须放入警报代码时,你正在从viewAction嵌入html响应,例如:

public function getActivatedAction(Request $request)
{
    $alertStatus = $request->request->get('status', 'active');
    $alerts = $this->getDoctrine()->getRepository('NickAlertBundle:Alert')->findAllByStatus($alertStatus);

    return $this->render('NickAlertBundle:Alerts:show.html.twig', array("alerts"=>$alerts));
}
<{1}}

上的

NickAlertBundle:Alerts:show.html.twig

然后在你的{# NickAlertBundle:Alerts:show.html.twig #} {% for alert in alerts %} <tr> <td>{{ alert[0].id }}</td> <td>{{ alert[0].searchCommand }}</td> <td>{{ alert.flight_number }}</td> <td>{{ alert.classes }}</td> <td>{{ alert.pseudos }}</td> <td>{{ alert[0].alertStatus }}</td> </tr> {% endfor %}

NickAlertBundle:Page:view.html.twig


您可以在Enity存储库文件中添加一个methond:

$('#alerts').change(function(){
    $.ajax({
        type: "POST",
        url: $('#alerts').attr('data-url'), # getActivatedAction route
        data:{ status: $(this).val() },
        success: function(data){
            $('#alert-container table tbody').html(data);
        }
    });
});

然后在你的控制器中,你可以传递带有数据或html的json:

# src/Package/AppBundle/Entity/AlertRepository.php
<?php

namespace Package\AppBundle\Entity;

use Doctrine\ORM\EntityRepository;

/**
 * AlertRepository
 *
 * This class was generated by the Doctrine ORM. Add your own custom
 * repository methods below.
 */
class AlertRepository extends EntityRepository
{
  public function findAllByStatus($status = "active")
  {
    $qb = $this->createQueryBuilder('a');
    $query = $qb
        ->where(
          $qb->expr()->eq('a.alertStatus', $status)
        )
        ->orderBy('a.id', 'DESC')
        ->getQuery();
    return $query->getResult();
  }

}

现在在yor html文件中,您可以使用ajax来请求警报:

<?php
...
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\HttpFoundation\JsonResponse;
...

public function getActivatedAction(Request $request)
{
    $alertStatus = $request->request->get('status', 'active');
    $alerts = $this->getDoctrine()->getRepository('AppBundle:Alert')->findAllByStatus($alertStatus);

    // With JSON
    $json = alerts->toJson() // your own method

    $response = new JsonResponse();
    $response->setData($json);

    // With HTML
    return $this->render('AppBundle:Alerts:show.html.twig', array("alerts"=>$alerts));
}

如果我没有错过任何东西它应该有用。

如果我弄错了,请告诉我,我希望这会对你有所帮助。

答案 1 :(得分:1)

$('#alert-container table tbody').html(data);行将根据服务器返回的响应设置tbody表的contento,因此您必须有两个不同的文件

{% NickAlertBundle:Page:view.html.twig %}
{% extends 'NickAlertBundle::layout.html.twig' %}

{% block main %}

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

<section class="panel panel-default">
    <header class="panel-heading">
        <h3 class="panel-title">View Alerts</h3>
        <select name="alerts" id="alerts" data-url="{{ path('NickAlertBundle_status') }}">
            <option value="active">Active</option>
            <option value="inactive">Inactive</option>
        </select>
    </header>
    <div class="panel-body">

        <div class="row" id="alert-container">
            <table width="100%" cellpadding="0" cellspacing="0" border="0" id="datatable" class="table">
                <thead>
                <tr>
                    <th>Id</th>
                    <th>Search Command</th>
                    <th>Flight Number</th>
                    <th>Booking Class</th>
                    <th>Alert Pseudo</th>
                    <th>Alert Status</th>
                </tr>
                </thead>
                <tbody>
                    {% for alert in alerts %}
                        <tr>
                            <td>{{ alert[0].id }}</td>
                            <td>{{ alert[0].searchCommand }}</td>
                            <td>{{ alert.flight_number }}</td>
                            <td>{{ alert.classes }}</td>
                            <td>{{ alert.pseudos }}</td>
                            <td>{{ alert[0].alertStatus }}</td>
                        </tr>
                    {% endfor %}
                </tbody>
            </table>
        </div>
    </div>
</section>
    </div>

{% endblock %}

{# NickAlertBundle:Page:status.html.twig #}
{% for alert in alerts %}
    <tr>
        <td>{{ alert[0].id }}</td>
        <td>{{ alert[0].searchCommand }}</td>
        <td>{{ alert.flight_number }}</td>
        <td>{{ alert.classes }}</td>
        <td>{{ alert.pseudos }}</td>
        <td>{{ alert[0].alertStatus }}</td>
    </tr>
{% endfor %}

答案 2 :(得分:0)

要解决网址问题,你应该提出:

NickAlertBundle_view:
    pattern:  /view-alerts
    defaults: { _controller: NickAlertBundle:Alert:view }
    methods:  [GET]

NickAlertBundle_status:
    pattern:  /view-alerts
    defaults: { _controller: NickAlertBundle:Alert:getActivated }
    methods:  [POST]