.

DBConstraintUnique Class Reference
[Model]

A unique constraint. More...

Inheritance diagram for DBConstraintUnique:
DBConstraint PolicyHolder IDBConstraint IPolicyHolder

List of all members.

Public Member Functions

  validate ($arr_fields, $key_fields)
  Check if constraints are fullfiled.

Detailed Description

A unique constraint.

Author:
Gerd Riesselmann

Definition at line 8 of file dbconstraint.unique.cls.php.


Member Function Documentation

DBConstraintUnique::validate ( arr_fields,
key_fields  
)

Check if constraints are fullfiled.

Parameters:
array  Associative array of form fieldname => fieldvalue
Returns:
Status

Reimplemented from DBConstraint.

Definition at line 15 of file dbconstraint.unique.cls.php.

00015                                                            {
00016                 $ret = new Status();
00017                 
00018                 $dao = DB::create($this->tablename);
00019                 $query = new DBQuerySelect($dao);
00020                 $query->set_fields(array('count(*)' => 'c'));
00021                 
00022                 // Set unique conditions
00023                 $unique_fields = $this->get_fields();
00024                 foreach($unique_fields as $col_name) {
00025                         $col_value = Arr::get_item($arr_fields, $col_name, null);
00026                         $query->add_where($col_name, '=', $col_value);
00027                 }               
00028                 
00029                 // Add keys
00030                 foreach($key_fields as $col_name => $col_value) {
00031                         if (!is_null($col_value)) {
00032                                 $query->add_where($col_name, '!=', $col_value);                 
00033                         }
00034                 }
00035                 
00036                 $result = DB::query($query->get_sql(), $dao->get_table_driver());
00037                 $arr_result = $result->fetch();
00038 
00039                 if ($arr_result['c'] > 0) {
00040                         $num_unique_fields = count($unique_fields);
00041                         $tr_unique_fields = array();
00042                         foreach($unique_fields as $col_name) {
00043                                 $tr_unique_fields[] = tr($col_name, 'global'); 
00044                         }
00045                         $err_msg_fields = implode(', ', $tr_unique_fields);
00046                          
00047                         if ($num_unique_fields == 1) {
00048                                 $ret->append(tr(
00049                                         'There are already records for the provided value of %field',
00050                                         'core',
00051                                         array(
00052                                                 '%field' => $err_msg_fields
00053                                         )
00054                                 ));
00055                         }
00056                         else {
00057                                 $ret->append(tr(
00058                                         'There are already records for the given combination of %fields',
00059                                         'core',
00060                                         array(
00061                                                 '%fields' => $err_msg_fields
00062                                         )
00063                                 ));                             
00064                         }
00065                 }
00066                 
00067                 return $ret;
00068         }

The documentation for this class was generated from the following file: