3.0.16 • Published 7 months ago

manong v3.0.16

Weekly downloads
-
License
ISC
Repository
-
Last release
7 months ago

#框架介绍

码农 Manong 框架是一个为用 NodeJS 程序语言编写网络应用程序的人员提供的软件包。 提供强大的、完整的类库包,满足开发中的项目需求,Manong 框架可以将需要完成的任务代码量最小化,大大提高项目开发效率与质量,当然使用是非常简便、快捷的。高效的核心编译处理机制让系统运行更快,提供丰富的的错误解决方案,让修正代码变得更快速。

做为优秀的框架产品,Manong在系统性能上做了大量的优化处理,只为让程序员使用 Manong 框架强悍的功能,用最短的时间完成项目的开发。为了便于快速进行项目开发,框架也提供了相应的前端功能组件使程序员从繁琐的组件调试中解脱出来。 #开发规范

##命名规范

  1. 所有类文件使用 驼峰 命名规范
  2. 文件名大小写要一致,否则上传到Linux服务器将无法解析
  3. 函数命名规则是第个单词以 _ 连接如 get_size
  4. 常量以大写字母与下划线组合 如 APP_PATH
  5. 默认模板是.php 可以通过配置文件修改
  6. 数据库与数据表使用小写字母与下划线组合,如 hd_user

##开发建议

  1. 必须遵守框架命名规范
  2. 开发过程中开启DEBUG模式,有助于快速分析错误
  3. 更改环境后(例如上传到服务器等)必须删除 Storage 缓存目录
  4. 私有 $_GET 变量是框架运行中用到的特殊变量,所以同学们不能使用

私有变量

GET 私有变量指 Manong 占有的 $_GET 值,开发人员请不要使用这些变量,否则会产生异常。

  • $_GET['s'] 模块/控制器/动作数据
  • $_GET['p'] 分页变量

#入口文件

##介绍

Manong 框架使用单一入口访问,所有的请求都是通过入口文件完成的。无论从安全性还是方法调用及文件加载方面都带来了很高的便捷性。

##使用

常规使用

import Manong from 'manong';
import * as functions from './app/common/function.js';
import middleware from './app/common/middleware.js';
import config from './config.js';

const files = include('./app/controllers', false, /\.js$/);

const model = include('./app/modules', true, /\.js$/);

const {controllers, modules} = getCommonData(files, model);

(new Manong({
    middleware: middleware,
    controllers: controllers,
    modules: modules,
    functions: functions,
    config: config
})).run();

目录结构

Manong 框架采用扁平化设计 ,目录结构更加清晰,使用 控制反转(IoC)依赖注入 ,使用扩展Manong框架变得更简单。

框架下载后的目录结构:

├── app                   项目目录
│   ├── common            公共方法
│   ├── ├── function.js   自定义全局函数
│   ├── └── middleware.js 中间件
│   ├── controllers       控制器
│   └── modules           模型
├── index.js            项目入口文件
├── public              公共文件夹
├── view                视图文件夹
└──config.js           配置文件

系统常量

##介绍

系统自定了义一些常量方便我们在程序中使用

  __DIR__

#设置配置

项目运行中我们会有要临时更改一个配置项的需求,比如临时更改模板目录等,这时可以使用以下方式完成。

设置配置

C('alipay.key.auth','houdunwang');

获取配置

配置管理使用 Config 服务完成,可以在框架中直接使用Config 不需要引入类。

C('view.path');

获取配置文件使用 get 方法完成,参数为 ”配置文件名.配置项"的形式。

如果想要获取配置文件的所有内容,只传递文件名就可以:

C('app');

工具函数

u 生成url

##创建控制器

export default class DemoController extends BasicController {
    /**
     * 获取详情信息
     */
    actionIndex() {
        return C("database");
    }
}

##使用命令创建

创建基本控制器

manong c user

创建模型

manong m user

#数据库配置 #配置

配置

