table.class.php 10.3 KB
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
<?php
/**
 * [WeEngine System] Copyright (c) 2014 WE7.CC
 * WeEngine is NOT a free software, it under the license terms, visited http://www.we7.cc/ for more details.
 */

defined('IN_IA') or exit('Access Denied');


abstract class We7Table {

	const ONE_TO_ONE = 'ONE_TO_ONE';
	const ONE_TO_MANY = 'ONE_TO_MANY';
	const BELONGS_TO = 'BELONGS_TO';
	const MANY_TO_MANY = 'MANY_TO_MANY';

		protected $tableName = '';
		protected $primaryKey = 'id';
	protected $field = array('group_id');
		protected $rule = array();
		protected $default = array();
		protected $cast = array();
		protected $query;
		private $attribute = array();
	
	private $relationDefine = array();


	public function __construct() {
				load()->classs('validator');
		$this->query = load()->object('Query');
		$this->query->from($this->tableName);
	}

	
	public function searchWithPage($pageindex, $pagesize) {
		if (!empty($pageindex) && !empty($pagesize)) {
			$this->query->page($pageindex, $pagesize);
		}

		return $this;
	}

	
	public function getLastQueryTotal() {
		return $this->query->getLastQueryTotal();
	}

	
	public function count() {
		return $this->query->count();
	}


	
	public function fill($field, $value = '') {
		if (is_array($field)) {
			foreach ($field as $column => $val) {
				$this->fillField($column, $val);
			}
			return $this;
		}
		$this->fillField($field, $value);
		return $this;
	}

	
	private function fillField($column, $val) {
		if (in_array($column, $this->field)) {
			$val = $this->getColumnVal($column, $val);
			$this->attribute[$column] = $val;
			$this->query->fill($column, $val);
		}
	}

	
	private function getColumnVal($column, $val) {
		$method = 'set'.$this->studly($column).'Field';
		if (method_exists($this, $method)) {
			return $this->{$method}($val);
		}
		return $this->cast($column, $val);
	}


	
	private function cast($column, $val) {
		if (isset($this->cast[$column])) {
			switch ($this->cast[$column]) {
				case 'int' : return intval($val); break;
				case 'string' : return strval($val); break;
				case 'float' : return floatval($val); break;
				case 'double' : return doubleval($val); break;
				case 'bool' : return boolval($val); break;
			}
		}
		return $val;
	}


	
	
	private function appendDefault() {
		foreach ($this->default as $field => $value) {
			if (! isset($this->attribute[$field])) {
				if ($value === 'custom') {
					$method = 'default'.$this->studly($field);
					if (! method_exists($this, $method)) {
						trigger_error($method.'方法未找到');
					}
					$value = call_user_func(array($this, $method));
				}
				$this->fillField($field, $value);
			}
		}
	}

	
	protected function valid($data) {
		if (count($this->rule) <= 0) {
			return error(0);
		}
		$validator = Validator::create($data, $this->rule);
		$result = $validator->valid();
		return $result;
	}

	public function get() {
		$data = $this->query->get();
		if (! $data || empty($data)) {
			return $data;
		}
		$this->loadRelation($data);
		return $data;
	}

	public function getall($keyfield = '') {
		$data = $this->query->getall($keyfield);
		if (! $data || empty($data)) {
			return $data;
		}
		$this->loadRelation($data, true);
		return $data;
	}

	
	public function getQuery() {
		return $this->query;
	}

	public function getTableName() {
		return $this->tableName;
	}
	
	public function with($relation) {
		$relations = is_string($relation) ? func_get_args() : $relation;
		foreach ($relations as $relation =>$val) {
			if (is_numeric($relation)) {
				$relation = $val;
			}
			if (!is_callable($val)) {
				$val = null;
			}
			$this->relationDefine[$relation] = $val;
		}

		return $this;
	}
	
