{"id":1475,"date":"2021-10-11T21:42:48","date_gmt":"2021-10-11T13:42:48","guid":{"rendered":"https:\/\/www.siediyer.cn\/?p=1475"},"modified":"2021-10-13T02:17:49","modified_gmt":"2021-10-12T18:17:49","slug":"%e4%b8%80%e4%b8%aa%e7%ae%80%e6%98%93%e7%9a%84orm%e7%b1%bb","status":"publish","type":"post","link":"https:\/\/www.siediyer.cn\/?p=1475","title":{"rendered":"\u4e00\u4e2a\u7b80\u6613\u7684ORM\u7c7b"},"content":{"rendered":"<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">&lt;?php\r\n\/**\r\n * Created by PhpStorm.\r\n * User: siediyer\r\n * Date: 2017-10-28\r\n * Time: 18:22\r\n *\/\r\nclass MyOrm\r\n{\r\n    public $host = '127.0.0.1';  \/\/\u6570\u636e\u5e93\u5730\u5740\r\n    public $dbname = 'test';   \/\/\u6570\u636e\u5e93\u540d\r\n    public $user = 'root';  \/\/\u6570\u636e\u5e93\u7528\u6237\u540d\r\n    public $pwd = 'root';   \/\/\u6570\u636e\u5e93\u5bc6\u7801\r\n    public $port = '3306';  \/\/\u6570\u636e\u5e93\u7aef\u53e3\r\n    public $charset = 'utf8';   \/\/\u6570\u636e\u5e93\u7f16\u7801\r\n    private $conn = null;    \/\/\u6570\u636e\u5e93\u94fe\u63a5\u8d44\u6e90\r\n    private $alias = [];  \/\/\u8bb0\u5f55\u5168\u5c40\u7684\u8bed\u53e5\u53c2\u6570\r\n    private $sql;    \/\/\u5b58\u50a8\u6700\u540e\u4e00\u6761sql\r\n    public function __construct()\r\n    {\r\n        if( is_null( $this-&gt;conn ) ){\r\n            $dsn = \"mysql:host=$this-&gt;host;dbname=$this-&gt;dbname;charset=$this-&gt;charset;port=$this-&gt;port\";\r\n            $this-&gt;conn = new PDO( $dsn, $this-&gt;user, $this-&gt;pwd );\r\n        }\r\n    }\r\n    \/\/field\u8bed\u53e5\r\n    public function field( $field )\r\n    {\r\n        if( !is_string( $field ) ){\r\n            throw new exception(\"field\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u5b57\u7b26\u4e32\");\r\n        }\r\n        $this-&gt;alias['field'] = $field;\r\n        return $this;\r\n    }\r\n    \/\/table\u8bed\u53e5\r\n    public function table( $table )\r\n    {\r\n        if( !is_string( $table ) ){\r\n            throw new exception(\"table\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u5b57\u7b26\u4e32\");\r\n        }\r\n        $this-&gt;alias['table'] = $table;\r\n        return $this;\r\n    }\r\n    \/\/where\u8bed\u53e5\r\n    public function where( $where )\r\n    {\r\n        $this-&gt;alias['where'] = '';\r\n        if( is_array( $where ) ){\r\n            foreach( $where as $key=&gt;$vo ){\r\n                $this-&gt;alias['where'] .= \" `$key`\" . ' = ' . $vo . ' and ';\r\n            }\r\n            $this-&gt;alias['where'] = rtrim( $this-&gt;alias['where'], 'and ' );\r\n        }else if( is_string( $where ) ){\r\n            $this-&gt;alias['where'] = $where;\r\n        }else{\r\n            throw new exception(\"where\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u6570\u7ec4\u6216\u5b57\u7b26\u4e32\");\r\n        }\r\n        return $this;\r\n    }\r\n    \/\/limit\u8bed\u53e5\r\n    public function limit( $limit )\r\n    {\r\n        $this-&gt;alias['limit'] = '';\r\n        if( is_numeric( $limit ) ){\r\n           $this-&gt;alias['limit'] = '0,' . $limit;\r\n        }else if( is_string( $limit ) ){\r\n            $this-&gt;alias['limit'] = $limit;\r\n        }else{\r\n            throw new exception(\"limit\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u6570\u5b57\u6216\u5b57\u7b26\u4e32\");\r\n        }\r\n        return $this;\r\n    }\r\n    \/\/order\u8bed\u53e5\r\n    public function order( $order )\r\n    {\r\n        if( !is_string( $order ) ){\r\n            throw new exception(\"order\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u5b57\u7b26\u4e32\");\r\n        }\r\n        $this-&gt;alias['order'] = $order;\r\n        return $this;\r\n    }\r\n    \/\/group\u8bed\u53e5\r\n    public function group( $group )\r\n    {\r\n        if( !is_string( $group ) ){\r\n            throw new exception(\"group\u8bed\u53e5\u7684\u53c2\u6570\u5fc5\u987b\u4e3a\u5b57\u7b26\u4e32\");\r\n        }\r\n        $this-&gt;alias['group'] = $group;\r\n        return $this;\r\n    }\r\n    \/\/\u89e3\u6790\u67e5\u8be2sql\u8bed\u53e5\r\n    public function ParseSelectSql()\r\n    {\r\n        $this-&gt;sql = 'select *';\r\n        if( !empty( $this-&gt;alias['field'] ) ){\r\n            $this-&gt;sql = str_replace( '*', $this-&gt;alias['field'], $this-&gt;sql );\r\n        }\r\n        if( empty( $this-&gt;alias['table'] ) ){\r\n            throw new exception(\"\u8bf7\u7528table\u5b50\u53e5\u8bbe\u7f6e\u67e5\u8be2\u8868\");\r\n        }else{\r\n            $this-&gt;sql .= ' from ' . $this-&gt;alias['table'];\r\n        }\r\n        if( !empty( $this-&gt;alias['where'] ) ){\r\n            $this-&gt;sql .= ' where ' . $this-&gt;alias['where'];\r\n        }\r\n        if( !empty( $this-&gt;alias['group'] ) ){\r\n            $this-&gt;sql .= ' group by ' . $this-&gt;alias['group'];\r\n        }\r\n        if( !empty( $this-&gt;alias['order'] ) ){\r\n            $this-&gt;sql .= ' order by ' . $this-&gt;alias['order'];\r\n        }\r\n        if( !empty( $this-&gt;alias['limit'] ) ){\r\n            $this-&gt;sql .= ' limit ' . $this-&gt;alias['limit'];\r\n        }\r\n    }\r\n    \/\/\u89e3\u6790\u6dfb\u52a0sql\u8bed\u53e5\r\n    public function ParseAddSql()\r\n    {\r\n        $this-&gt;sql = 'insert into ';\r\n        if( empty( $this-&gt;alias['table'] ) ){\r\n            throw new exception(\"\u8bf7\u7528table\u5b50\u53e5\u8bbe\u7f6e\u6dfb\u52a0\u8868\");\r\n        }else{\r\n            $this-&gt;sql .= $this-&gt;alias['table'] . ' set ';\r\n        }\r\n        return $this-&gt;sql;\r\n    }\r\n    \/\/\u89e3\u6790\u66f4\u65b0sql\u8bed\u53e5\r\n    public function ParseUpdateSql()\r\n    {\r\n        $this-&gt;sql = 'update ';\r\n        if( empty( $this-&gt;alias['table'] ) ){\r\n            throw new exception(\"\u8bf7\u7528table\u5b50\u53e5\u8bbe\u7f6e\u4fee\u6539\u8868\");\r\n        }else{\r\n            $this-&gt;sql .= $this-&gt;alias['table'] . ' set ';\r\n        }\r\n        if( empty( $this-&gt;alias['where'] ) ){\r\n            throw new exception(\"\u66f4\u65b0\u8bed\u53e5\u5fc5\u987b\u6709where\u5b50\u53e5\u6307\u5b9a\u6761\u4ef6\");\r\n        }\r\n        return $this-&gt;sql;\r\n    }\r\n    \/\/\u89e3\u6790\u5220\u9664sql\u8bed\u53e5\r\n    public function ParseDeleteSql()\r\n    {\r\n        $this-&gt;sql = 'delete from ';\r\n        if( empty( $this-&gt;alias['table'] ) ){\r\n            throw new exception(\"\u8bf7\u7528table\u5b50\u53e5\u8bbe\u7f6e\u5220\u9664\u8868\");\r\n        }else{\r\n            $this-&gt;sql .= $this-&gt;alias['table'];\r\n        }\r\n        if( empty( $this-&gt;alias['where'] ) ){\r\n            throw new exception(\"\u5220\u9664\u8bed\u53e5\u5fc5\u987b\u6709where\u5b50\u53e5\u6307\u5b9a\u6761\u4ef6\");\r\n        }\r\n        $this-&gt;sql .= ' where ' . $this-&gt;alias['where'];\r\n        return $this-&gt;sql;\r\n    }\r\n    \/\/\u67e5\u8be2\u8bed\u53e5\r\n    public function select()\r\n    {\r\n        $this-&gt;ParseSelectSql();\r\n        $row = $this-&gt;conn-&gt;query( $this-&gt;sql )-&gt;fetchAll( PDO::FETCH_ASSOC );\r\n        $result = [];\r\n        foreach( $row as $key=&gt;$vo ){\r\n            $arrObj = clone $this;  \/\/clone\u5f53\u524d\u5bf9\u8c61\u9632\u6b62\u5bf9this\u5bf9\u8c61\u9020\u6210\u6c61\u67d3\r\n            $arrObj-&gt;data = $vo;\r\n            $result[$key] = $arrObj;\r\n            unset( $arrObj );\r\n        }\r\n        return $result;\r\n    }\r\n    \/\/\u67e5\u8be2\u4e00\u6761\r\n    public function find()\r\n    {\r\n        $this-&gt;ParseSelectSql();\r\n        $row = $this-&gt;conn-&gt;query( $this-&gt;sql )-&gt;fetch( PDO::FETCH_ASSOC );\r\n        $arrObj = clone $this;  \/\/clone\u5f53\u524d\u5bf9\u8c61\u9632\u6b62\u5bf9this\u5bf9\u8c61\u9020\u6210\u6c61\u67d3\r\n        $arrObj-&gt;data = $row;\r\n        $result = $arrObj;\r\n        unset( $arrObj );\r\n        return $result;\r\n    }\r\n    \/\/\u6dfb\u52a0\u6570\u636e\r\n    public function add( $data )\r\n    {\r\n        if( !is_array( $data ) ){\r\n            throw new exception(\"\u6dfb\u52a0\u6570\u636eadd\u65b9\u6cd5\u53c2\u6570\u5fc5\u987b\u4e3a\u6570\u7ec4\");\r\n        }\r\n        $this-&gt;ParseAddSql();\r\n        foreach( $data as $key=&gt;$vo ){\r\n            $this-&gt;sql .= \" `{$key}` = '\" . $vo . \"',\";\r\n        }\r\n        $this-&gt;conn-&gt;exec( rtrim( $this-&gt;sql, ',' ) );\r\n        return $this-&gt;conn-&gt;lastInsertId();\r\n    }\r\n    \/\/\u66f4\u65b0\u8bed\u53e5\r\n    public function update( $data )\r\n    {\r\n        if( !is_array( $data ) ){\r\n            throw new exception(\"\u66f4\u65b0\u6570\u636eupdate\u65b9\u6cd5\u53c2\u6570\u5fc5\u987b\u4e3a\u6570\u7ec4\");\r\n        }\r\n        $this-&gt;ParseUpdateSql();\r\n        foreach( $data as $key=&gt;$vo ){\r\n            $this-&gt;sql .= \" `{$key}` = '\" . $vo . \"',\";\r\n        }\r\n        $this-&gt;sql = rtrim( $this-&gt;sql, ',' ) . ' where ' . $this-&gt;alias['where'];\r\n        return $this-&gt;conn-&gt;exec( $this-&gt;sql );\r\n    }\r\n    \/\/\u5220\u9664\u8bed\u53e5\r\n    public function delete()\r\n    {\r\n        $this-&gt;ParseDeleteSql();\r\n        return $this-&gt;conn-&gt;exec( $this-&gt;sql );\r\n    }\r\n    \/\/\u83b7\u53d6\u67e5\u8be2\u6570\u636e\r\n    public function getData()\r\n    {\r\n        return $this-&gt;data;\r\n    }\r\n    \/\/\u83b7\u53d6\u6700\u540e\u4e00\u6b21\u6267\u884c\u7684sql\u8bed\u53e5\r\n    public function getLastSql()\r\n    {\r\n        return $this-&gt;sql;\r\n    }\r\n    public function __get($name)\r\n    {\r\n        return $this-&gt;getData()[$name];\r\n    }\r\n    public function offsetExists($offset)\r\n    {\r\n        if( !isset( $this-&gt;getData()[$offset] ) ){\r\n            return NULL;\r\n        }\r\n    }\r\n    public function offsetGet($offset)\r\n    {\r\n        return $this-&gt;getData()[$offset];\r\n    }\r\n    public function offsetSet($offset, $value)\r\n    {\r\n        return $this-&gt;data[$offset] = $value;\r\n    }\r\n    public function offsetUnset($offset)\r\n    {\r\n        unset( $this-&gt;data[$offset] );\r\n    }\r\n}<\/pre>\n<p>\u4f60\u53ef\u4ee5\u8fd9\u4e48\u7528\uff1a<\/p>\n<pre class=\"EnlighterJSRAW\" data-enlighter-language=\"generic\">$orm = new MyOrm();\r\n\/\/\u67e5\u8be2\u8bed\u53e5\r\n$res = $orm-&gt;table('user')-&gt;order('id desc')-&gt;select();\r\n$res = $orm-&gt;table('user')-&gt;where(\"name='test'\")-&gt;order('id desc')-&gt;select();\r\n$res = $orm-&gt;table('user')-&gt;where(['id' =&gt; 1])-&gt;order('id desc')-&gt;find();\r\n$res = $orm-&gt;table('user')-&gt;where(\"age &gt; 20\")-&gt;group('group by name')-&gt;order('id desc')-&gt;limit(2)-&gt;select();\r\n$res = $orm-&gt;table('user')-&gt;where(\"age &gt; 20\")-&gt;group('group by name')-&gt;order('id desc')-&gt;limit('2,2')-&gt;select();\r\n\/\/\u4f60\u53ef\u4ee5\u8fd9\u6837\u5904\u7406\u6570\u636e\r\nforeach( $res as $key=&gt;$vo ){\r\n    echo $vo-&gt;name . '&lt;br\/&gt;';\r\n}\r\n\/\/\u4e5f\u53ef\u4ee5\u8fd9\u6837\u5904\u7406\r\nforeach( $res as $key=&gt;$vo ){\r\n    echo $vo['name'] . '&lt;br\/&gt;';\r\n}\r\n\/\/\u8fd8\u53ef\u4ee5\u8fd9\u6837\r\nforeach( $res as $key=&gt;$vo ){\r\n    print_r( $vo-&gt;getData() ) . '&lt;br\/&gt;';\r\n}\r\n\/\/\u6dfb\u52a0\u6570\u636e\r\n$data = [\r\n    'name' =&gt; 'test1',\r\n    'age' =&gt; 20,\r\n    'password' =&gt; '21232f297a57a5a743894a0e4a801fc3',\r\n    'salt' =&gt; 'domain'\r\n];\r\n$res = $orm-&gt;table('user')-&gt;add( $data );\r\n\/\/\u66f4\u65b0\u6570\u636e\r\n$res = $orm-&gt;table('user')-&gt;where(['id' =&gt; 4])-&gt;update( ['name' =&gt; 'sdfdsfdsd', 'salt' =&gt; '111'] );\r\n\/\/\u5220\u9664\u6570\u636e\r\n$res = $orm-&gt;table('user')-&gt;where(['id' =&gt; 7, 'id' =&gt; 6])-&gt;delete();\r\n\/\/\u83b7\u53d6\u6267\u884c\u7684sql\u8bed\u53e5\r\necho $orm-&gt;getLastSql();\r\nvar_dump($res);<\/pre>\n<p>&nbsp;<\/p>\n","protected":false},"excerpt":{"rendered":"<p>&lt;?php \/** * Created by PhpStorm. * User: siediyer *  [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[2],"tags":[103,104],"class_list":["post-1475","post","type-post","status-publish","format-standard","hentry","category-php","tag-orm","tag-tp"],"_links":{"self":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1475","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=1475"}],"version-history":[{"count":3,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1475\/revisions"}],"predecessor-version":[{"id":1480,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=\/wp\/v2\/posts\/1475\/revisions\/1480"}],"wp:attachment":[{"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=1475"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=1475"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.siediyer.cn\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=1475"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}