我们公司正在创建(非常活跃)注册表格。处理这些数据的应用程序是Laravel 5应用程序。我正试图消除注册处理脚本中的所有不必要的查询。但是请注意,它是甚至更重要我们正确记录每个注册。
现在,我写了这个到应用程序流:
$entry_timestamp = time();
DB::table('form_registration_data')->insert($userDataArray);
$matchArray = array('last_name' => $last_name, 'entry_timestamp' => $entry_timestamp);// the (required field last_name/unix entry_timestamp) pair is (safely IMO) assumed to be a unique "ID"
$sql = DB::table('form_registration_data')->select('entry_id')->where($matchArray)->first();
if (!empty($sql)) {
//from this point script knows user data was recorded properly
} else {
$errorMsg = 'There was a problem with the server. Please try again.";
}
我之所以写了一个单独的选择查询我不是100%一定的,只是因为插入查询没有失败这个手段它正确执行和数据被正确地记录在DB(请记住,这是最重要的方面)
换句话说,这就是为什么我没有写这样的剧本:
$sql = DB::table('form_registration_data')->insert($userDataArray);
if ($sql) {
//was the data truly recorded properly - 100% guaranteed ???
} else {
$errorMsg = 'There was a problem with the server. Please try again.";
}
我太过分了吗?换句话说,插入查询GUARANTEE的非失败是否在DB中记录了正确的数据?
为什么不尝试获取它的基础上'LAST_INSERT_ID()',看看它是否在那里,以确保?如果它没有失败它可能成功了,但是不能保证别的东西没有删除它,或者将来不会删除它。编程充满了不确定性。 – tadman
同意,如果您使用ORM并为该变量分配要保存的值。您可以使用$ data-> id来检查保存。如果id不在那里,它不会保存。 – arcee123
@tadman真实 - 但问题集中在脚本执行的时候 - 而不是未来的删除。该脚本的其他部分没有任何删除功能 –