	private function loadRelation(array &$data, $muti = false) {
		foreach ($this->relationDefine as $relation => $closure) {
			$this->doload($relation, $data, $muti, $closure); 		}
	}

	
	private function doload($relation, &$data, $muti = false, callable $closure = null) {
		if (method_exists($this, $relation)) {
			$relation_param = call_user_func(array($this, $relation));
			list($type, $table, $foreign_key, $owner_key) = $relation_param;
			if ($type == self::MANY_TO_MANY) {
				$this->doManyToMany($relation, $relation_param, $data, $muti);
				return;
			}
			
			$single = $this->isGetSingle($type);
			
			$foreign_vals = $this->getForeignVal($data, $owner_key, $muti);
			
			$second_table_data = $this->getSecondTableData($table, $foreign_key, $foreign_vals, $single, $closure);
			if (! $muti) {
				$data[$relation] = $second_table_data;
				return;
			}
			if ($single) {
				$second_table_data = array($second_table_data);
			}
			$second_table_data = $this->groupBy($foreign_key, $second_table_data);

			foreach ($data as &$item) {
				$relation_val = isset($second_table_data[$item[$owner_key]]) ? $second_table_data[$item[$owner_key]] : array();
				if ($single) {
					$relation_val = count($relation_val) > 0 ? current($relation_val) : array();
				}
				$item[$relation] =  $relation_val;
			}
		}
	}

	
	private function doManyToMany($relation, $relation_param, &$data, $muti = false) {
		list($type, $table, $foreign_key, $owner_key, $center_table, $center_foreign_key, $center_owner_key)
			= $relation_param;


		$foreign_vals = $this->getForeignVal($data, $owner_key, $muti);
		$three_table = table($table);
		$nativeQuery = $three_table->getQuery();

		$nativeQuery->from($three_table->getTableName(), 'three')
			->innerjoin($center_table, 'center')
			->on(array('center.'.$center_foreign_key => 'three.'.$foreign_key))
			->select('center.*')
			->where('center.'.$center_owner_key, $foreign_vals);

		$three_table_data = $three_table->getall(); 		if (!$muti) {
			$data[$relation] = $three_table_data;
			return;
		}

		$three_table_data = $this->groupBy($center_owner_key, $three_table_data);
		
		foreach ($data as &$item) {
			$three_val = isset($three_table_data[$item[$owner_key]]) ? $three_table_data[$item[$owner_key]] : array();
			$item[$relation] = $three_val;
		}


	}



	
	private function isGetSingle($type) {
		return in_array($type, array(self::ONE_TO_ONE, self::BELONGS_TO)) ? true : false;
	}

	
	private function getForeignVal($data, $owner_key, $muti = false) {
		if (! $muti) {
			return $data[$owner_key];
		}
		return array_map(function($item) use ($owner_key){
			return $item[$owner_key];
		}, $data);
	}

	
	private function getSecondTableData($table, $foreign_key, $foreign_vals, $single = false, $closure = null) {
		$table_instance = table($table)->where($foreign_key, $foreign_vals);
		if ($closure) {
			call_user_func($closure, $table_instance->getQuery()); 		}
		if ($single) {
			return $table_instance->get();
		}
		return $table_instance->getall();
	}



	
	private function groupBy($key, $array) {
		$result = array();

		foreach ($array as $item) {
			$val = $item[$key];
			if (isset($result[$val])) {
				$result[$val][] = $item;
			} else {
				$result[$val] = array($item);
			}
		}
		return $result;
	}

	
	protected function hasOne($table, $foreign_key, $owner_key = false) {
		return $this->relationArray(self::ONE_TO_ONE, $table, $foreign_key, $owner_key);
	}

	
	protected function hasMany($table, $foreign_key, $owner_key = false) {
		return $this->relationArray(self::ONE_TO_MANY, $table, $foreign_key, $owner_key);
	}

	
	protected function belongsTo($table, $foreign_key, $owner_key = false) {
		return $this->relationArray(self::BELONGS_TO, $table, $foreign_key, $owner_key);
	}


	
	protected function belongsMany($table, $foreign_key, $owner_key, $center_table, $center_foreign_key = false,
	                               $center_owner_key = false) {
		if (! $owner_key) {
			$owner_key = $this->primaryKey;
		}
		if (!$center_foreign_key) {
			$center_foreign_key = $foreign_key;
		}
		if (!$center_owner_key) {
			$center_owner_key = $owner_key;
		}
		return array(self::MANY_TO_MANY, $table, $foreign_key, $owner_key, $center_table, $center_foreign_key, $center_owner_key);
	}

	
	private function relationArray($type, $table, $foreign_key, $owner_key) {
		if (! $owner_key) {
			$owner_key = $this->primaryKey;
		}
		if (!in_array($type, array(self::ONE_TO_ONE, self::ONE_TO_MANY, self::BELONGS_TO), true)) {
			trigger_error('不支持的关联类型');
		}
		return array($type, $table, $foreign_key, $owner_key);
	}

	
	public function getById($id) {
		$this->query->from($this->tableName)->where($this->primaryKey, $id);
		if (is_array($id)) {
			return $this->getall();
		}
		return $this->get();
	}

	public function getcolumn($field = '') {
		$data = $this->query->getcolumn($field);
		return $data;
	}

	
	public function where($condition, $parameters = array(), $operator = 'AND') {
		$this->query->where($condition, $parameters, $operator);
		return $this;
	}

	
	public function whereor($condition, $parameters = array()) {
		return $this->where($condition, $parameters, 'OR');
	}
	
	public function save() {
				if($this->query->hasWhere()) {
			$result = $this->valid($this->attribute);
			if (is_error($result)) {
				return $result;
			}
			return $this->query->update();
		}

		$this->appendDefault();
		$result = $this->valid($this->attribute);
		if (is_error($result)) {
			return $result;
		}
		return $this->query->insert();
	}

	
	public function delete() {
		if ($this->query->hasWhere()) {
			return $this->query->delete();
		}
		return false;
	}

	private function doWhere($field, $params, $operator = 'AND') {
		if ($params == 0) {
			return $this;
		}
		$value = $params[0];
		if (count($params) > 1) {
						$field = $field.' '.$params[1];
		}
		$this->query->where($field, $value, $operator);
		return $this;
	}

	
	private function snake($value) {
		$delimiter = '_';
		if (! ctype_lower($value)) {
			$value = preg_replace('/\s+/u', '', ucwords($value));
			$value = strtolower(preg_replace('/(.)(?=[A-Z])/u', '$1'.$delimiter, $value));
		}
		return $value;
	}

	
	private function studly($value) {
		$value = ucwords(str_replace(array('-', '_'), ' ', $value));
		return str_replace(' ', '', $value);
	}

	
	public function __call($method, $params) {
				$actions = array(
			'searchWith',
			'whereor',
			'where',
			'fill'
		);
		foreach ($actions as $action) {
			$fields = explode($action, $method);
			if (count($fields) > 1 && empty($fields[0]) && !empty($fields[1])) {
				$field = $this->snake($fields[1]);
				switch ($action) {
					case 'whereor':
						return $this->doWhere($field, $params, 'OR');
					case 'fill' :
						$this->fill($field, $params[0]);
						return $this;
					default :
						return $this->doWhere($field, $params);
				}
			}
		}
		return $this;
	}
}