yohancayres / crud-advanced
此类是一个具有高级方法的Crud。提供开发中的敏捷性和生产力。编辑
v1.0
2017-04-29 13:45 UTC
Requires
- php: >=5.3.3
This package is not auto-updated.
Last update: 2024-09-25 01:04:26 UTC
README
此类是一个具有高级方法的Crud。提供开发中的敏捷性和生产力。
所有方法和它们的描述将列在下面。所有的Crud.class.php类都已文档化。
安装
手动安装
您可以手动安装CrudAdanved
require_once "src/CrudAdvanced/DataBase.php"; require_once "src/CrudAdvanced/Val.php"; require_once "src/CrudAdvanced/Crud.php"; use CrudAdvanced\DataBase; use CrudAdvanced\Val; use CrudAdvanced\Crud;
使用Composer安装
您可以使用Composer命令安装CrudAdanved
composer require yohancayres/crud-advanced
或添加到composer.json文件中
{ "require": { "yohancayres/crud-advanced": "dev-master" } }
可用方法
通用方法
- _set($key, $value) - 设置或更改属性的值;
- _get($key) - 获取属性的值;
- loadData(array $data) - 从数组加载数据到对象属性;
- requiredParam(array $params) - 检查是否存在所需的属性;
- getParamArray(array $params) - 获取对象属性的数组;
数据库方法
- dbInsert() - 将对象的属性插入数据库;
- dbUpdateAll() - 刷新数据库中的所有属性;
- dbUpdateAtts($atts) - 更新数据库中的特定属性;
- dbUpdate($attr, $newvalue) - 定义并更新数据库中属性的值;
- dbUpdateIncrease($attr, $amount) - 通过增加值来更新属性(此值可以是负数);
- dbSearch($attr, $data) - 搜索数据库;
- dbRemove() - 从数据库中删除一条记录;
- dbCheckExists($attr) - 检查记录是否已存在;
- dbLoadData($values) - 从数据库加载所有数据;
- dbLoadDataBy($attr, $values) - 使用第一个参数定义的属性加载所有数据库数据;
- fetchById($id, $values) - 获取数据库记录;
- fetchRandom($values) - 获取数据库的随机记录;
- fetchAll($where, $loadAtts) - 从表中获取所有记录;
关系
- hasOne($className, $thisAttName, $classAttName, $loadAtts)- 创建一对一关系;
- hasMany($className, $thisAttName, $classAttName, $loadAtts) - 创建一对多关系;
示例
连接到数据库
首先,应用程序需要信息来连接到数据库
use CrudAdvanced\DataBase; DataBase::configure('127.0.0.1', 'root', 'password', 'dbteste');
使用Crud
要创建一个类并使用Crud方法,您需要将Crud扩展到您的类中。
use CrudAdvanced\Val; use CrudAdvanced\Crud; class User extends Crud { /* Database table definition */ public $table = "users"; // Define Table name public $tableCols = ['name', 'email', 'password']; // Define table cols /* * Other class methods and attributes can be defined freely. */ }
使用dbInsert()插入新用户
此方法将所有属性插入到数据库中。请注意属性名称,它们必须与数据库中的字段完全相同。
$user = new User([ 'name' => 'Yohan Cayres', 'email' => 'yohan.cayres@mail.com', 'password' => md5('somepassword') ]); // Create new object type User $user->dbInsert(); // Insert at the Database
dbLoadDataBy()
此方法将加载数据库中的属性,并搜索定义的属性。
$user->dbLoadDataBy('email', 'id,name,password'); // Loads a database attribute by email. echo $user->_get('id');
使用dbUpdateAll()或dbUpdateAtts()更新某些行
此方法需要id属性,您可以通过dbLoadDataBy()获取它;
$user->_set('email', 'newMail@mail.com'); // This will set a new email $user->dbUpdateAtts('email'); // Only the attribute 'email' will be updated $user->dbUpdateAll(); // All the attributes will be updated
直接更新
此方法需要id属性,您可以通过dbLoadDataBy()获取它;
$user->dbUpdate('email', 'newMail@mail.com'); // Set and update directly.