diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php new file mode 100644 index 0000000..786ab78 --- /dev/null +++ b/src/ActiveRecord.php @@ -0,0 +1,368 @@ +findTableColumns(); + } + + /** + * getIsNewRecord. + * @return bool + */ + public function getIsNewRecord() + { + return $this->_isNewRecord; + } + + /** + * getColumns. + * @return array + */ + public function getColumns() + { + return $this->_columns; + } + + /** + * getOldAttributes. + * @param null $column + * @return null|array|mixed + */ + public function getOldAttributes($column = null) + { + return $column ? ($this->_oldAttributes[$column] ?? null) : $this->_oldAttributes; + } + + /** + * tableName. + */ + abstract public static function tableName(): string; + + /** + * getDb. + * @return BaseModel + */ + public static function getDb() + { + return new BaseModel(); + } + + /** + * getTableSchema. + * @return array + */ + public static function getTableSchema() + { + static $_columns; + if (empty($_columns)) { + $_columns = static::getDb()->query('SHOW FULL COLUMNS FROM ' . static::tableName())->fetchAll(); + } + return $_columns; + } + + /** + * 主键必须设置 + * getPrimaryKey. + * @return array + */ + public static function getPrimaryKey() + { + if (empty(self::$_primaryKey)) { + foreach (static::getTableSchema() as $column) { + if ($column['Key'] == 'PRI') { + self::$_primaryKey[] = $column['Field']; + } + } + } + return self::$_primaryKey; + } + + /** + * toArray. + * @return array + */ + public function toArray() + { + $data = []; + + foreach ($this->getColumns() as $field => $column) { + $data[$field] = $this->{$field}; + } + + return $data; + } + + /** + * getChangeAttributes. + * @return array + */ + public function getChangeAttributes() + { + $changeAttributes = []; + foreach ($this->getOldAttributes() as $column => $oldValue) { + if ($this->{$column} !== $oldValue) { + $changeAttributes[$column] = $this->{$column}; + } + } + return $changeAttributes; + } + + /** + * save. + * @return bool + */ + public function save() + { + return $this->getIsNewRecord() ? $this->insert() : $this->update(); + } + + /** + * update. + * @return bool + */ + public function update() + { + $changeAttributes = $this->getChangeAttributes(); + if (! empty($changeAttributes)) { + return static::getDb()->update(static::tableName(), $changeAttributes, $this->getPrimaryKeyCondition())->rowCount(); + } + return true; + } + + /** + * insert. + * @return bool + */ + public function insert() + { + $insertId = static::getDb()->insert(static::tableName(), $this->toArray()); + + $primaryKey = self::getPrimaryKey()[0] ?? null; + + if ($primaryKey && isset($this->{$primaryKey})) { + $this->{$primaryKey} = $insertId; + } + + return $insertId ? true : false; + } + + /** + * delete. + * @return bool + */ + public function delete() + { + return static::getDb()->delete(static::tableName(), $this->getPrimaryKeyCondition())->rowCount() ? true : false; + } + + /** + * findOrCreate. + * @param array|string $where + * @return ActiveRecord|static + */ + public static function findOrCreate($where) + { + $where = self::formatWhere($where); + + $data = null; + + if (! empty($where)) { + $data = static::getDb()->get(static::tableName(), '*', self::formatWhere($where)); + } + + $model = (new static()); + if (! empty($data)) { + return $model->createModelsByDataBase($data); + } + + return $model; + } + + /** + * findOne. + * @param array|string $where + * @return static + */ + public static function findOne($where) + { + $where = self::formatWhere($where); + + if (empty($where)) { + return null; + } + + $data = static::getDb()->get(static::tableName(), '*', $where); + return $data ? (new static())->createModelsByDataBase($data) : null; + } + + /** + * findAll. + * @param array $where + * @return array|static[] + */ + public static function findAll($where = []) + { + $dataList = static::getDb()->select(static::tableName(), '*', $where); + + if (empty($dataList)) { + return []; + } + + $models = []; + + foreach ($dataList as $data) { + $models[] = (new static())->createModelsByDataBase($data); + } + + return $models; + } + + /** + * insertOne. + * @return bool + */ + public static function insertOne(array $data) + { + return static::getDb()->insert(static::tableName(), [$data])->rowCount(); + } + + /** + * insertMulti. + * @return bool + */ + public static function insertMulti(array $data) + { + return static::getDb()->insert(static::tableName(), $data)->rowCount(); + } + + /** + * updateAll. + * @param array $where + * @return bool + */ + public static function updateAll(array $data, $where = []) + { + return static::getDb()->update(static::tableName(), $data, $where)->rowCount(); + } + + /** + * deleteAll. + * @param null $where + * @return bool + */ + public static function deleteAll($where = null) + { + return static::getDb()->delete(static::tableName(), $where)->rowCount() ? true : false; + } + + /** + * load. + */ + public function load(array $data) + { + foreach ($data as $key => $value) { + if (! isset($this->{$key})) { + continue; + } + + $this->{$key} = $value; + } + } + + /** + * findTableColumns. + */ + protected function findTableColumns() + { + foreach (static::getTableSchema() as $column) { + $field = $column['Field']; + + $this->{$field} = $column['Default']; + + $this->_columns[$field] = $column; + } + } + + /** + * createModelsByDataBase. + * @return $this + */ + protected function createModelsByDataBase(array $rows) + { + foreach ($rows as $column => $value) { + $this->{$column} = $value; + $this->_oldAttributes[$column] = $value; + } + + $this->_isNewRecord = false; + + return $this; + } + + /** + * getPrimaryKeyCondition. + * @return array + */ + protected function getPrimaryKeyCondition() + { + $condition = []; + + foreach (self::getPrimaryKey() as $primaryKey) { + $condition[$primaryKey] = $this->getOldAttributes($primaryKey); + } + + return $condition; + } + + /** + * formatWhere. + * @param array|string $where + * @return array + */ + protected static function formatWhere($where) + { + if (is_string($where)) { + return [self::getPrimaryKey()[0] => $where]; + } + return $where; + } +} diff --git a/src/BaseModel.php b/src/BaseModel.php index d5af892..1f79c52 100644 --- a/src/BaseModel.php +++ b/src/BaseModel.php @@ -831,8 +831,8 @@ protected function dataImplode($data, &$map, $conjunctor) $type = gettype($value); if ( - $type === 'array' && - preg_match('/^(AND|OR)(\\s+#.*)?$/', $key, $relation_match) + $type === 'array' + && preg_match('/^(AND|OR)(\\s+#.*)?$/', $key, $relation_match) ) { $relationship = $relation_match[1]; @@ -846,8 +846,8 @@ protected function dataImplode($data, &$map, $conjunctor) $map_key = $this->mapKey(); if ( - is_int($key) && - preg_match('/([a-zA-Z0-9_\.]+)\[(?\>\=?|\<\=?|\!?\=)\]([a-zA-Z0-9_\.]+)/i', $value, $match) + is_int($key) + && preg_match('/([a-zA-Z0-9_\.]+)\[(?\>\=?|\<\=?|\!?\=)\]([a-zA-Z0-9_\.]+)/i', $value, $match) ) { $stack[] = $this->columnQuote($match[1]) . ' ' . $match['operator'] . ' ' . $this->columnQuote( $match[3] @@ -1066,9 +1066,9 @@ protected function whereClause($where, &$map) if (is_numeric($LIMIT)) { $where_clause .= ' LIMIT ' . $LIMIT; } elseif ( - is_array($LIMIT) && - is_numeric($LIMIT[0]) && - is_numeric($LIMIT[1]) + is_array($LIMIT) + && is_numeric($LIMIT[0]) + && is_numeric($LIMIT[1]) ) { $where_clause .= ' LIMIT ' . $LIMIT[1] . ' OFFSET ' . $LIMIT[0]; } @@ -1098,16 +1098,16 @@ protected function selectContext($table, &$map, $join, &$columns = null, $where $join_key = is_array($join) ? array_keys($join) : null; if ( - isset($join_key[0]) && - strpos((string) $join_key[0], '[') === 0 + isset($join_key[0]) + && strpos((string) $join_key[0], '[') === 0 ) { $is_join = true; $table_query .= ' ' . $this->buildJoin($table, $join); } else { if (is_null($columns)) { if ( - ! is_null($where) || - (is_array($join) && isset($column_fn)) + ! is_null($where) + || (is_array($join) && isset($column_fn)) ) { $where = $join; $columns = null;