文件名称:PHP ORM(面向对象PDO数据库框架&API框架)
文件大小:29KB
文件格式:RAR
更新时间:2018-02-16 09:53:06
PHP ORM PDO API OOP
自主封装的PHP ORM框架,面向对象的PDO数据库操作,API框架,支持Get/Post/Put/Delete多种请求方式。 代码示例: User::$field_username, 2 => User::$field_email, 3 => User::$field_phone ); if ($type == 2 && !is_email($value) || $type == 3 && !is_mobilephone($value)) { die_error(USER_ERROR, $type_field_map[$type]['name'] . '格式无效'); } $user = new User(); $user->set_where_and($type_field_map[$type], SqlOperator::Equals, $value); $result = $user->exists(create_pdo()); echo_result($result ? 1 : 0); //存在返回1,不存在返回0 } //查询单条信息 if ($action == 2) { list($userid) = filter_request(array( request_userid())); //查询单条数据 $user = new User($userid); //set_query_fields可以指定查询字段,下面两种写法均可 //$user->set_query_fields('userid, username, email'); //$user->set_query_fields(array(User::$field_userid, User::$field_username, User::$field_email)); //还可设置where条件进行查询 //$user->set_where_and(User::$field_status, SqlOperator::Equals, 3); //$user->set_where_and(User::$field_truename, SqlOperator::IsNullOrEmpty); //$user->set_where_and(User::$field_age, SqlOperator::In, array(27, 29)); //$user->set_where_and(User::$field_regtime, SqlOperator::LessThan, '-6 month'); //创建数据库连接 $db = create_pdo(); $result = $user->load($db, $user); //也可以用Model类的静态方法 //$result = Model::load_model($db, $user, $user); if (!$result[0]) die_error(PDO_ERROR_CODE, '获取用户信息时数据库错误'); if (!$user) die_error(USER_ERROR, '不存在此用户'); echo_result($user->to_array()); } }); //适合新增数据操作 execute_request(HttpRequestMethod::Post, function() { list($username, $email) = filter_request(array( request_string('username'), request_email('email'))); //设置其他信息(假数据,仅供测试) $password = md5('888888'); $phone = '189' . str_shuffle('25688990'); $age = mt_rand(18, 30); $userInfo = array( 'password' => $password, 'phone' => $phone, 'age' => $age ); //新增数据 $user = new User(0, true); //第二个参数true表示实例化后赋初始值 $user->set_username($username); $user->set_email($email); //创建数据库连接 $db = create_pdo(); //开始事务 pdo_transaction($db, function($db) use($user, $userInfo) { $result = $user->insert($db); if (!$result[0]) throw new TransactionException(PDO_ERROR_CODE, '注册用户失败', $result); $user->reset(); // 不调用reset(),会更新所有字段 $user->set_password($userInfo['password']); $user->set_phone($userInfo['phone']); $user->set_age($userInfo['age']); $result = $user->update($db); if (!$result[0]) throw new TransactionException(PDO_ERROR_CODE, '更新用户失败', $result); }); echo_msg('注册成功'); //可以直接提交json,用下面方式接收 $newUser = request_object(); //$newUser = request_array(); }); //适合更新数据操作 execute_request(HttpRequestMethod::Put, function() { //可以直接提交json,用下面方式接收 $newUser = request_object(); //$newUser = request_array(); $user = new User(); $user->set_field_from_array($newUser); $result = $user->update($db); if (!$result[0]) die_error(USER_ERROR, '更新失败'); echo_msg('更新成功'); }); //适合删除操作 execute_request(HttpRequestMethod::Delete, function() { $userid = request_object_id(); //删除数据 $user = new User($userid); //还可设置where条件进行删除 //$user->set_where_and(User::$field_status, SqlOperator::Equals, 3); //$user->set_where_and(User::$field_truename, SqlOperator::IsNullOrEmpty); //$user->set_where_and(User::$field_age, SqlOperator::In, array(27, 29)); //$user->set_where_and(User::$field_regtime, SqlOperator::LessThan, '-6 month'); //创建数据库连接 $db = create_pdo(); $result = $user->delete($db); //也可以用Model类的静态方法 //$result = Model::delete_model($db, $user); if (!$result[0]) die_error(USER_ERROR, '删除用户数据时数据库错误'); echo_msg('删除成功'); });
【文件预览】:
PDOOP
----db()
--------pdoop.sql(4KB)
----Common()
--------response.php(6KB)
--------error-code.php(710B)
--------ExtNumeric.php(1KB)
--------util.php(9KB)
--------request.php(7KB)
--------ExtDateTime.php(2KB)
--------mysql.php(1KB)
----Models()
--------User.php(4KB)
--------Base()
----demo()
--------user-list.php(2KB)
--------delete-user.php(846B)
--------userHandler.php(5KB)
--------add-user-trans.php(1KB)
--------user-list-paged.php(1KB)
--------add-user.php(1KB)
--------update-user.php(1KB)
--------exist-user.php(835B)
--------user-info.php(1KB)
----loader-api.php(367B)
----create-model.php(5KB)
----application.php(1KB)