00001 <?php
00002
00003
00004
00005
00006
00007
00008 class DBRelation implements IDBRelation {
00009
00010
00011
00012 const NOT_NULL = 1;
00013
00014 const ONE_TO_ONE = 1;
00015 const ONE_TO_MANY = 2;
00016 const MANY_TO_MANY = 3;
00017
00018 protected $target_table;
00019 protected $arr_fields = array();
00020 protected $policy;
00021 protected $type;
00022
00023 public function __construct($target_table, $fields = null, $policy = self::NOT_NULL, $type = self::ONE_TO_MANY) {
00024 $this->target_table = $target_table;
00025 $this->policy = $policy;
00026 $this->type = $type;
00027
00028 if (!empty($fields)) {
00029 foreach(Arr::force($fields) as $field) {
00030 $this->add_field_relation($field);
00031 }
00032 }
00033 }
00034
00035
00036
00037
00038
00039
00040 public function get_target_table_name() {
00041 return $this->target_table;
00042 }
00043
00044
00045
00046
00047
00048
00049 public function get_fields() {
00050 return $this->arr_fields;
00051 }
00052
00053
00054
00055
00056
00057
00058 public function get_reversed_fields() {
00059 $ret = array();
00060 foreach($this->get_fields() as $key => $field) {
00061 $ret[$field->get_target_field_name()] = $field->reverse();
00062 }
00063 return $ret;
00064 }
00065
00066
00067
00068
00069
00070
00071 public function get_null_allowed() {
00072 return !Common::flag_is_set($this->policy, self::NOT_NULL);
00073 }
00074
00075
00076
00077
00078
00079
00080
00081
00082
00083
00084 public function validate($arr_fields) {
00085 $ret = new Status();
00086
00087 $dao = DB::create($this->target_table);
00088
00089 $b_all_fields_null = true;
00090 foreach($this->get_fields() as $column => $relation) {
00091
00092 $value = Arr::get_item($arr_fields, $column, null);
00093 $b_all_fields_null = $b_all_fields_null && is_null($value);
00094
00095
00096 $target_field = $relation->get_target_field_name();
00097 $dao->$target_field = $value;
00098 }
00099
00100 if ($b_all_fields_null) {
00101 if (!$this->get_null_allowed()) {
00102 $ret->append(tr(
00103 'No instance set for relation to table %target',
00104 'core',
00105 array(
00106 '%target' => tr($this->get_target_table_name(), 'global')
00107 )
00108 ));
00109 }
00110 }
00111 else if ($dao->count() == 0) {
00112
00113 $ret->append(tr(
00114 'No matching instance found for relation to table %target',
00115 'core',
00116 array(
00117 '%target' => tr($this->get_target_table_name(), 'global')
00118 )
00119 ));
00120 }
00121 return $ret;
00122 }
00123
00124 public function add_field_relation(IDBFieldRelation $field) {
00125 $this->arr_fields[$field->get_source_field_name()] = $field;
00126 }
00127
00128
00129
00130
00131
00132
00133 public function get_policy() {
00134 return $this->policy;
00135 }
00136
00137
00138
00139
00140
00141
00142 public function set_policy($policy) {
00143 $this->policy = $policy;
00144 }
00145
00146
00147
00148
00149
00150
00151
00152 public function has_policy($policy) {
00153 return Common::flag_is_set($this->policy, $policy);
00154 }
00155
00156
00157
00158
00159
00160
00161 public function get_type() {
00162 return $this->type;
00163 }
00164 }