123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322 |
- <?php
- namespace think\model;
- use think\Db;
- use think\db\Query;
- use think\Model;
- class Merge extends Model
- {
- protected $relationModel = [];
- protected $fk = '';
- protected $mapFields = [];
-
- public function __construct($data = [])
- {
- parent::__construct($data);
-
- if (empty($this->fk)) {
- $this->fk = strtolower($this->name) . '_id';
- }
- }
-
- public static function get($data = null, $with = [], $cache = false)
- {
- $query = self::parseQuery($data, $with, $cache);
- $query = self::attachQuery($query);
- return $query->find($data);
- }
-
- protected static function attachQuery($query)
- {
- $class = new static();
- $master = $class->name;
- $fields = self::getModelField($query, $master, '', $class->mapFields, $class->field);
- $query->alias($master)->field($fields);
- foreach ($class->relationModel as $key => $model) {
- $name = is_int($key) ? $model : $key;
- $table = is_int($key) ? $query->getTable($name) : $model;
- $query->join($table . ' ' . $name, $name . '.' . $class->fk . '=' . $master . '.' . $class->getPk());
- $fields = self::getModelField($query, $name, $table, $class->mapFields, $class->field);
- $query->field($fields);
- }
- return $query;
- }
-
- protected static function getModelField($query, $name, $table = '', $map = [], $fields = [])
- {
-
- $fields = $fields ?: $query->getTableInfo($table, 'fields');
- $array = [];
- foreach ($fields as $field) {
- if ($key = array_search($name . '.' . $field, $map)) {
-
- $array[] = $name . '.' . $field . ' AS ' . $key;
- } else {
- $array[] = $field;
- }
- }
- return $array;
- }
-
- public static function all($data = null, $with = [], $cache = false)
- {
- $query = self::parseQuery($data, $with, $cache);
- $query = self::attachQuery($query);
- return $query->select($data);
- }
-
- protected function parseData($model, $data)
- {
- $item = [];
- foreach ($data as $key => $val) {
- if ($this->fk != $key && array_key_exists($key, $this->mapFields)) {
- list($name, $key) = explode('.', $this->mapFields[$key]);
- if ($model == $name) {
- $item[$key] = $val;
- }
- } else {
- $item[$key] = $val;
- }
- }
- return $item;
- }
-
- public function save($data = [], $where = [], $sequence = null)
- {
- if (!empty($data)) {
-
- if (!$this->validateData($data)) {
- return false;
- }
-
- foreach ($data as $key => $value) {
- $this->setAttr($key, $value, $data);
- }
- if (!empty($where)) {
- $this->isUpdate = true;
- }
- }
-
- $this->autoCompleteData($this->auto);
-
- if ($this->autoWriteTimestamp && $this->updateTime && !isset($this->data[$this->updateTime])) {
- $this->setAttr($this->updateTime, null);
- }
-
- if (false === $this->trigger('before_write', $this)) {
- return false;
- }
- $db = $this->db();
- $db->startTrans();
- $pk = $this->getPk();
- try {
- if ($this->isUpdate) {
-
- $this->autoCompleteData($this->update);
- if (false === $this->trigger('before_update', $this)) {
- return false;
- }
- if (empty($where) && !empty($this->updateWhere)) {
- $where = $this->updateWhere;
- }
-
- $data = $this->getChangedData();
-
- foreach ($this->data as $key => $val) {
- if ($this->isPk($key)) {
- $data[$key] = $val;
- }
- }
-
- $data = $this->parseData($this->name, $data);
- if (is_string($pk) && isset($data[$pk])) {
- if (!isset($where[$pk])) {
- unset($where);
- $where[$pk] = $data[$pk];
- }
- unset($data[$pk]);
- }
-
- $result = $db->strict(false)->where($where)->update($data);
-
- foreach ($this->relationModel as $key => $model) {
- $name = is_int($key) ? $model : $key;
- $table = is_int($key) ? $db->getTable($model) : $model;
-
- $data = $this->parseData($name, $data);
- if (Db::table($table)->strict(false)->where($this->fk, $this->data[$this->getPk()])->update($data)) {
- $result = 1;
- }
- }
-
- $this->trigger('after_update', $this);
- } else {
-
- $this->autoCompleteData($this->insert);
-
- if ($this->autoWriteTimestamp && $this->createTime && !isset($this->data[$this->createTime])) {
- $this->setAttr($this->createTime, null);
- }
- if (false === $this->trigger('before_insert', $this)) {
- return false;
- }
-
- $data = $this->parseData($this->name, $this->data);
-
- $result = $db->name($this->name)->strict(false)->insert($data);
- if ($result) {
- $insertId = $db->getLastInsID($sequence);
-
- if ($insertId) {
- if (is_string($pk)) {
- $this->data[$pk] = $insertId;
- }
- $this->data[$this->fk] = $insertId;
- }
-
- $source = $this->data;
- if ($insertId && is_string($pk) && isset($source[$pk]) && $this->fk != $pk) {
- unset($source[$pk]);
- }
- foreach ($this->relationModel as $key => $model) {
- $name = is_int($key) ? $model : $key;
- $table = is_int($key) ? $db->getTable($model) : $model;
-
- $data = $this->parseData($name, $source);
- Db::table($table)->strict(false)->insert($data);
- }
- }
-
- $this->isUpdate = true;
-
- $this->trigger('after_insert', $this);
- }
- $db->commit();
-
- $this->trigger('after_write', $this);
- $this->origin = $this->data;
- return $result;
- } catch (\Exception $e) {
- $db->rollback();
- throw $e;
- }
- }
-
- public function delete()
- {
- if (false === $this->trigger('before_delete', $this)) {
- return false;
- }
- $db = $this->db();
- $db->startTrans();
- try {
- $result = $db->delete($this->data);
- if ($result) {
-
- $pk = $this->data[$this->getPk()];
-
- foreach ($this->relationModel as $key => $model) {
- $table = is_int($key) ? $db->getTable($model) : $model;
- $query = new Query;
- $query->table($table)->where($this->fk, $pk)->delete();
- }
- }
- $this->trigger('after_delete', $this);
- $db->commit();
- return $result;
- } catch (\Exception $e) {
- $db->rollback();
- throw $e;
- }
- }
- }
|