From 1e7181c2821cab11b58c9edfe2a7d2cb19f79b56 Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 13:15:51 +0800 Subject: [PATCH 1/3] simple orm --- src/ActiveRecord.php | 367 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 367 insertions(+) create mode 100644 src/ActiveRecord.php diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php new file mode 100644 index 0000000..0bf10cd --- /dev/null +++ b/src/ActiveRecord.php @@ -0,0 +1,367 @@ +findTableColumns(); + } + + /** + * getIsNewRecord + * @return bool + */ + public function getIsNewRecord() + { + return $this->_isNewRecord; + } + + /** + * getColumns + * @return array + */ + public function getColumns() + { + return $this->_columns; + } + + /** + * getOldAttributes + * @param null $column + * @return array|mixed|null + */ + public function getOldAttributes($column=null) + { + return $column?($this->_oldAttributes[$column]??null):$this->_oldAttributes; + } + + /** + * findTableColumns + */ + protected function findTableColumns() + { + foreach (static::getTableSchema() as $column) { + + $field = $column['Field']; + + $this->{$field} = $column['Default']; + + $this->_columns[$field] = $column; + } + } + + /** + * tableName + * @return string + */ + 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; + } + + /** + * 主键 + * @var null + */ + private static $_primaryKey = null; + + /** + * 主键必须设置 + * 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; + } + + /** + * createModelsByDataBase + * @param array $rows + * @return $this + */ + protected function createModelsByDataBase(array $rows) + { + + foreach ($rows as $column => $value) { + $this->{$column} = $value; + $this->_oldAttributes[$column] = $value; + } + + $this->_isNewRecord = false; + + return $this; + } + + /** + * getChangeAttributes + * @return array + */ + public function getChangeAttributes() + { + $changeAttributes = []; + foreach ($this->getOldAttributes() as $column => $oldValue) { + if ($this->{$column} !== $oldValue) { + $changeAttributes[$column] = $this->{$column}; + } + } + return $changeAttributes; + } + + /** + * getPrimaryKeyCondition + * @return array + */ + protected function getPrimaryKeyCondition() + { + $condition = []; + + foreach (self::getPrimaryKey() as $primaryKey) { + $condition[$primaryKey] = $this->getOldAttributes($primaryKey); + } + + return $condition; + } + + /** + * 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; + } + + /** + * formatWhere + * @param array|string $where + * @return array + */ + protected static function formatWhere($where) + { + if(is_string($where)){ + return [self::getPrimaryKey()[0]=>$where]; + } + return $where; + } + + /** + * 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 static[]|array + */ + 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 + * @param array $data + * @return bool + */ + public static function insertOne(array $data) + { + return static::getDb()->insert(static::tableName(), [$data])->rowCount(); + } + + /** + * insertMulti + * @param array $data + * @return bool + */ + public static function insertMulti(array $data) + { + return static::getDb()->insert(static::tableName(), $data)->rowCount(); + } + + /** + * updateAll + * @param array $data + * @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 + * @param array $data + */ + public function load(array $data) + { + foreach ($data as $key => $value) { + + if (!isset($this->{$key})) + continue; + + $this->{$key} = $value; + } + } + +} From 668a25eeef38e278e3045e9e613444df85adbe7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B0=8F=E5=8F=AE=E5=BD=93=E7=9A=84=E8=82=9A=E5=85=9C?= <254364502@qq.com> Date: Wed, 16 Jun 2021 13:18:05 +0800 Subject: [PATCH 2/3] Update ActiveRecord.php --- src/ActiveRecord.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 0bf10cd..3c57d74 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -6,7 +6,7 @@ /** * Class ActiveRecord - * @package App\Model + * @package Simps\DB */ abstract class ActiveRecord { From 8afed9ba62a4a6332acd944492b8950da703dd56 Mon Sep 17 00:00:00 2001 From: lyl <254364502@qq.com> Date: Wed, 16 Jun 2021 17:59:34 +0800 Subject: [PATCH 3/3] composer cs-fix --- src/ActiveRecord.php | 235 ++++++++++++++++++++++--------------------- src/BaseModel.php | 22 ++-- 2 files changed, 129 insertions(+), 128 deletions(-) diff --git a/src/ActiveRecord.php b/src/ActiveRecord.php index 3c57d74..786ab78 100644 --- a/src/ActiveRecord.php +++ b/src/ActiveRecord.php @@ -1,12 +1,17 @@ _oldAttributes[$column]??null):$this->_oldAttributes; + return $column ? ($this->_oldAttributes[$column] ?? null) : $this->_oldAttributes; } /** - * findTableColumns - */ - protected function findTableColumns() - { - foreach (static::getTableSchema() as $column) { - - $field = $column['Field']; - - $this->{$field} = $column['Default']; - - $this->_columns[$field] = $column; - } - } - - /** - * tableName - * @return string + * tableName. */ abstract public static function tableName(): string; /** - * getDb + * getDb. * @return BaseModel */ public static function getDb() @@ -92,42 +87,37 @@ public static function getDb() } /** - * getTableSchema + * getTableSchema. * @return array */ public static function getTableSchema() { static $_columns; - if(empty($_columns)){ + if (empty($_columns)) { $_columns = static::getDb()->query('SHOW FULL COLUMNS FROM ' . static::tableName())->fetchAll(); } return $_columns; } - /** - * 主键 - * @var null - */ - private static $_primaryKey = null; - /** * 主键必须设置 - * getPrimaryKey + * getPrimaryKey. * @return array */ public static function getPrimaryKey() { - if(empty(self::$_primaryKey)){ + if (empty(self::$_primaryKey)) { foreach (static::getTableSchema() as $column) { - if ($column['Key'] == 'PRI') + if ($column['Key'] == 'PRI') { self::$_primaryKey[] = $column['Field']; + } } } return self::$_primaryKey; } /** - * toArray + * toArray. * @return array */ public function toArray() @@ -142,25 +132,7 @@ public function toArray() } /** - * createModelsByDataBase - * @param array $rows - * @return $this - */ - protected function createModelsByDataBase(array $rows) - { - - foreach ($rows as $column => $value) { - $this->{$column} = $value; - $this->_oldAttributes[$column] = $value; - } - - $this->_isNewRecord = false; - - return $this; - } - - /** - * getChangeAttributes + * getChangeAttributes. * @return array */ public function getChangeAttributes() @@ -175,22 +147,7 @@ public function getChangeAttributes() } /** - * getPrimaryKeyCondition - * @return array - */ - protected function getPrimaryKeyCondition() - { - $condition = []; - - foreach (self::getPrimaryKey() as $primaryKey) { - $condition[$primaryKey] = $this->getOldAttributes($primaryKey); - } - - return $condition; - } - - /** - * save + * save. * @return bool */ public function save() @@ -199,58 +156,46 @@ public function save() } /** - * update + * update. * @return bool */ public function update() { $changeAttributes = $this->getChangeAttributes(); - if(!empty($changeAttributes)){ + if (! empty($changeAttributes)) { return static::getDb()->update(static::tableName(), $changeAttributes, $this->getPrimaryKeyCondition())->rowCount(); } return true; } /** - * insert + * insert. * @return bool */ public function insert() { $insertId = static::getDb()->insert(static::tableName(), $this->toArray()); - $primaryKey = self::getPrimaryKey()[0]??null; + $primaryKey = self::getPrimaryKey()[0] ?? null; - if($primaryKey && isset($this->{$primaryKey})) + if ($primaryKey && isset($this->{$primaryKey})) { $this->{$primaryKey} = $insertId; + } - return $insertId?true:false; + return $insertId ? true : false; } /** - * delete + * delete. * @return bool */ public function delete() { - return static::getDb()->delete(static::tableName(), $this->getPrimaryKeyCondition())->rowCount()?true:false; - } - - /** - * formatWhere - * @param array|string $where - * @return array - */ - protected static function formatWhere($where) - { - if(is_string($where)){ - return [self::getPrimaryKey()[0]=>$where]; - } - return $where; + return static::getDb()->delete(static::tableName(), $this->getPrimaryKeyCondition())->rowCount() ? true : false; } /** - * findOrCreate + * findOrCreate. * @param array|string $where * @return ActiveRecord|static */ @@ -260,11 +205,12 @@ public static function findOrCreate($where) $data = null; - if(!empty($where)) - $data = static::getDb()->get(static::tableName(), '*', self::formatWhere($where)); + if (! empty($where)) { + $data = static::getDb()->get(static::tableName(), '*', self::formatWhere($where)); + } $model = (new static()); - if (!empty($data)) { + if (! empty($data)) { return $model->createModelsByDataBase($data); } @@ -272,7 +218,7 @@ public static function findOrCreate($where) } /** - * findOne + * findOne. * @param array|string $where * @return static */ @@ -280,24 +226,26 @@ public static function findOne($where) { $where = self::formatWhere($where); - if(empty($where)) + if (empty($where)) { return null; + } $data = static::getDb()->get(static::tableName(), '*', $where); return $data ? (new static())->createModelsByDataBase($data) : null; } /** - * findAll + * findAll. * @param array $where - * @return static[]|array + * @return array|static[] */ public static function findAll($where = []) { $dataList = static::getDb()->select(static::tableName(), '*', $where); - if (empty($dataList)) + if (empty($dataList)) { return []; + } $models = []; @@ -309,8 +257,7 @@ public static function findAll($where = []) } /** - * insertOne - * @param array $data + * insertOne. * @return bool */ public static function insertOne(array $data) @@ -319,8 +266,7 @@ public static function insertOne(array $data) } /** - * insertMulti - * @param array $data + * insertMulti. * @return bool */ public static function insertMulti(array $data) @@ -329,8 +275,7 @@ public static function insertMulti(array $data) } /** - * updateAll - * @param array $data + * updateAll. * @param array $where * @return bool */ @@ -340,28 +285,84 @@ public static function updateAll(array $data, $where = []) } /** - * deleteAll + * deleteAll. * @param null $where * @return bool */ public static function deleteAll($where = null) { - return static::getDb()->delete(static::tableName(), $where)->rowCount()?true:false; + return static::getDb()->delete(static::tableName(), $where)->rowCount() ? true : false; } /** - * load - * @param array $data + * load. */ public function load(array $data) { foreach ($data as $key => $value) { - - if (!isset($this->{$key})) + 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;