如何在理论中对一列重新编号 [英] How to renumber a column in doctrine
本文介绍了如何在理论中对一列重新编号的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在使用教义在zf2中开发一个项目,我需要创建一种方法来对 order
字段重新编号,以使这些值是连续的。之前:
I am developing a project in zf2 using doctrine and I need to create a method to renumber the order
field so that the values are sequential. Before:
+-----+-------+--------+------------+
| id | order | item | collection |
+-----+-------+--------+------------+
| 987 | 1 | apple | fruits |
| 46 | 2 | banana | fruits |
| 394 | 7 | grape | fruits |
| 265 | 30 | pear | fruits |
| 89 | 1 | squash | vegetables |
+-----+-------+--------+------------+
之后:
+-----+-------+--------+------------+
| id | order | item | collection |
+-----+-------+--------+------------+
| 987 | 1 | apple | fruits |
| 46 | 2 | banana | fruits |
| 394 | 3 | grape | fruits |
| 265 | 4 | pear | fruits |
| 89 | 1 | squash | vegetables |
+-----+-------+--------+------------+
订单顺序由收集
完成,但我不需要重新编号的方法整个数据集;
The order sequences are by collection
, but I don’t need the method to renumber the entire dataset; just the records in a particular collection.
我正在考虑的一些解决方案包括:
Some of the solutions I am considering include:
- 按顺序将相关记录转储到新表中,
- 添加一个名为<$的字段c $ c> new_order 是一个自动编号字段,
- 加入
id
字段中的表,然后更新current_table.order =
,
new_table.new_order - 删除临时表。
- Dump the pertinent records in order into a new table,
- Add a field called
new_order
that is an autonumber field, - Join the tables on the
id
field and updatecurrent_table.order = new_table.new_order
, - Delete the temporary table.
循环浏览记录并一次更新一个:
Cycle Through the Records and Update one at a Time:
$collection = … // results from select query where collection=fruits
n = 1;
For each ($collection as $item) {
// update query to set order=n where id=$item[id]
n += 1
}
还有其他想法吗?
推荐答案
请非常肯定地使用第二种方法... IE
Very much, definitely, please, use the 2nd method... I.E. cycle through records and update.
不使用临时表的快速原因:
Quick reason for not using temp tables:
- 如果您正在使用MySQL临时表,则该表对当前会话可见;如果您使用持久连接,则实际上可以由多个会话共享。如果您同时运行两次脚本,则可能导致某些数据损坏。创建真实表也是如此。
您应该做的是:
- 检索所有数据,或至少以逻辑批次检索它们(在这种情况下,可以仅检索特定集合的行,例如水果)
- 对行进行排序(之前也可以在SQL查询中完成)
- 使用计数器来更新行,就像您建议的一样
这篇关于如何在理论中对一列重新编号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文