public function _joinRelated(array $related) { $affiliate = NULL; foreach ($related as $identifier => $affiliate) { if ($affiliate) { break; } } if (!$affiliate) { $affiliate = Util::newID(); } $store = $this->identifierStore(); $remove = array(); foreach ($related as $identifier => $_id) { $related[$identifier] = $affiliate; $store->set($identifier, $affiliate); if ($_id && $_id != $affiliate) { $remove[$_id] = $_id; } } $store = $this->affiliateStore(); foreach ($remove as $_id) { $store->delete($_id); } $store->set($affiliate, array_keys($related)); return $related; }
public function _joinRelated(array $related) { $affiliation = NULL; foreach ($related as $identifier => $affiliation) { if ($affiliation) { break; } } if (!$affiliation) { $affiliation = Util::newID(); } $db = $this->db(); $table = $this->table(); $local_txn = DB\Transaction::claimStart(); DB\Transaction::add($db); $sql_insert = "INSERT OR IGNORE INTO `{$table}` (`identifier`, `affiliation`) VALUES (%s, %i)"; $sql_update = "UPDATE `{$table}` set `affiliation` = %i WHERE `identifier` = %s"; foreach ($related as $identifier => $_id) { if ($_id == $affiliation) { continue; } $related[$identifier] = $affiliation; $rs = $db->execute($sql_insert, $identifier, $affiliation); if ($rs->affected() < 1) { $db->execute($sql_update, $affiliation, $identifier); } } if ($local_txn && !DB\Transaction::commit()) { throw new Exception('database error: unable to commit transaction', $db); } return $related; }
public function _joinRelated(array $related) { $db = $this->db(); $table = $this->table(); if (!DB\Transaction::atStart()) { DB\Transaction::add($db); } $affiliation = NULL; foreach ($related as $identifier => $affiliation) { if ($affiliation) { break; } } if (!$affiliation) { $affiliation = Util::newID(); } $clauses = array(); foreach ($related as $identifier => $_id) { if ($_id == $affiliation) { continue; } $related[$identifier] = $affiliation; $clauses[] = $db->prep_args('(%s, %s, %i)', array($identifier, sha1($identifier, TRUE), $affiliation)); } if (!$clauses) { return $related; } $sql = "INSERT INTO `{$table}` (`identifier`, `hash`, `affiliation` ) VALUES " . implode(',', $clauses) . ' ON DUPLICATE KEY UPDATE `affiliation` = VALUES( `affiliation` )'; $db->execute($sql); return $related; }