Added a public methods to interface + some logger messages.
[core.git] / inc / classes / main / criteria / dataset / class_DataSetCriteria.php
index 1c03be5d3f043040a749ebc00501270f9d01d28d..323a893d56423ab5cf5cd59e39cac0d793b4ec8c 100644 (file)
@@ -108,7 +108,38 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         * @return      $uniqueValue    Value of the unique key
         */
        public final function getUniqueValue () {
-               return $this->getCriteriaElemnent($this->getUniqueKey());
+               // Get primary key first
+               $primaryKey = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey);
+
+               /*
+                * If this is not set, this could mean a badly written frontend as
+                * tables should always have a primary key.
+                */
+               if (empty($primaryKey)) {
+                       // @TODO Issue a warning
+                       self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: Primary key not set for table ' . $this->getTableName() . ', please fix your table. Falling back to unique key ...');
+
+                       // Get unique key
+                       $uniqueKey = trim($this->getCriteriaElemnent($this->getUniqueKey()));
+
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',uniqueKey=' . $uniqueKey);
+
+                       // Is it empty, too?
+                       if (empty($uniqueKey)) {
+                               // Bad news, nothing is "unique" by design for this table
+                               ApplicationEntryPoint::app_exit('Table ' . $this->getTableName() . ' has both no primary and unique key, but ' . __METHOD__ . ' was called. Please fix your table.');
+                       } else {
+                               // Return unique key
+                               return $uniqueKey;
+                       }
+               } else {
+                       // Return primary key
+                       return $primaryKey;
+               }
        }
 
        /**