1. Go to this page and download the library: Download yogcloud/easyswoole-orm library. Choose the download type require.
2. Extract the ZIP file and open the index.php.
3. Add this code to the index.php.
<?php
require_once('vendor/autoload.php');
/* Start to develop here. Best regards https://php-download.com/ */
yogcloud / easyswoole-orm example snippets
// 主键条件
Model::invoke()->get(1);
=> select * from table where primary_key = 1
// 数组条件
Model::invoke()->get([col => val]);
// 闭包条件查询
Model::invoke()->get(function ($query) {
$query->where(col, val);
// Or $query->where([col => val]);
});
Model::invoke()->where(col, val)->get();
Model::invoke()->where(col, val, '=')->get();
Model::invoke()->where(col, val)->find();
=> select * from table where col = val limit 1
// 主键条件
Model::invoke()->all('1,2,3');
// 主键条件数组
Model::invoke()->all([1,2,3]);
=> select * from table where primary_key in (1,2,3)
// 数组条件
Model::invoke()->all([col => val]);
=> select * from table where col = val
// 闭包条件查询
Model::invoke()->all(function ($query) {
$query->where(col, val)->limit(num)->order(col, 'asc')
});
// 链式调用
Model::invoke()->where(col, val)
->limit(num)
->order(col, 'asc')
->select();
=> SELECT * FROM table WHERE col = val ORDER BY col ASC LIMIT num
// 获取某行某个字段的值
Model::invoke()->where(col, val)->value(col)
=> SELECT col FROM table WHERE col = val LIMIT 1
// 获取某个列的值
Model::invoke()->where(col, val)->value(col)
=> SELECT col FROM table WHERE col = val
// 获取多个列的值
Model::invoke()->where(col, val)->value(col1, col2)
Model::invoke()->where(col, val)->value('col1,col2')
=> SELECT col1,col2 FROM table WHERE col = val
Model::invoke()->chunk(3, function ($objs) {
});
=> SELECT * FROM table WHERE primary_key > 1 ORDER BY primary_key ASC LIMIT 1
=> SELECT * FROM table WHERE primary_key > 2 ORDER BY primary_key ASC LIMIT 1
=> SELECT * FROM table WHERE primary_key > 3 ORDER BY primary_key ASC LIMIT 1
$model = Model::invoke()->get(1);
$model->col = val;
$model->save();
=> SELECT * FROM table WHERE primary_key = 1 LIMIT 1
=> UPDATE table SET col = val WHERE primary_key = 1
$model = Model::invoke();
$model->save([
col => val
], [where_col => where_val]);
=> UPDATE table SET col = val WHERE where_col = where_val
// 只更新指定字段
$model = Model::invoke();
$model->allowField(['col'])->save([
col => val,
col1 => val1
], [where_col => where_val]);
=> UPDATE table SET col = val WHERE where_col = where_val
// 使用 where 条件更新
$model = Model::invoke();
$model->where(col, val)->update([col1 => val1]);
=> UPDATE table SET col1 = val1 WHERE col = val;
// 使用主键条件更新
// 更新的数据包含主键列时 无需使用 where 方法
$model = Model::invoke();
$model->update([primary_key => val11, col => val]);
=> UPDATE table SET col = val WHERE primary_key = val11;
$model = Model::invoke();
$model->save([col2 => val2], function ($query) {
$query->where(col, val)->where(col1, val1, '>');
});
=> UPDATE table SET col2 = val2 WHERE (col = val AND col1 > val1)
// 显式指定更新数据
$model = Model::invoke();
$model->isUpdate(true)
->save([primary_key => 1, col => val]);
=> UPDATE table SET col = val WHERE primary_key = 1
$model = Model::invoke()->get(1);
$model->delete();
=> DELETE FROM table WHERE primary_key = 1
$model = Model::invoke();
$model->destroy(1);
=> DELETE FROM table WHERE primary_key = 1
$model = Model::invoke();
$model->destroy('1,2,3');
$model->destroy([1,2,3]);
=> DELETE FROM table WHERE primary_key = 1;
=> DELETE FROM table WHERE primary_key = 2;
=> DELETE FROM table WHERE primary_key = 3;
$model = Model::invoke();
$model->destroy([col => val]);
=> DELETE FROM table WHERE col = val;
// 闭包删除
$model = Model::invoke();
$model->destroy(function ($query) {
$query->where(col, val, '>');
});
=> SELECT * FROM table WHERE col > val
=> DELETE FROM table WHERE primary_key = key1
=> DELETE FROM table WHERE primary_key = key2
=> DELETE FROM table WHERE primary_key = key3
...
=> DELETE FROM table WHERE primary_key = keyX
// keyX 取决于最前面查询返回的所有记录的 主键id
// 根据 where 查询条件删除
$model = Model::invoke();
$model->where(col, val, '>')->delete();
=> DELETE FROM table WHERE col > val;
$model = Model::invoke();
$model->count();
=> SELECT COUNT(*) AS es_count FROM table LIMIT 1
$model = Model::invoke();
$model->count(col);
=> SELECT COUNT(col) AS es_count FROM table LIMIT 1
$model = Model::invoke();
$model->where(col, val, '>')->count();
=> SELECT COUNT(*) AS es_count FROM table WHERE col > val LIMIT 1
$model = Model::invoke();
$model->max('age');
=> SELECT MAX(col) AS es_max FROM table LIMIT 1
$model = Model::invoke();
$model->min(col);
=> SELECT MIN(col) AS es_min FROM table LIMIT 1
$model = Model::invoke();
$mode->where(col, val)->avg(col);
=> SELECT AVG(col) AS es_avg FROM table WHERE col = val LIMIT 1
$model = Model::invoke();
$model->sum(col);
=> SELECT SUM(col) AS es_sum FROM table WHERE col = val LIMIT 1
$model = Model::invoke();
$mode->where(col, val)->SUM(col);
=> SELECT SUM(col) AS es_sum FROM table WHERE col = val LIMIT 1
./vendor/bin/co-phpunit tests
Loading please wait ...
Before you can download the PHP files, the dependencies should be resolved. This can take some minutes. Please be patient.