Implementiert durch Annotationen in thinkphp5.1:
Datenvalidator
Ausgew?hlte Erfassungs- und Formatierungsparameter
Automatische Injektion von Attributobjekten
Automatische Transaktionen
Verwendung:
ArticleController-Controller.
<?php namespace app\index\controller; use app\index\validate\Article\SaveValidate; use app\common\model\ArticleModel; // 引入對(duì)應(yīng)的注解 use Fairy\Annotation\Autowire; use Fairy\Annotation\RequestParam; use Fairy\Annotation\Validator; use think\Request; class ArticleController { /** * 屬性對(duì)象注入 * @Autowire() * @var ArticleModel */ public $articleModel; /** * 數(shù)據(jù)驗(yàn)證 * clsss: thinkphp定義的驗(yàn)證器類名(必填) string類型 * scene: 驗(yàn)證場(chǎng)景名 string類型 * batch:是否批量驗(yàn)證 bool類型 * throw: 驗(yàn)證失敗是否拋出異常 bool類型 * @Validator( * class="SaveValidate"::class, * scene="save", * batch=false, * throw=false * ) * * 獲取參數(shù) * fields: 定義要獲取的字段名,可批量設(shè)置默認(rèn)值 array類型 * mapping: 轉(zhuǎn)換前臺(tái)傳遞的字段名為自定義的字段名 array類型 * method: 獲取參數(shù)的方法,支持get、post、put、delte string類型 * json: 格式化json字段的數(shù)據(jù) array類型 * * json使用示例: * json:{field1,field2,...fieldn} * 表示格式化field1,field2,...,字段的json數(shù)據(jù) * * 支持json一維和二維字段的涮選,如 * json:{field1:{childField1,childField2...}} * 表示格式化field1字段的json數(shù)據(jù),并只獲取field1字段下的childField1和childField2下標(biāo)的值(支持深度一維和二維,會(huì)自動(dòng)識(shí)別) * * @RequestParam( * fields={"title","image_url","content","category_id","is_temporary","extra":"默認(rèn)值"}, * json={"category_id"}, * mapping={"image_url":"img_url"}, * method="post" * ) */ public function save(Request $request) { //獲取過濾過后的參數(shù) $postData = $request->requestParam; return MyToolkit::success($this->articleModel->store($postData)); } }
AaaModel model
<?php namespace app\common\model; use Fairy\Annotation\Autowire; use Fairy\Annotation\Transaction; use Fairy\ModelAnnotationScaner; use think\Db; use think\Model; class AaaModel extends Model { // 模型中使用Autowire注解的trait use ModelAnnotationScaner; /** * @Autowire() * @var AccountModel */ public $accountModel; /** * 注解控制事務(wù) * 返回值等價(jià)于true commit 否則 rollback * @Transaction() */ public function store() { return Db::name('aaa')->insert(['id' => 14, 'username' => 'bob']) > 0; } }