diff --git a/application/helpers/admin/activate_helper.php b/application/helpers/admin/activate_helper.php index 4c9a94bb701..f080b724beb 100644 --- a/application/helpers/admin/activate_helper.php +++ b/application/helpers/admin/activate_helper.php @@ -26,7 +26,7 @@ function fixNumbering($fixnumbering, $iSurveyID) LimeExpressionManager::RevertUpgradeConditionsToRelevance($iSurveyID); //Fix a question id - requires renumbering a question $oldqid = (int) $fixnumbering; - $lastqid=Questions::model()->getMaxId(); + $lastqid=Questions::model()->getMaxId('qid', true); // Always refresh as we insert new qid's $newqid=$lastqid+1; // Not sure we can do this in MSSQL ? diff --git a/application/models/LSActiveRecord.php b/application/models/LSActiveRecord.php index c5dfdf08abd..1f37dd82aec 100644 --- a/application/models/LSActiveRecord.php +++ b/application/models/LSActiveRecord.php @@ -103,6 +103,42 @@ public function beforeDelete() { $result = App()->getPluginManager()->dispatchEvent(new PluginEvent('before'.get_class($this).'Delete', $this)); return parent::beforeDelete(); - } + } + + /** + * Return the max value for a field + * + * This is a convenience method, that uses the primary key of the model to + * retrieve the highest value. + * + * @param string $field The field that contains the Id, when null primary key is used if it is a single field + * @param boolean $forceRefresh Don't use value from static cache but always requery the database + * @return false|int + */ + public function getMaxId($field = null, $forceRefresh = false) { + static $maxIds = array(); + + if (is_null($field)) { + $primaryKey = $this->primaryKey(); + if (is_string($primaryKey)) { + $field = $primaryKey; + } else { + // Composite key, throw a warning to the programmer + throw new Exception(sprintf('Table %s has a composite primary key, please explicitly state what field you need the max value for.', $this->tableName())); + } + } + + if ($forceRefresh || !array_key_exists($field, $maxIds)) { + $maxId = $this->dbConnection->createCommand() + ->select('MAX(' . $this->dbConnection->quoteColumnName($field) . ')') + ->from($this->tableName()) + ->queryScalar(); + + // Save so we can reuse in the same request + $maxIds[$field] = $maxId; + } + + return $maxIds[$field]; + } } \ No newline at end of file diff --git a/application/models/Question.php b/application/models/Question.php index fa5a8e9d945..e5a48bccb10 100644 --- a/application/models/Question.php +++ b/application/models/Question.php @@ -96,24 +96,6 @@ public function rules() ); } - /** - * Return the max id (primary key) - * - * Actually used in activate_helper fixNumbering function - * Don't use static because can be call after adding a question - * - * @since 130711 - * @return false|int - */ - public function getMaxId() - { - $maxId = $this->dbConnection->createCommand() - ->select('MAX(qid)') - ->from($this->tableName()) - ->queryScalar(); - return $maxId; - } - /** * Rewrites sort order for questions in a group * diff --git a/application/models/SurveyDynamic.php b/application/models/SurveyDynamic.php index 4b54a25b0b1..5524698972b 100644 --- a/application/models/SurveyDynamic.php +++ b/application/models/SurveyDynamic.php @@ -261,28 +261,6 @@ public function exist($srid) return $exist; } - /** - * Return the max id (primary key) - * - * This is used in export, when using the record id instead of the row number (count of records) - * - * @staticvar null $maxId - * @return false|int - */ - public function getMaxId() - { - static $maxId = null; - - if (is_null($maxId)) { - $maxId = $this->dbConnection->createCommand() - ->select('MAX(' . $this->primaryKey() . ')') - ->from($this->tableName()) - ->queryScalar(); - } - - return $maxId; - } - /** * Return next id if next response exist in database *