aros/omnifetch-phalcon

用于简化获取API端点的Phalcon获取库

dev-master 2018-08-24 12:32 UTC

This package is not auto-updated.

Last update: 2024-09-14 19:31:06 UTC


README

OmniFetch for Phalcon是一个有用的库,可以满足您大多数获取API端点的需求。它帮助您根据特定的模型获取数据行,并轻松地将其他相关模型的数据附加到结果中。它还允许使用布尔运算符的组合 ANDOR 以及使用 LIKEISIS NOT 比较运算符进行复杂筛选条件,并支持分页。

Total Downloads

依赖项

  • 已安装Phalcon <= 2.1.0
  • 为了使库正常工作,需要在模型之间建立完整的关系。

安装

安装此扩展的首选方式是通过 Composer

$ composer require aros/omnifetch-phalcon

另一个选择是简单地添加以下行到您的 composer.json 文件的 require 块中。

"aros/omnifetch-phalcon": "dev-master"

然后运行 composer installcomposer update 来下载它并更新自动加载器。

公共方法

  • getAll(string $main_model, array $params, array $settings)

  • getOne(string $main_model, array $params, array $settings)

参数

这些是在 $params(公共方法中的输入参数)中允许的有效键。以下是可用的键:

  • embeds - 这是一个包含要附加到结果中主模型数据的关联模型别名的数组。
  • filters - 这是一个包含用于获取数据的筛选条件的数组。条件将按照它们在数组中的顺序处理和组合。每个条件本身也是一个数组,包含以下内容:
    • field - 这表示用于筛选的列。请注意:模型的别名必须在列的名称中使用,例如 Card.id。此处使用的字段也可以是主模型的相关模型。例如,如果 IssuerCard 的相关模型,则可以使用 Issuer.name 进行筛选。
    • value - 这表示用于筛选的字段的值。
    • cmp(可选)- 这表示要使用的比较运算符。默认情况下为 =。这可以是 ><=>=<=!=LIKEISIS NOT
    • cond(可选)- 这表示要使用的布尔运算符。默认情况下为 AND,但也支持 OR
  • page - 这是一个表示要获取的页码的整数。当省略时,默认为 0,表示第一页。仅适用于 getAll 方法。
  • page_size - 这是一个表示当前页面最大大小的整数。当省略时,默认为 20。仅适用于 getAll 方法。
  • order_by - 这是一个表示 SQL 中排序子句的字符串。请注意:应使用模型的别名在排序中使用的列的名称中。例如,使用 Card.id 而不是 id,以避免列歧义,尤其是在使用 embeds 时。

设置

这些是在 $settings(公共方法中的输入参数)中允许的有效键。以下是可用的键:

  • 主键 - 它表示 主模型 的主键。这是 必须的

输出参数

对于 getAll 方法,以下是其输出参数

  • list - 包含结果列表
  • pagination - 包含分页信息
    • next - 下一个页面索引,如果没有下一页则为 null
    • previous - 上一个页面索引,如果没有上一页则为 null
    • total_count - 符合过滤条件所有记录的总数
    • count - 当前页面中的记录数

对于 getOne 方法,输出是一个单独的记录,如果记录不存在则为 null

示例用法

首先需要在模型中设置关系

<?php

class Book extends \Phalcon\Mvc\Model
{
    //...
    public function initialize()
    {
        $this->belongsTo('type_id', 'BookType', 'id', array('alias' => 'BookType'));
        $this->belongsTo('author_id', 'Author', 'id', array('alias' => 'Author'));
    }
    //...
}

以下是对 getOne 方法的示例用法

<?php
use \OmniFetch\PhalconOmniFetch;

class BookController extends ControllerBase
{
    public function getOneAction()
    {
        $filters = $this->request->getQuery('filters');
        // filters = [{"field": "Book.id", "value": "230"}]
        $embeds = $this->request->getQuery('embeds');
        // embeds = ["BookType", "Author"]

        $omni_fetch = new PhalconOmniFetch();
        $result = $omni_fetch->getOne('Book', [
            'embeds' => json_decode($embeds),
            'filters' => json_decode($filters, true)
        ],[
            'primary_key' => 'id'
        ]);

        return $this->response->sendSuccess($result);
    }
}

结果将是

{
    "id": 230,
    "author_id": 1,
    "type_id": 1,
    "name": "Wizard of Oz",
    "code": "CB128EF2",
    "created_date": "2015-01-02 14:23:12",
    "Author": {
        "id": 1,
        "name": "Jon Doe",
        "address": "Ikeja, Lagos State, Nigeria"
    },
    "BookType": {
        "id": 1,
        "name": "fantasy"
    }
}

以下是对 getAll 方法的示例用法

<?php
use \OmniFetch\PhalconOmniFetch;

class CardController extends ControllerBase
{
    public function getAllAction()
    {
        $filters = $this->request->getQuery('filters');
        // filters = [{"field": "Book.code", "value": "%25CB%25", "cmp": "LIKE"}]
        // [Note: %25 is the url encoding for %]
        $embeds = $this->request->getQuery('embeds');
        // embeds = ["BookType", "Author"]
        $order_by = $this->request->getQuery('order_by');
        // order_by = Book.id DESC

        $omni_fetch = new PhalconOmniFetch();
        $result = $omni_fetch->getAll('Book', [
            'embeds' => json_decode($embeds),
            'filters' => json_decode($filters, true),
            'order_by' => $order_by,
            'page_size' => 3 //made it 3 for example sake
        ],[
            'primary_key' => 'id'
        ]);

        return $this->response->sendSuccess($result);
    }
}

结果将是

{
    "list" : [
        {
            "id": 230,
            "author_id": 1,
            "type_id": 1,
            "name": "Wizard of Oz",
            "code": "CB128EF2",
            "created_date": "2015-01-02 14:23:12",
            "Author": {
                "id": 1,
                "name": "Jon Doe",
                "address": "Ikeja, Lagos State, Nigeria"
            },
            "BookType": {
                "id": 1,
                "name": "fantasy"
            }
        },
        {
            "id": 127,
            "author_id": 2,
            "type_id": 2,
            "name": "Love once again",
            "code": "987ACB43",
            "created_date": "2015-03-04 03:50:34",
            "Author": {
                "id": 2,
                "name": "Mary Porter",
                "address": "Abeokuta, Ogun State, Nigeria"
            },
            "BookType": {
                "id": 2,
                "name": "romance"
            }
        },
        {
            "id": 59,
            "author_id": 1,
            "type_id": 5,
            "name": "The life of Mine",
            "code": "873FCB12",
            "created_date": "2014-05-12 20:01:59",
            "Author": {
                "id": 1,
                "name": "Jon Doe",
                "address": "Ikeja, Lagos State, Nigeria"
            },
            "BookType": {
                "id": 5,
                "name": "biography"
            }
        }
    ],
    "pagination": {
        "next": 1,
        "previous": null,
        "total_count": "6",
        "count": 3
    }
}

待办事项

  • 为嵌入项添加对多对多关系的支持
  • 添加对复合主键的支持