themost / phpclient
MOST Web 框架 PHP 客户端库
Requires
- pear/http_request2: dev-trunk
Requires (Dev)
- phpunit/phpunit: 4.7.x-dev
This package is not auto-updated.
Last update: 2024-10-02 06:58:58 UTC
README
MOST Web 框架 PHP 客户端库
用法
如果您没有MOST Web框架应用,请克隆MOST Web框架 OMS 示例应用并按照安装说明进行操作。
创建一个新的PHP项目,并使用MOST Web框架PHP客户端与该应用进行通信。
身份验证
使用基本身份验证
context->authenticate("alexis.rees@example.com","user");
使用基于cookie的身份验证
//get cookie from request
context.getService().setCookie($response->getCookies());
ClientDataContext 类
模型(名称)
根据给定的名称获取ClientDataModel类的实例。
$result = $context->model("Order")
->where("orderStatus")->equal(1)
->getItems();
var_dump($result);
getService()
获取与该数据上下文关联的ClientDataService实例。
print $context->getService()->getBase();
setService(service)
将给定的ClientDataService实例与该数据上下文关联。
$context->setService(new MyDataService("http://data.example.com"));
ClientDataModel 类
getName()
获取表示此数据模型名称的字符串。
getService()
获取与此数据模型关联的ClientDataService实例。
remove(obj)
移除给定项目或项目数组。
$order = new stdClass();
$order->id = 23;
$context->model("Order")->remove($order);
var_dump($order);
save(obj)
创建或更新给定项目或项目数组。
$result = $context->model("Order")
->where("id")
->equal("23")
->getItem();
$result->orderStatus = 3;
$context->model("Order")->save($result);
var_dump($result);
getSchema()
返回此数据模型的JSON模式。
$context = $this->getContext();
$schema = $context->model("Group")->getSchema();
var_dump($schema);
select(...attr)
通过选择属性或属性集合初始化并返回ClientDataQueryable类的实例。
$result = $context->model("User")
->where("name")
->equal("alexis.rees@example.com")
->select("id","name","description")
->first();
var_dump($result) ;
skip(num)
通过指定要跳过的记录数初始化并返回ClientDataQueryable类的实例。
$context = $this->getContext();
$result = $context->model("Order")
->skip(10)
->take(10)
->getItems();
var_dump($result);
take(num)
通过指定要获取的记录数初始化并返回ClientDataQueryable类的实例。
$context = $this->getContext();
$result = $context->model("Order")
->take(10)
->getItems();
var_dump($result);
where(attr)
使用给定的属性作为左操作数初始化比较表达式,并返回ClientDataQueryable类的实例。
$context = $this->getContext();
$result = $context->model("Order")
->where("orderedItem/category")->equal("Laptops")
->take(10)
->getItems();
var_dump($result);
ClientDataQueryable 类
ClientDataQueryable类使开发人员能够对数据模型执行简单和扩展查询。ClientDataQueryable类遵循DataQueryable,这是由MOST Web框架ORM服务器端模块引入的。
逻辑运算符
或者(或)
$result = $context->model("Product")
->where("category")->equal("Desktops")
->either("category")->equal("Laptops")
->orderBy("price")
->take(5)
->getItems();
var_dump($result);
也(与)
$context = $this->getContext();
$result = $context->model("Product")
->where("category")->equal("Laptops")
->also("price")->between(200,750)
->orderBy("price")
->take(5)
->getItems();
var_dump($result);
比较运算符
等于
$context = $this->getContext();
$result = $context->model("Product")
->where("category")
->equal("Laptops")
->orderBy("price")
->getItems();
var_dump($result);
不等于
$context = $this->getContext();
$result = $context->model("Product")
->where("category")
->notEqual("Laptops")
->also("category")
->notEqual("Desktops")
->orderBy("price")
->getItems();
var_dump($result);
大于
$context = $this->getContext();
$result = $context->model("Order")
->where("orderedItem/price")->greaterThan(968)
->also("orderedItem/category")->equal("Laptops")
->also("orderStatus/alternateName")->notEqual("OrderCancelled")
->select("id",
"orderStatus/name as orderStatusName",
"customer/description as customerDescription",
"orderedItem")
->orderByDescending("orderDate")
->take(10)
->getItems();
var_dump($result);
大于等于
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->greaterOrEqual(1395.9)
->orderByDescending("price")
->take(10)
->getItems();
var_dump($result);
小于
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->lowerThan(263.56)
->orderBy("price")
->take(10)
->getItems();
var_dump($result);
小于等于
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->lowerOrEqual(263.56)
->also("price")->greaterOrEqual(224.52)
->orderBy("price")
->take(5)
->getItems();
var_dump($result);
包含
$context = $this->getContext();
$result = $context->model("Product")
->where("name")->contains("Book")
->also("category")->equal("Laptops")
->orderBy("price")
->take(5)
->getItems();
var_dump($result);
介于
$context = $this->getContext();
$result = $context->model("Product")
->where("category")->equal("Laptops")
->either("category")->equal("Desktops")
->andAlso("price")->between(200,750)
->orderBy("price")
->take(5)
->getItems();
var_dump($result);
聚合函数
计数
$context = $this->getContext();
$result = $context->model("Product")
->select("category", "count(id) as total")
->groupBy("category")
->orderByDescending("count(id)")
->getItems();
var_dump($result);
最小值
$context = $this->getContext();
$result = $context->model("Product")
->select("category", "min(price) as minimumPrice")
->where("category")->equal("Laptops")
->either("category")->equal("Desktops")
->groupBy("category")
->orderByDescending("min(price)")
->getItems();
var_dump($result);
最大值
$context = $this->getContext();
$result = $context->model("Product")
->select("category", "max(price) as maximumPrice")
->where("category")->equal("Laptops")
->getItem();
var_dump($result);
字符串函数
索引
$context = $this->getContext();
$result = $context->model("Product")
->where("name")->indexOf("Intel")
->greaterOrEqual(0)
->getItems();
var_dump($result);
子字符串
$context = $this->getContext();
$result = $context->model("Product")
->where("name")->substr(6,4)
->equal("Core")
->getItems();
var_dump($result);
以...开头
$context = $this->getContext();
$result = $context->model("Product")
->where("name")->startsWith("Intel Core")
->equal(true)
->getItems();
var_dump($result);
以...结尾
$context = $this->getContext();
$result = $context->model("Product")
->where("name")->endsWith("Edition")
->equal(true)
->getItems();
var_dump($result);
小写
$context = $this->getContext();
$result = $context->model("Product")
->where("category")->toLowerCase()
->equal("laptops")
->getItems();
var_dump($result);
大写
$context = $this->getContext();
$result = $context->model("Product")
->where("category")->toUpperCase()
->equal("LAPTOPS")
->getItems();
var_dump($result);
日期函数
日期
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getDate()
->equal("2015-04-18")
->getItems();
var_dump($result);
月份
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()
->equal(4)
->getItems();
var_dump($result);
日
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()->equal(4)
->also("orderDate")->getDay()->lowerThan(15)
->getItems();
var_dump($result);
年
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()->equal(5)
->also("orderDate")->getDay()->lowerOrEqual(10)
->also("orderDate")->getFullYear()->equal(2015)
->getItems();
var_dump($result);
小时
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()->equal(5)
->also("orderDate")->getDay()->lowerOrEqual(10)
->also("orderDate")->getHours()->between(10,18)
->getItems();
var_dump($result);
分钟
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()->equal(5)
->also("orderDate")->getHours()->between(9,17)
->also("orderDate")->getMinutes()->between(1,30)
->getItems();
var_dump($result);
秒
$context = $this->getContext();
$result = $context->model("Order")
->where("orderDate")->getMonth()->equal(5)
->also("orderDate")->getHours()->between(9,17)
->also("orderDate")->getMinutes()->between(1,30)
->also("orderDate")->getSeconds()->between(1,45)
->getItems();
var_dump($result);
数学函数
四舍五入
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->round()->lowerOrEqual(177)
->getItems();
var_dump($result);
向下取整
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->floor()->lowerOrEqual(177)
->getItems();
var_dump($result);
向上取整
$context = $this->getContext();
$result = $context->model("Product")
->where("price")->ceil()->lowerOrEqual(177)
->getItems();
var_dump($result);
方法
also(name)
准备逻辑与表达式。
参数
- name: 将在此表达式中使用的字段名称
andAlso(name)
准备逻辑与表达式。如果已定义表达式,则将新与表达式包装在内
参数
-
name: 将在此表达式中使用的字段名称
$context = $this->getContext(); $result = $context->model("Product") ->where("category")->equal("Laptops") ->either("category")->equal("Desktops") ->andAlso("price")->round()->lowerOrEqual(250) ->getItems(); var_dump($result);
expand(...attr)
参数
- attr:表示将要展开的字段或字段数组的字符串参数数组。如果缺少attr,则将删除之前定义的所有可展开字段
定义在最终结果中要展开的属性或属性数组。当需要将非可展开属性展开到最终结果中时,应使用此操作。
$context = $this->getContext();
$result = $context->model("Order")
->where("customer")->equal(337)
->orderByDescending("orderDate")
->expand("customer")
->getItem();
var_dump($result);
first()
执行指定的查询并返回第一个项目。
$context = $this->getContext();
$result = $context->model("User")
->where("name")
->equal("alexis.rees@example.com")
->first();
var_dump($result);
getItem()
执行指定的查询并返回第一个项目。
$context = $this->getContext();
$result = $context->model("User")
->where("name")
->equal("alexis.rees@example.com")
->getItem();
var_dump($result);
getItems()
执行指定的查询并返回一个项目数组。
$context = $this->getContext();
$result = $context->model("Product")
->where("category")
->equal("Laptops")
->getItems();
var_dump($result);
getList()
根据指定的分页参数执行底层查询并返回一个结果集。结果集包含以下属性
-
total (number):记录总数
-
skip (number):跳过的记录数
-
records (Array):表示查询结果的数组对象。
$context = $this->getContext(); $result = $context->model("Product") ->where("category") ->equal("Laptops") ->take(10) ->getList(); var_dump($result);
skip(val)
通过跳过指定的记录数来准备分页操作
参数
-
val:要跳过的记录数
$context = $this->getContext(); $result = $context->model("Order") ->skip(10) ->take(10) ->getItems(); var_dump($result);
take(val)
通过获取指定的记录数来准备数据分页操作
参数
-
val:要获取的记录数
$context = $this->getContext(); $result = $context->model("Order") ->take(10) ->getItems(); var_dump($result);
groupBy(...attr)
准备一个分组表达式
$context = $this->getContext();
$result = $context->model("Order")
->select("orderedItem/model as productModel",
"orderedItem/name as productName",
"count(id) as orderCount")
->where("orderDate")->getFullYear()->equal(2015)
->groupBy("orderedItem")
->orderByDescending("count(id)")
->take(5)
->getItems();
var_dump($result);
orderBy(...attr)
准备一个升序排序操作
$context = $this->getContext();
$result = $context->model("Product")
->orderBy("category","name")
->take(5)
->getItems();
var_dump($result);
thenBy(...attr)
继续升序排序操作
$context = $this->getContext();
$result = $context->model("Product")
->orderBy("category")
->thenBy("name")
->take(5)
->getItems();
var_dump($result);
orderByDescending(...attr)
准备一个降序排序操作
$context = $this->getContext();
$result = $context->model("Product")
->orderByDescending("category","name")
->take(5)
->getItems();
var_dump($result);
thenByDescending(...attr)
继续降序排序操作
$context = $this->getContext();
$result = $context->model("Product")
->orderByDescending("category")
->thenByDescending("name")
->take(5)
->getItems();
var_dump($result);