数据库配置需要修改system/config/database.php文件

  "database": {
    "read": {
      "host": "",
      "driver": "mysql",
      "charset": "utf8",
      "user": "",
      "password": "",
      "database": ""
    },
    "write": {
      "host": "",
      "driver": "mysql",
      "charset": "utf8",
      "user": "",
      "password": "",
      "database": ""
    },
    "prefix": "hk_"
  },

#核心操作

数据库查询构造器 (query builder) 提供方便、流畅的接口,用来建立及执行数据库查找语法。在你的应用程序里面,它可以被使用在大部分的数据库操作,而且它在所有支持的数据库系统上都可以执行。

Manong 框架的 CURD 的自由度超过你的想象,当开启 DEBUG 模式后,所有 SQL 语句问题均会在页面中显示,一目了然。

注意: Manong 查询构造器使用 PDO 参数绑定,以保护应用程序免于 SQL 注入,因此传入的参数不需额外转义特殊字符。

###预准备操作
Manong支持使用预准备查询,可以完全避免SQL注入。

修改操作

DB.execute("update news set total=:total where id=:id",[':total'=>5,':id'=>1]);

查询操作

使用标识名

DB.query("select * from site where siteid=:siteid AND name=:name",
[':siteid'=>36,':name'=>'码农']);

使用占位符

DB.query("select * from news where title like ?",['%码农网%']) 

##常用操作
查找

下面是使用原生 SQL 语句进行查询,更灵活的方式请查看 查询构造器 部分

DB.table('user').where('id','>',1).get();

新增

DB.table('user').insert(['username'=>'向军','qq'=>'2300071698']);
//数组数据会过滤掉非法字段

替换

DB.table('user').replace(['id'=>1,'username'=>'向军','qq'=>'2300071698']);
//如果字段中有主键或唯一索引,并且数据存在,replace操作将执行替换当前记录的操作

添加并获取自增主键

如果数据表有自动递增的ID,可以使用 insertGetId 添加数据并返回该 ID

DB.table('user'). insertGetId(['username'=>'向军','qq'=>'2300071698']);

更新

DB.table('user').where("id",1).update(['username'=>'码农网']);
//数组数据会过滤掉非法字段

删除

DB.table('user').where('id',1).delete();
//删除指定的主键值
DB.table('user').delete(1);
DB.table('user').delete([2,3,5]);

自增一个字段值

将total字段值加2

DB.table("user").where('id',1).increment('total',2);

自减一个字段值

DB.table("user").where('id',1).decrement('total',2);

将total字段减少2

获取自增主键

$db = DB.table( 'news' );
$db.insert( [ 'title' => '码农网' ] );
echo $db.getInsertId();

获取受影响条数

$db = DB.table( 'news' );
$db.update( [ 'title' => '码农人' ] );
echo $db.getAffectedRow();

#查询构造器

select

从数据表中取得所有的数据列

DB.table('user').get();

取得指定的字段

DB.table('user').get(['username','age']);

从数据表中取得单一数据列

DB.table('user').where('username','向军').first();

//取出指定主键的值
DB.table('user').first(2);

从数据表中取得单一数据列的单一字段

DB.table('user').where('username', '向军').pluck('username');

//返回第一条记录的 username 字段值

取得单一字段值的列表

DB.table('user').lists('username');

//满足条件记录的所有username字段
[
    [0] => admin
    [1] => hdxj
]

返回一维数组,第一个字段做为键名使用,第 2 个字段做为键值

DB.table('user').lists('id,username'); 

//id 字段做为键名使用
[
    [1] => admin
    [2] => hdxj
]

多个字段返回二维数组,第一个字段值做为键名使用,其余字段做为键值

DB.table('user').lists('id,username,age');

//返回值如下
[
    [1] => [
            [id] => 1
            [username] => admin
            [age] => 22
           ]

    [2] => [
            [id] => 2
            [username] => hdxj
            [age] => 67
           ]
]

指定查询结果字段

