00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBConstraintUnique extends DBConstraint {
00009
00010
00011
00012
00013
00014
00015 public function validate($arr_fields, $key_fields) {
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
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
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 }
00069 }