守得云开见月明 首页 关于

PhalAPI快速上手

2018-06-06 / Juexe / Web

个人做快速查找之用。

一、API 层

接口参数规则

public function getRules()
{
    return [
        // test 接口
        'test' => [
            // string, max长度
            'userName' => ['name' => 'username', 'type' => 'string', 'require' => true, 'max' => 10],
            // string,中文验证
            'cnName' => ['name' => 'username', 'type' => 'string', 'format' => 'utf8', 'max' => 10],
            // string,正则验证,邮箱验证
            'email' => ['name' => 'email', 'regex' => "/^([0-9A-Za-z\\-_\\.]+)@([0-9a-z]+\\.[a-z]{2,3}(\\.[a-z]{2})?)$/i"],
            
            // boolean,布林类型,接收[ok,ture,success,on,yes,1]
            'isRemenber' => ['name' => 'isRemenber', 'type' => 'boolean', 'default' => TRUE],
            
            // date,日期自动转时间戳,max时间范围
            'myDate' => ['name' => 'register_date', 'type' => 'date', 'format' => 'timestamp', 'max'=>'2015-01-31 23:59:59'],
            
            // array,接收 "1,2,3" 转换成数组['1','2','3']
            'uids' => ['name' => 'uids', 'type' => 'array', 'format' => 'explode', 'separator' => ',', default=>'4,5,6'],
            // array,接收json {"username":"test","password":"123456"}转换成['username' => 'test', 'password' => '123456'],
            'user' => ['name' => 'params', 'type' => 'array', 'format' => 'json'],
            
            // enum,枚举只接收range,否则失败,注意数值也用字符串表示
            'sex' => ['name' => 'sex', 'type' => 'enum', 'range' => array('female', 'male','0','1')],
            
            // file,文件。max文件字节大小。range文件类型。用$_FILES['upfile']获取
            'upfile' => [
                'name' => 'upfile', 
                'type' => 'file', 
                'min' => 0, 
                'max' => 1024 * 1024, 
                'range' => ['image/jpeg', 'image/png'] , 
                'ext' => ['jpeg', 'png']
            ],
            
            // callback,自定义回调验证
            'version' => ['name' => 'version', 'type' => 'callable', 'callback' => 'App\\Common\\Request\\Version::formatVersion']
        ],
    ];
}

更多 http://docs.phalapi.net/#/v2.0/api?id=参数规则配置详细说明

二、Model 层

获取 NotORM 实例

1. 继承 NotORMModel:在Model层获取

namespace App\Model;
use PhalApi\Model\NotORMModel as NotORM;
class User extends NotORM {
    public function doSth() {
        $orm = $this->getORM(); 
    }
}

2. 助手函数:在API层中获取,目前感觉没什么卵用

$orm = \PhalApi\DI()->notorm;

操作 Model 类

1. 在 API 层中

// 实例化
$model = new App\Model\User();

// 增
$data = array('name' => 'phalapi');
$id = $model->insert($data);
//$id = $model->insert($data, 5); //如果是分表,可以通过第二个参数指定分表的参考ID

// 删
$model->delete(1);

// 改
$data = array('name' => 'test', 'update_time' => time());
$model->update(1, $data); //基于主键的快速更新

// 查
$row = $model->get(1);
$row = $model->get(1, 'id, name'); //取指定的字段
$row = $model->get(1, array('id', 'name')); //可以数组取指定要获取的字段

2. 在 Model 层中

namespace App\Model;
use PhalApi\Model\NotORMModel as NotORM;
class User extends NotORM {
    public function doSth() {
        // 获取实例
        $user = $this->getORM();
        
        // 基本语句
        $user->select()
             ->where()
             ->and()
             ->or()
             ->order()
             ->limit()
             ->group()
             
        // 增
        $user->insert();
        $user->insert_update();
        
        // 删
        $user->where()->delete();
        
        // 改
        $user->where()->update();
        
        // 查
        $user->where()->fetch(); //用于循环
        $user->where()->where()->fetchOne(); //只取最上一条
        $user->where()->fetchAll(); //全部取出
        $user->where()->count();
        $user->where()->min('number');
        $user->where()->max();
        $user->where()->sum();
    }
}

更多 http://docs.phalapi.net/#/v2.0/model?id=在model内的curd基本操作

Model 层高阶操作

1. 事务操作

// Step 1: 开启事务
\PhalApi\DI()->notorm->beginTransaction('db_demo');

// Step 2: 数据库操作
\PhalApi\DI()->notorm->user->insert(array('name' => 'test1'));
\PhalApi\DI()->notorm->user->insert(array('name' => 'test2'));

// Step 3: 提交事务/回滚
\PhalApi\DI()->notorm->commit('db_demo');
//\PhalApi\DI()->notorm->rollback('db_demo');

2. 关联查询

  • NotORM 内置,主键命名必须符合要求:
// SELECT expires_time, user.username, user.nickname FROM phalapi_user_session_0 
// LEFT JOIN phalapi_user AS user 
// ON phalapi_user_session_0.user_id = user.id 
// WHERE (token = 'ABC') LIMIT 1
$rs = \PhalApi\DI()->notorm->user_session_0
    ->select('expires_time, user.username, user.nickname')
    ->where('token', 'ABC')
    ->fetchRow();
var_dump($rs);
  • 自己构造查询语句:
$sql = 'SELECT t.id, t.team_name, v.vote_num '
    . 'FROM phalapi_team AS t LEFT JOIN phalapi_vote AS v '
    . 'ON t.id = v.team_id '
    . 'ORDER BY v.vote_num DESC';

$rows = $this->getORM()->queryAll($sql, array());
var_dump($rows);

更多 http://docs.phalapi.net/#/v2.0/model?id=事务操作、关联查询和其他操作

  • 数据库视图

可见 Phalapi 目前对关联查询支持不佳,以上方法使用起来都不是很方便,可以考虑直接在数据库建立视图来查询,这样会好很多。

 

(完)

添加评论

Powered by Typecho and Theme by Juexe © 2018 Juexe