DB.table('user').field('username AS name,age').get();

或

DB.table('user').field(['username','age']).get();

根据某个字段查询

DB.table('user').getByName("hdphp");
//返回一条记录

增加查询子句到现有的查询中

$db = DB.table('user').field('username AS name','age','id');
$db.where('id','>',2).get();

使用 where 及运算符

DB.table('user').where('id','>',1).get();
DB.table('user').where('id','>',1).where('id','<',10).get();

使用andwhere

DB.table('user').where('id','>',1).andwhere('id','<',10).get();

使用orwhere

DB.table('user').where('id','>',1).orwhere('id','<',10).get();

使用 logic 条件连接符

DB.table('user').where('id','>',1).logic('or').where('id','<',22).get();

预准备whereRaw

DB.table('user').whereRaw('age > ? and username =?', [1,'admin']).get();

使用 whereBetween

DB.table('user').whereBetween('id',[10,30]).get();

使用 WhereNotBetween

DB.table('user').whereNotBetween('id',[10,30]).get();

使用 WhereIn

DB.table('user').whereIn('id',[2,3,9]).get();

使用 WhereNotIn

DB.table('user').whereNotIn('id',[3,5,6]).get();

使用 WhereNull

DB.table('user').whereNull('username').get();

使用 WhereNotNull

DB.table('user').whereNotNull('id').get();

指定条件关系

DB.table('user').where('id','>',1).logic('AND').whereBetween('id',[1,10]).get();

排序(Order By)

DB.table('user').orderBy('id','DESC').get();

DB.table('user').orderBy('id','DESC').orderBy('rank','ASC').get();
//多个排序条件

分组GROUP BY

DB.table('user').groupBy('age').get();

分组筛选HAVING

DB.table('user').groupBy('age').having('count(sex)','>',2).get();

取部分数据LIMIT

DB.table('user').limit(2).get();
DB.table('user').limit(2,5).get();

##聚合

DB.table("user").count('id');
DB.table("user").max('id');
DB.table("user").min('id');
DB.table("user").avg('id');
DB.table("user").sum('id');

##JOIN

多表关联INNER JOIN

DB.table('user')
	.join('class','user.cid','=','class.cid')
	.join('contacts','user.id','=','contacts.uid')
	.get()

多表关联LEFT JOIN

DB.table('user').leftJoin('class','user.cid','=','class.cid').get();

多表关联RIGHT JOIN

DB.table('user').rightJoin('class','user.cid','=','class.cid').get();

#定义模型

Manong 框架提供了高效的 Model(模型)操作机制,具有以下特点:

  1. 可以对基础模型类进行随意的扩充
  2. 支持加载其他模块或其他应用类某个模块的模型
  3. 丰富的加载模型方式
  4. 功能强大的数据操作方式
  5. 自动验证
  6. 自动完成
  7. 自动映射
  8. 表单令牌

提示:所有查询构造器里的方法,查询 Model 模型时也可以使用。

##创建模型
我们先从建立一个 Model 模型开始。模型通常放在 system\model 目录下,但是您可以将它们放在任何地方。

<?php namespace system\model;

use hdphp\model\model;

class News extends model{
	protected $table = "news";
}

模型数据表是可选的,如果不设置系统将取模型名做为表名,请看下面的代码:

class News extends Model{
}

模型定义并没有设置$table 属性,那么系统会将模型名称转为小写后做为表名。

还有一种情况,如果我们有一个数据表user_role,我们在不设置表名时如何定义模型名呢?代码如下:

class UserRole extends Model{
}

#模型动作

##介绍

Manong 实现了ActiveRecords模式的ORM模型,表映射到类,记录映射到对象。最大的特点就是使用方便和便于理解(因为采用了对象化),提供了开发的最佳体验,从而达到敏捷开发的目的。

如果数据库表里有 updated_at 和 created_at 两个字段,可以通过模型自动操作这两个字段,只要将 $timestamps 属性设为 true 即可。

##查询

