如何检查多对多集合的唯一性? [英] How to check uniqueness of many-to-many collection?
问题描述
- 用户提交了一个集合,这是我的PHP应用程序所需的流程(是的,它含糊不清,但它更容易)比方说,通过整数ID大约5个对象。 (它实际上更像15,但为了方便起见,我们假设为5)。
- 应用程序检查此集合是否已经提交过,并将它保存在MySQL数据库中(如果不是 li>
- 应用程序将这些对象保存到数据库中(如果它们之前未保存)并且集合是多对多的,所以有一个对象表,一个集合表和一个关联这两者的表。)
一对样本流: p>
- 用户提交111,112,113,114
- 这套是新的!
- 我们已经看到了对象111和112,但是为113和114提取并保存数据,因为我们还没有。
- 这套是新的!
- 另一个用户提交111,112,113,114
- 我们以前看过这个集合。不要打扰储蓄。
- 由于我们已经看到了这个集合,我们很明显看到了这些对象。不要打扰储蓄。
步骤1和3很简单。第2步是我不知道如何继续。看起来不必要的数据库繁重的是要查询包含这些确切ID的集合的关系,所以我即将发布一些明显的解决方案,例如简单的ID列表和散列,但我也想知道是否还有更多理想的解决方案。
谢谢!
解决方案,我会这样做:
- 在用户提交后,在进入数据库之前,我会将数据库中的数据提取到数组中。在你的例子中,我将有
$ collection = array('111','112','113','114');
-
我将分两步检查新的用户输入。第一步是查看它是否已经存在于数据库中。如果不是,则插入。否则忽略:
$ fore $($输入为$输入)
{
if(!in_array($ input,$ collection))
{
//在此处插入
}
}
然后在第二个循环中,我反过来删除未被用户选中的数据。
foreach($ collection as $ data )
{
if(!in_array($ data,$ inputs))
{
// do delete here
}
}就你而言,你可能需要也可能不需要第二个循环。我需要这个,因为我把输入作为复选框,用户可以选择激活/停用,因此我把它翻译为插入和删除。
Here's the desired flow of my PHP app (yes, it's vague, but it's easier that way):
- User submits a set of, let's say, about 5 objects by integer IDs. (It'll really be more like 15, but let's say 5 for ease.)
- App checks if this collection has been submitted before, and saves it in a MySQL database if not
- App saves these objects in the database, if they haven't been saved before
(Objects and collections are many-to-many, so there is an objects table, a collections table, and a table relating the two.)
A couple sample flows:
- User submits 111, 112, 113, 114
- This set is new! The collection is saved.
- We've seen objects 111 and 112, but fetch and save the data for 113 and 114, since we haven't.
- Another user submits 111, 112, 113, 114
- We've seen this collection before. Don't bother saving.
- Since we've seen the collection, we've obviously seen the objects. Don't bother saving.
Steps 1 and 3 are simple. Step 2 is where I'm not sure how to proceed. It seems unnecessarily database-heavy to be querying the relationship for sets containing those exact IDs, so I'm about to post a few obvious solutions such as a simple ID list and hashing, but I'd also like to know if there are more ideal solutions out there.
Thanks!
解决方案In my application, I will do this step:
- After user submit, and before entering to database, I will fetch the data in the database into an array. In your example above, I will have
$collection = array('111', '112', '113', '114');
I will check the new user input in two step. First step is to see if it already in the database or not. If it not, then insert. Otherwise ignore:
foreach ( $inputs as $input ) { if ( ! in_array($input, $collection) ) { //do insert here } }
Then in second loop, I do it in reverse, to delete the data that not selected by user.
foreach ( $collection as $data ) { if ( ! in_array($data, $inputs) ) { //do delete here } }
In your case, you might or might not need the second loop. I needed this since I make the input as checkboxes, that the user can choose to activate / deactivate, thus I translate it as insert and delete.
这篇关于如何检查多对多集合的唯一性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
- 用户提交111,112,113,114