项目作者: iriven

项目描述 :
一个功能强大的PHP多驱动程序数据库使用PDO的抽象和访问层
高级语言: PHP
项目地址: git://github.com/iriven/PhpDbal.git
创建时间: 2017-05-11T07:59:17Z
项目社区:https://github.com/iriven/PhpDbal

开源协议:GNU General Public License v3.0

下载


Iriven PhpDbal

Donate

A powerful PHP multi driver database Abstraction and Access Layer using PDO

Requirements

  • php_pdo
  1. /*
  2. * NOTE: To use this class, the PDO extension must be installed and active on your web server.
  3. * The same is true of the PDO modules of each driver you used in your project (eg pdo_mysql for MySQL)
  4. */

Dependencies

Usage:

Installation And Initialisation

To utilize PhpDbal, first import and require PhpDBAL.php file in your project.

Installation
  1. require_once 'PhpDBAL.php';
Example Configuration File (setting.php)

The configuration file must adhere to the psr-4 standard:

  1. return array (
  2. 'default' =>
  3. array (
  4. 'driver' => 'mysql',
  5. 'host' => 'localhost1',
  6. 'dbname' => 'mydatabase1',
  7. 'user' => 'myusername1',
  8. 'password' => 'mypassword',
  9. 'prefix'=>'DB1_',
  10. 'port' => 3306,
  11. 'persistent' => 1,
  12. 'fetchmode' => 'object',
  13. 'prepare' => 1
  14. ),
  15. 'project2' =>
  16. array (
  17. 'driver' => 'pgsql',
  18. 'host' => 'localhost2',
  19. 'dbname' => 'mydatabase2',
  20. 'user' => 'myusername2',
  21. 'password' => 'mypassword',
  22. 'port' => 5432
  23. )
  24. );
Initialisation
  1. $DBInstance = new Iriven\PhpDBAL(
  2. new Iriven\Libs\DatabaseConfiguration($PoolName='default')
  3. ); //Initialisation
  4. /*
  5. * NOTE: to load a different database instance, just change the "poolname" value.
  6. * According to the setting.php file content, you can set the poolname value to "project2",
  7. * in order to connect to the second database.
  8. */

QueryBuilder Usage

- Basic Usage:

List all active members informations in the ‘users’ database table:

  1. $Members = $DBInstance->QueryBuilder()
  2. ->select()
  3. ->from('users','u')
  4. ->where('u.isactive = :active')
  5. ->andWhere('u.isbanned = :banned')
  6. ->setParameters([':active'=>1,':banned'=>0])
  7. ->execute();
  8. if(!$Members)
  9. echo 'No active member found';
  10. else
  11. print_r($Members);

Insert a new User or Update user data:

  1. $data = [
  2. 'id'=>null,
  3. 'username'=>null,
  4. 'email'=>null,
  5. 'password'=>null,
  6. 'activationKey'=>null,
  7. 'banned'=>null,
  8. 'active'=>null
  9. ];
  10. extract($data);
  11. $values = [];
  12. $params = [];
  13. if(!empty($username)) { $values['u.usersusername'] = ':pseudo'; $params[':pseudo'] = $username;}
  14. if(!empty($email)) { $values['u.usersemail'] = ':email'; $params[':email'] = $email;}
  15. if(!empty($password)) { $values['u.userspassword'] = ':passwd'; $params[':passwd'] = $password;}
  16. if(!empty($token)) { $values['u.usersactivationkey'] = ':token'; $params[':token'] = $token;}
  17. if(!empty($banned)) { $values['u.usersisactive'] = ':banned'; $params[':banned'] = $banned;}
  18. if(!empty($active)) { $values['u.usersisactive'] = ':active'; $params[':active'] = $active;}
  19. if(!empty($id) and $id>=1)
  20. {
  21. $QueryBuilder = $this->QueryBuilder()
  22. ->update('users','u')
  23. ->where('u.idusers = :uid')
  24. ->setParameter(':uid',$id);
  25. if($values)
  26. $QueryBuilder->set($values)->setParameters($params);
  27. }
  28. else
  29. {
  30. $QueryBuilder = $this->QueryBuilder()
  31. ->insert('users u')
  32. ->values($values)
  33. ->setParameters($params);
  34. }
  35. $uid = $QueryBuilder->execute();
- Advanced Usage:

retrieve user login informations. here a user can login using a couple of (username + password) or (email + password)

  1. $data = [
  2. 'uid'=>null,
  3. 'username'=>null,
  4. 'email'=>null,
  5. 'banned'=>false,
  6. 'active'=>1
  7. ];
  8. extract($data);
  9. $clause = [];
  10. $params = [];
  11. $User = $DBInstance->QueryBuilder()
  12. ->select('u.id AS id, u.username AS username, u.password AS password, u.email AS email, u.isactive AS active, u.isbanned AS banned')
  13. ->from('users','u');
  14. if(!empty($username))
  15. {
  16. $expr = $User->expr();
  17. $clause[] = $expr->orX($expr->eq('username',':pseudo'),$expr->eq('email',':pseudo'));
  18. $params[':pseudo'] = $username;
  19. }
  20. if(!empty($uid)) { $clause[] = 'id = :uid'; $params[':uid'] = $uid;}
  21. if(!empty($banned)) { $clause[] = 'banned= :banned'; $params[':banned'] = $banned;}
  22. if(!empty($active)) { $clause[] = 'active = :active'; $params[':active'] = $active;}
  23. if($clause)
  24. $User->where(implode(' AND ',$clause))->setParameters($params);
  25. if(!$User->execute())
  26. echo 'User not found';
  27. else
  28. print_r($User);

Compatibility:

This project handles most of the well-known database vendors including:

  • MySQL
  • SQLite
  • PgSQL
  • Oracle
  • SQLServer
  • SQLsrv
  • MsSQL
  • DB2
  • IBM
  • ODCB
  • Sysbase

Authors

License

This project is licensed under the GNU General Public License V3 - see the LICENSE file for details

Donation

If this project help you reduce time to develop, you can give me a cup of coffee :)

Donate

Disclaimer

If you use this library in your project please add a backlink to this page by this code.

  1. <a href="https://github.com/iriven/PhpDbal" target="_blank">This Project Uses Alfred's TCHONDJO PHPDbal Library.</a>

Issues Repport

Repport issues Here