AR模式的数据查询比较简单,因为更多情况下面查询条件都是以主键或者某个关键的字段。这种类型的查询,Manong 有着很好的支持。 先举个最简单的例子,假如我们要查询主键为8的某个用户记录,如果按照之前的方式,我们可能会使用下面的方法:

// 查找id为8的用户数据,返回值为数组
User::where('uid',8).find();

用AR模式的话可以直接写成(返回值为模型对象):

User::find(5);

##新增

添加成功时,如果数据表有请主键则返回主键,否则返回true

// 然后直接给数据对象赋值
$User.name = 'hdphp';
$User.email = 'houdunwang@126.com';
// 把数据对象添加到数据库
User::add();

如果使用了create方法创建数据对象的话,仍然可以在创建完成后进行赋值

$db=User::find(1);
$db.create(); // 创建User数据对象,默认使用 $_POST
// 增加或者更改其中的属性
$User.nickname = 'php培训';
$user.save();

##更新

使用$_POST数据添加

$User = new User;
//create 方法会执行自动验证,如果没有传递数据时使用$_POST数据
if($User.create())
{
	$user.save();
}

更新找到的数据

要更新模型,可以取出它,更改属性值,然后使用 save 方法:

$db = User::find(1); // 查找主键为1的数据
$db.username = 'hdphp'; // 修改数据对象
$db.save(); // 保存当前数据对象

上面这种方式仅仅是示例,不代表保存操作之前一定要先查询。与下面的方式是等效的:

$User = new User;
$User.id = 1;
$User.username = 'hdphp'; // 修改数据对象
$User.save(); // 保存当前数据对象

记录不存在才新增

$user =$User.firstOrCreate(['username' => '李四'],['username'=>'李四','age'=>22]);

//如果不存在叫 “李四” 的用户就新增用户

指定更新数据

数据中必须存在主键值

$User.save(array('id'=>1,'username'=>'houdunwang.com'));

您可以结合查询语句更新

$user.where('id', '>', 100).update(['cid' => 2]);

更新模型的时间戳

$user.touch();

//表中需要存在 updated_at 字段

使用save更新时,最好结合create方法

获取模型数据

$user.getData();

##删除

可以删除当前查询的数据对象

$User.find(2);
$User.delete(); // 删除当前的数据对象
或者直接根据主键进行删除
$User.delete(8); // 删除主键为8的数据
$User.delete('5,6'); // 删除主键为5、6的多个数据
3.0.16

7 months ago

3.0.15

7 months ago

3.0.12

8 months ago

3.0.13

8 months ago

3.0.11

9 months ago

3.0.9

12 months ago

3.0.10

10 months ago

3.0.8

1 year ago

3.0.7

1 year ago

3.0.6

1 year ago

3.0.5

1 year ago

3.0.4

1 year ago

3.0.3

1 year ago

3.0.2

1 year ago

3.0.1

1 year ago

3.0.0

1 year ago

2.0.33

2 years ago

2.0.34

2 years ago

2.0.31

2 years ago

2.0.32

2 years ago

2.0.28

2 years ago

2.0.29

2 years ago

2.0.30

2 years ago

2.0.26

2 years ago

2.0.27

2 years ago

2.0.15

2 years ago

2.0.16

2 years ago

2.0.13

2 years ago

2.0.14

2 years ago

2.0.11

2 years ago

2.0.12

2 years ago

2.0.9

2 years ago

2.0.10

2 years ago

2.0.19

2 years ago

2.0.17

2 years ago

2.0.18

2 years ago

2.0.24

2 years ago

2.0.25

2 years ago

2.0.22

2 years ago

2.0.23

2 years ago

2.0.20

2 years ago

2.0.21

2 years ago

2.0.7

2 years ago

2.0.6

2 years ago

2.0.8

2 years ago

2.0.5

2 years ago

2.0.4

3 years ago

2.0.3

3 years ago

2.0.1

3 years ago

2.0.0

3 years ago