Laravel firstOrNew为相同数据创建重复条目

我的应用程序中有以下特定代码:</ p>

  if  ($ request-&gt; filled('jawaban')){
$ jawaban = JawabanUserM :: where('user_id',$ user-&gt; id)
  • &gt; where('paket_id',$ kuis [' paket_id']) - &gt; firstOrNew(['soal_id'=&gt; $ kuis ['curr_id']]);
    if($ jawaban-&gt; jawaban_id!= $ request-&gt; jawaban){
    $ jawaban - &gt; user_id = $ user-&gt; id;
    $ jawaban-&gt; paket_id = $ kuis ['paket_id'];
    $ jawaban-&gt; soal_id = $ kuis ['curr_id'];
    $ jawaban - &gt; jawaban_id = $ request-&gt; jawaban;
    $ jawaban-&gt; save();
    }
    }
    </ code> </ pre>

    它基本上插入答案 从用户到表JawabanUserM </ p>

    我在部署时运行此应用程序。

    使用laravel 5.5,mysql 5.5.58,nginx 1.12.2,php-fpm 7.0.27。</ p>

    一次只有800-1000 ++普通用户。 使用InnoDB引擎。</ p>

    当我查看时,我发现了一些重复的记录。 我检查使用这个SQL:</ p>

      SELECT 
    id,soal_id,user_id,jawaban_id,paket_id,COUNT(*)
    FROM
    jawabanuserm
    GROUP BY
    soal_id,user_id ,jawaban_id,paket_id
    HAVING
    COUNT(*)&gt; 1
    ORDER BY COUNT(*)DESC
    </ code> </ pre>

    结果为:</ p>

      id |  soal_id |  user_id |  jawaban_id |  paket_id |  COUNT(*)
    4702 | 117 | 100 | 401 | 6 | 6
    4694 | 111 | 100 | 401 | 6 | 5
    15080 | 110 | 1300 | 1077 | 7 | 5
    8097 | 186 | 435 | 686 | 6 | 4
    4679 | 97 | 100 | 401 | 6 | 4
    ....
    </ code> </ pre>

    任何解决此问题的建议以及为什么会发生这种情况?</ p>

    起初我 坚信这是由于流程之间沟通不畅造成的。 我向我的同事询问他们建议使用程序,但这会影响我认为的表现。</ p>
    </ div>

展开原文

原文

I have this specific code from my application:

if ($request->filled('jawaban')) {
                $jawaban = JawabanUserM::where('user_id', $user->id)
                    ->where('paket_id', $kuis['paket_id'])->firstOrNew(['soal_id'=>$kuis['curr_id']]);
                if($jawaban->jawaban_id != $request->jawaban) {
                    $jawaban->user_id = $user->id;
                    $jawaban->paket_id = $kuis['paket_id'];
                    $jawaban->soal_id = $kuis['curr_id'];
                    $jawaban->jawaban_id = $request->jawaban;
                    $jawaban->save();
                }
        }

it basicly insert answer from user to table JawabanUserM

I run this application on deployment. using laravel 5.5, mysql 5.5.58, nginx 1.12.2, php-fpm 7.0.27.

with 800-1000++ average user at a time. using InnoDB engine.

when i check around i found some duplicate records. i check using this sql :

SELECT
    id, soal_id, user_id, jawaban_id, paket_id, COUNT(*)
FROM
    jawabanuserm
GROUP BY
    soal_id, user_id, jawaban_id, paket_id
HAVING 
    COUNT(*) > 1
ORDER BY COUNT(*) DESC

the result was :

id | soal_id | user_id | jawaban_id | paket_id | COUNT(*)
4702 | 117 | 100 | 401 | 6 | 6
4694 | 111 | 100 | 401 | 6 | 5
15080 | 110 | 1300 | 1077 | 7 | 5
8097 | 186 | 435 | 686 | 6 | 4
4679 | 97 | 100 | 401 | 6 | 4
....

Any advice to workaround this problem and why this is happening?

at first i strongly believe this is caused by bad communicating between process. i have ask to my colleague they suggest using procedure, but it will impact the performance i think.

dsfdfd1211
dsfdfd1211 在数据库级别,您可以拥有soal_id,user_id,jawaban_id,paket_id(一起)的唯一索引,这将导致重复输入时出错。然而,你真正的问题是firstOrNew基本上会忽略你的其他条件,只是做自己的查询,所以你需要找到一个不同的方法(理想情况下,这看起来更适合作为一个关系,而不是一个模型)
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