Exemple #1
0
 /**
  * Check unique field combinations.
  * The function is called by the validate() method automatically. It is
  * not necessary to call this manually in a validation process.
  * Errors that are found are stored in the $record parameter.
  *
  * @param array $record The record to validate
  */
 public function validateUniqueFieldSets(&$record)
 {
     $db = $this->m_nodeObj->getDb();
     foreach ($this->m_nodeObj->m_uniqueFieldSets as $uniqueFieldSet) {
         $query = $db->createQuery();
         $query->addField('*');
         $query->addTable($this->m_nodeObj->m_table);
         $attribs = [];
         foreach ($uniqueFieldSet as $field) {
             $attrib = $this->m_nodeObj->m_attribList[$field];
             if ($attrib) {
                 $attribs[] = $attrib;
                 if (method_exists($attrib, 'createDestination') && isset($attrib->m_refKey) && is_array($attrib->m_refKey) && count($attrib->m_refKey) > 1) {
                     $attrib->createDestination();
                     foreach ($attrib->m_refKey as $refkey) {
                         $query->addCondition($query->quoteField($refkey) . " = '" . $db->escapeSQL($record[$attrib->fieldName()][$refkey]) . "'");
                     }
                 } else {
                     if (!$attrib->isNotNullInDb() && $attrib->isEmpty($record)) {
                         $query->addCondition($query->quoteField($field) . ' IS NULL');
                     } else {
                         $query->addCondition($query->quoteField($field) . " = '" . $attrib->value2db($record) . "'");
                     }
                 }
             } else {
                 Tools::atkerror("Field {$field} is mentioned in uniquefieldset but does not exist in " . $this->m_nodeObj->atkNodeUri());
             }
         }
         if ($this->m_mode != 'add') {
             $query->addCondition('NOT (' . $this->m_nodeObj->primaryKey($record) . ')');
         }
         if (count($db->getRows($query->buildSelect())) > 0) {
             Tools::atkTriggerError($record, $attribs, 'error_uniquefieldset');
         }
     }
 }