Php Symfony2:使用flush()或persist()优化数据处理
我正在使用Symfony(const version=“2.5.10”),并在PHP版本5.5.19中使用XAMPP 我的问题是我不断地出内存错误。因为我想我在数据库中查询了上千个数据(行)。关于信息,我在数据库中有很多数据。我想使用Php Symfony2:使用flush()或persist()优化数据处理,php,symfony,Php,Symfony,我正在使用Symfony(const version=“2.5.10”),并在PHP版本5.5.19中使用XAMPP 我的问题是我不断地出内存错误。因为我想我在数据库中查询了上千个数据(行)。关于信息,我在数据库中有很多数据。我想使用flush()或任何可以用来优化数据处理的工具 以下是我在indexAction控制器中的代码: public function indexAction(){ $em = $this->getDoctrine()->getManager(); $
flush()
或任何可以用来优化数据处理的工具
以下是我在indexAction控制器中的代码:
public function indexAction(){
$em = $this->getDoctrine()->getManager();
$po = $em->getRepository('MatrixEdiBundle:EdiTransactionDetail')->findDocs('850');
return $this->render('MatrixEdiBundle:Matrix:index.html.twig', array('po' => $po));
}
index.html.twig
{% extends '::layout.html.twig' %}
{# {% include 'MatrixEdiBundle:Matrix:header.html.twig'%} #}
{% block body %}
<div class="content">
</br>
<table id="datTable" class="table table-bordered table-condensed table-hover">
<thead>
<th colspan="8">Edi Matrix</th>
<tr>
<th>Po Numbers</th>
<th>Trading Partner Id</th>
<th>PO 855 Acknowledgement</th>
<th>PO 997 Acknowledgement</th>
<th>Advance Shipment Notice</th>
<th>Invoice</th>
<th>Purchase Order Change</th>
<th>Order Status</th>
</tr>
</thead>
<tbody>
{% for tran in po %}
<tr>
<td><a href="{{ path('matrix_edi_showpo', {'po_num': tran.poNumber}) }}"data-toggle="modal" data-target="#myModal">{{tran.poNumber}}</td>
<td>{{tran.ediTransaction.senderId}}</td>
<td><a href="{{ path('matrix_edi_findAll', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'doc_type': 855}) }}"data-toggle="modal"data-target="#myModal">
{{ render(controller('MatrixEdiBundle:Matrix:matrix', {
'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran. ediTransaction.receiverId, 'doc_type': 855, 'gs_number': tran.ediTransaction.gsNumber })) }}</a>
</td>
<td><a href="{{ path('matrix_edi_poack', {'gs_number': tran.ediTransaction.gsNumber, 'receiver_id': tran.ediTransaction.receiverId, 'sender_id': tran.ediTransaction.senderId}) }}"data-toggle="modal"data-target="#myModal">
{{ render(controller('MatrixEdiBundle:Matrix:matrix', {
'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran. ediTransaction.receiverId, 'doc_type': 997, 'gs_number': tran.ediTransaction.gsNumber })) }}</a>
</td>
<td><a href="{{ path('matrix_edi_findAll', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'doc_type': 856}) }}"data-toggle="modal"data-target="#myModal">{{ render(controller('MatrixEdiBundle:Matrix:matrix', {
'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran.ediTransaction.receiverId, 'doc_type': 856, 'gs_number': tran.ediTransaction.gsNumber }))}}</a>
</td>
<td><a href="{{ path('matrix_edi_findAll', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'doc_type': 810}) }}"data-toggle="modal"data-target="#myModal">{{ render(controller('MatrixEdiBundle:Matrix:matrix', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran.ediTransaction.receiverId, 'doc_type': 810, 'gs_number': tran.ediTransaction.gsNumber})) }}</a>
</td>
<td><a href="{{ path('matrix_edi_findAll', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'doc_type': 860}) }}"data-toggle="modal"data-target="#myModal">{{ render(controller('MatrixEdiBundle:Matrix:matrix', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran.ediTransaction.receiverId, 'doc_type':860, 'gs_number': tran.ediTransaction.gsNumber})) }}</a>
</td>
<td><a href="{{ path('matrix_edi_findAll', {'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'doc_type': 870}) }}"data-toggle="modal"data-target="#myModal">{{ render(controller('MatrixEdiBundle:Matrix:matrix', {
'po_num': tran.poNumber, 'sender_id': tran.ediTransaction.senderId, 'reciever_id': tran.ediTransaction.receiverId, 'doc_type': 870, 'gs_number': tran.ediTransaction.gsNumber
})) }}</a></td>
</tr>
{% endfor %}
</tbody>
</table>
</div>
<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel" aria-hidden="false" >
<div class="modal-dialog">
<div class="modal-content" style="width: 650px;">
<div class="modal-header" style="background-color: #2d6ca2; color: white;">
<button type="button" class="close" data-dismiss="modal"><span aria-hidden="true" style="color: white;">×</span><span class="sr-only">Close</span></button>
<h4 class="modal-title" id="myModalLabel" >Details</h4>
</div>
<div class="modal-body">
Loading, please wait......
<div class="bootstrap-table">
<div class="fixed-table-container" style="height: 299px; padding-bottom: 37px;">
<div class="fixed-table-body">
<div class="fixed-table-loading" style="top: 27px; display: none;">Loading, please wait…</div>
</div>
<div class="fixed-table-pagination"></div>
</div>
</div><div class="clearfix"></div>
</div>
<div class="modal-footer">
<button type="button" class="btn btn-primary" data-dismiss="modal" >Close</button>
</div>
</div><!-- /.modal-content -->
</div><!-- /.modal-dialog -->
</div><!-- /.modal -->
{% endblock %}
{% block javascripts %}
{% javascripts
'bundles/matrixdoc/js/jQuery.js'
'bundles/matrixdoc/js/jquery.dataTables.min.js'
'bundles/matrixdoc/js/dataTables.bootstrap.js'
'bundles/matrixdoc/js/bootstrap.js'
%}
<script src="{{ asset_url }}"></script>
<script type="text/javascript">
$(document).ready(function() {
$('#datTable').dataTable( {
"scrollY": "400px",
"scrollCollapse": true,
"pagingType": "simple",
});
$('body').on('hidden.bs.modal', '.modal', function () {
$(this).removeData('bs.modal');
});
$(document).on("hidden.bs.modal", function (e) {
$(e.target).removeData("bs.modal").find(".modal-content").empty();
});
});
</script>
{% endjavascripts %}
{% endblock %}
{%extends'::layout.html.twig%}
{{{%include'MatrixEdiBundle:Matrix:header.html.twig%}}
{%block body%}
电子数据交换矩阵
采购订单编号
贸易伙伴Id
PO 855确认书
PO 997确认书
提前装运通知
发票联
采购订单变更
订单状态
{%用于po%中的tran}
{%endfor%}
×闭合
细节
正在装货,请稍候。。。。。。
正在加载,请稍候…
接近
{%endblock%}
{%block javascripts%}
{%javascripts
'bundles/matrixdoc/js/jQuery.js'
'bundles/matrixdoc/js/jquery.dataTables.min.js'
'bundles/matrixdoc/js/dataTables.bootstrap.js'
'bundles/matrixdoc/js/bootstrap.js'
%}
$(文档).ready(函数(){
$(“#数据表”)。数据表({
“滚动”:“400px”,
“卷轴崩溃”:没错,
“pagingType”:“简单”,
});
$('body').on('hidden.bs.modal','.modal',function(){
$(this.removeData('bs.modal');
});
$(文档).on(“hidden.bs.modal”,函数(e){
$(e.target).removeData(“bs.modal”).find(“.modal content”).empty();
});
});
{%endjavascripts%}
{%endblock%}
如果您不需要所有内容,您应该使用分页来获取数据,并使用限制和部分来获取所需字段
对于DQL中的部分,用户的简单示例如下:
//partial_fields is an array, which can be passed like this $repo->getPartialUser($user_id, array('field1', 'field2', 'etc..');
public function getPartialUser($id, $partial_fields){
$qb = $this->_em->createQueryBuilder()
->select('partial u.{'.implode(',',$partial_fields).'}')
->from('AcmeUserBundle:User', 'u')
->where('u.id = :id')
->setParameter('id', $id);
$result = $qb->getQuery()->getOneOrNullResult();
return $result;
}
编辑示例代码:
我会给你一个例子,但你可能仍然需要适应你的代码,或者它可能会开箱即用
首先,您需要一个带有简单代码的新细枝来显示表下导航的页码(请注意,我不会使用datatables paginator,但我们将保留它用于表布局渲染):
pager.html.twig
PS:根据数据负载的不同,将限制更改为每页显示多少条记录,如果速度慢50,则降低限制。这取决于每行有多少数据
下一步是控制器中的索引操作:
public function indexAction($page){
$em = $this->getDoctrine()->getManager();
$po = $em->getRepository('MatrixEdiBundle:EdiTransactionDetail')->getPagedDocs($page, 50);
$count = $po->count();
$pagination = array(
'page' => $page,
'route' => 'docs_index_route_name', //i dont know which name you have but it should be the route name of this indexAction
'route_params' => array()
);
if ($max_records > 0)
$pagination['pages_count'] = max(ceil($count / $max_records), 1);
return $this->render('MatrixEdiBundle:Matrix:index.html.twig', array('po' => $po,
'pagination' => $pagination
));
}
路由:
您需要修改索引的路径,因为我们添加了一个参数页,您也需要为路径添加该参数页:
index_docs:
pattern: /index/{page}
defaults: { _controller: "YourBundle:Controller:index", page: 1 }
注意:您需要更改名称和内容我不知道您的控制器名称和捆绑包,因此通常您只需要将/{page}添加到模式中,并将page:1添加到_controllerconfig中,这是默认值
最后要做的是index.html.twig,它需要包含我们的pager.html.twig
在关闭表格后包含以下代码:
{% if po|length > 0 and pagination['pages_count'] is defined and pagination['pages_count'] > 0 %}
{#---------Pager----------#}
<div style="text-align:center;">
{% include 'YourBundle:EntityDoc:pager.html.twig' %}
</div>
{% endif %}
{%if po|length>0且定义了分页['pages_count'],且分页['pages_count']>0%}
{#------寻呼机----------------}
{%include'YourBundle:EntityDoc:pager.html.twig%}
{%endif%}
注意:include是您创建pager.html.twig的文件的路径,我假设您熟悉它的工作原理
因此,如果我没有忘记任何事情,这应该是开箱即用的,它应该会大大提高您的性能,但我担心您的渲染控制器在那里的每一行可能仍然有问题。但是,请先尝试此解决方案,然后看,这取决于您在这些控制器中放置了什么。我不明白您想做什么
Flush
只是将更改保存到数据库中,您是在尝试获取数千行还是保存这些对象?获取并可能保存这些数据。条令中有一种称为批处理的东西。。你可以试一试,但我不能说flush
可以优化某些东西..我只想问,,,,这就是为什么我因为在数据库中获取了很多行而出现内存不足错误的原因?我可以用什么方法优化数据处理?您能帮我修改Can,使其不会出现内存不足错误吗?在我的表中,我使用的是数据表。它已经有了上一个和下一个,所以看起来我仍然有我的分页。你想让我发布我的代码吗?显然,你没有使用ajax作为datatable的分页器,因为datatable的分页器现在正在获取所有已经加载的记录,但是要优化,你需要使用ajax为每个页面获取记录,我使用datatable ajax效率不高,但在发布代码后,我会看看我能做些什么getDoctrine()和getManager()的用法是什么?getManager()和getEntityManager()之间的区别是什么?在哪里可以插入您提供的函数?顺便说一句,我刚刚接触symfony,我想从中学习。公共函数getEntityManager($name=null){trigger_error('getEntityManager在symfony 2.1之后已弃用。改用getManager',E_USER_弃用);返回$this->getManager($name);}它只是在较新版本的symfony中的早期版本getManager中出现的,不推荐使用的函数通常会调用较新的函数版本,正如您在上面所看到的,直到稍后将其完全删除为止。对于datatables,您可以使用这个捆绑包,它看起来非常可靠,或者您可以从中获得灵感,并使用自己的编码
index_docs:
pattern: /index/{page}
defaults: { _controller: "YourBundle:Controller:index", page: 1 }
{% if po|length > 0 and pagination['pages_count'] is defined and pagination['pages_count'] > 0 %}
{#---------Pager----------#}
<div style="text-align:center;">
{% include 'YourBundle:EntityDoc:pager.html.twig' %}
</div>
{% endif %}