Opps, forgot this.
[core.git] / inc / classes / main / criteria / dataset / class_DataSetCriteria.php
index 1c03be5d3f043040a749ebc00501270f9d01d28d..2745239433474cf09ec96c9caced5bdb72b26e6b 100644 (file)
@@ -37,6 +37,11 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
         */
        private $primaryKey = '';
 
+       /**
+        * Primary keys
+        */
+       private $primaryKeys = array();
+
        /**
         * Protected constructor
         *
@@ -108,7 +113,58 @@ 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(s) first
+               $primaryKey  = trim($this->getCriteriaElemnent($this->getPrimaryKey()));
+               $primaryKeys = $this->getPrimaryKeys();
+
+               // Debug message
+               //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',primaryKey=' . $primaryKey . ',primaryKeys()=' . count($primaryKeys));
+
+               /*
+                * If this is not set, this could mean a badly written frontend as
+                * tables should always have a primary key.
+                */
+               if (count($primaryKeys) > 0) {
+                       /*
+                        * Init return value, this can be put all together without any
+                        * separator as it only aids as a "unique value" for generating the
+                        * "row file name".
+                        */
+                       $return = '';
+
+                       // Combination set, so get all
+                       foreach ($primaryKeys as $primaryKey) {
+                               // Add it
+                               $return .= trim($this->getCriteriaElemnent($primaryKey));
+                       } // END - foreach
+
+                       // Debug message
+                       //* NOISY-DEBUG: */ self::createDebugInstance(__CLASS__)->debugOutput('[' . __METHOD__ . ':' . __LINE__ . ']: tableName=' . $this->getTableName() . ',return=' . $return . ' - EXIT!');
+
+                       // Return it
+                       return $return;
+               } elseif (!empty($primaryKey)) {
+                       // Return primary key
+                       return $primaryKey;
+               } else {
+                       // @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;
+                       }
+               }
        }
 
        /**
@@ -138,6 +194,26 @@ class DataSetCriteria extends BaseCriteria implements StoreableCriteria {
        public final function setPrimaryKey ($primaryKey) {
                $this->primaryKey = (string) $primaryKey;
        }
+
+       /**
+        * Setter for primary key array
+        *
+        * @param       $primaryKeys    Primary key array to set
+        * @return      void
+        */
+       public function setPrimaryKeyCombined (array $primaryKeys) {
+               $this->primaryKeys = $primaryKeys;
+       }
+
+       /**
+        * Getter for primary keys
+        *
+        * @return      $primaryKeys    Primary key array
+        */
+       public final function getPrimaryKeys () {
+               // Return it
+               return $this->primaryKeys;
+       }
 }
 
 // [EOF]