<EDIT>
该问题与通过 GET 传递数组并在某些 AJAX 代码中达到 URL 长度限制有关。我通过切换到 POST 方法解决了这个问题。
</EDIT>
我在工作中使用了一门课程来批量重新分配 SugarCRM 数据库中的潜在客户。
除了这个功能外,其他一切都在课堂上完美运行:
function reassign($leads = null, $newAssigned = null, $oldAssigned = null)
{
// How many professionals to assign to?
$roundrobin = count($newAssigned) - 1;
$recurse = 0;
if (!isset($leads)) {
die("You must select the accounts to reassign!");
}
if (!isset($newAssigned)) {
die("You must select a Professional!");
}
if (!isset($oldAssigned)) {
die("Something broke in the Assigning process. Check javascript.");
}
try {
foreach ($leads as $key => $value) {
$sql_update = "UPDATE accounts SET assigned_user_id = '".$newAssigned[$recurse]."' WHERE id = '".$value."'";
$assignment = $this->db_handle->prepare($sql_update);
$assignment->execute();
unset($assignment);
$sql_statement = "INSERT INTO accounts_audit
(id,
parent_id,
date_created,
created_by,
field_name,
data_type,
before_value_string,
after_value_string)
VALUES (uuid(),
'".$value."',
now(),
'".$_SESSION['session_user_id']."',
'assigned_user_id',
'relate',
'".$oldAssigned[$key]."',
'".$newAssigned[$recurse]."')";
$audit_statement = $this->db_handle->prepare($sql_statement);
$audit_statement->execute();
unset($audit_statement);
if ($recurse < $roundrobin) {
$recurse++;
} else {
$recurse = 0;
}
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "Successful!\n";
}
此功能允许用户从生成的列表中检查任意数量的潜在客户,并将这些潜在客户平等地重新分配给列表中的任意数量的代表,并将这些更改记录在表格中以用于审计目的。
我遇到的问题是,如果用户从 20-25 条左右的线索中选择任何地方,这个功能就会卡住并挂起。任何小于 20 并且它永远不会挂起。这是一个问题,因为用户必须能够重新分配数百或可能数千个潜在客户。
关于它为什么会这样锁定的任何想法(它锁定的潜在客户数量各不相同,通常是 20-25)?当潜在客户数低于 20 时,变化会立即发生。
编辑:
更新了代码以显示语句关闭。行为仍然没有变化。When the number of leads are selected that causes it to break, no changes are made. 它不会执行第一个 19-20 并在其余部分停止,不会进行任何更改,也不会将任何内容添加到审计表中。小于断点并且所有更改都已完成并记录在审计表中。
我正在使用 PDO 连接到数据库:
try {
$this->db_handle = new PDO("mysql:host=$this->dbHost;dbname=$this->dbDatabase", $this->dbUsername, $this->dbPassword);
$this->db_handle->setAttribute(PDO::ATTR_ERRMODE, PDO::ERRMODE_EXCEPTION);
} catch(PDOException $e) {
echo 'ERROR: ' . $e->getMessage();
}
我在那里没有错误。对于任何代码,我也没有在日志中收到任何错误。我感到很困惑。
编辑#2:
根据建议,这与绑定参数相同......
function reassign($leads = null, $newAssigned = null, $oldAssigned = null)
{
// How many professionals to assign to?
$roundrobin = count($newAssigned) - 1;
$recurse = 0;
$sql_assign = "UPDATE accounts SET assigned_user_id = :new_user_id WHERE id = :account_id";
$sql_audit = "INSERT INTO accounts_audit
(id,
parent_id,
date_created,
created_by,
field_name,
data_type,
before_value_string,
after_value_string)
VALUES (uuid(),
:parent_id,
now(),
:created_by,
'assigned_user_id',
'relate',
:old_assigned_user,
:new_assigned_user)";
$assignment = $this->db_handle->prepare($sql_assign);
$audit = $this->db_handle->prepare($sql_audit);
if (!isset($leads)) {
die("You must select the accounts to reassign!");
}
if (!isset($newAssigned)) {
die("You must select a Professional!");
}
if (!isset($oldAssigned)) {
die("Something broke in the Assigning process. Check javascript.");
}
try {
foreach ($leads as $key => $value) {
$assign_params = array(':new_user_id' => $newAssigned[$recurse], ':account_id' => $value);
$assignment->execute($assign_params);
$audit_params = array(':parent_id' => $value, ':created_by' => $_SESSION['session_user_id'], ':old_assigned_user' => $oldAssigned[$key], ':new_assigned_user' => $newAssigned[$recurse]);
$audit->execute($audit_params);
if ($recurse < $roundrobin) {
$recurse++;
} else {
$recurse = 0;
}
}
catch (Exception $e) {
echo 'Caught exception: ', $e->getMessage(), "\n";
}
echo "Successful!\n";
}
没有区别,除了在它起作用时速度更快。:(