Giter Site home page Giter Site logo

migliori / power-lite-pdo Goto Github PK

View Code? Open in Web Editor NEW
10.0 1.0 0.0 827 KB

PowerLite PDO is a DBAL PDO Wrapper, a lightweight and powerful PHP library that provides an efficient way to interact with multiple types of databases

Home Page: https://www.powerlitepdo.com

License: GNU General Public License v3.0

PHP 100.00%
database firebird mysql oci pdo pdo-wrapper pgsql php-library dbal

power-lite-pdo's Introduction

PowerLite PDO

Static Badge GPLv3 License GitHub Release

PowerLite PDO is a lightweight, powerful PHP library that provides a simple and efficient way to interact with databases using PHP Data Objects (PDO). It supports multiple database drivers and includes features like easy connection management, query execution, result handling and pagination.

PowerLite PDO

Table of Contents

Features

  • Containerized Connections: The containers are used to connect your database and handle the configuration and dependencies seamlessly.
  • Db & QueryBuilder Methods: Execute SQL queries and retrieve results using Db methods or the fluent QueryBuilder.
  • Efficient Result Handling: Fetch single row, all rows, or specific column's value with dedicated methods.
  • Comprehensive Database Operations: Provides a wide range of methods for diverse database interactions.
  • Pagination Support: Handle paginated results effortlessly with the Pagination class.
  • Error Management: User-friendly handling of database errors and exceptions.
  • Debug Mode: Provides detailed information about the requests for debugging purposes.
  • Prepared Statements: Support for prepared statements to prevent SQL injection attacks.
  • Transaction Control: Manage database transactions with methods to start, commit, and rollback.
  • High Code Quality Standards: The code follows best practices and coding standards.

Requirements

PHP ^7.4, PHP 8.x

Documentation

The documentation for PowerLite PDO is available on the PowerLite PDO website.

In addition to the documentation, a PHPDoc is also available here for more detailed information about the classes, methods, and their parameters.

Installation

Clone / download or install with Composer

composer require migliori/power-lite-pdo

Configuration

Open src/connection.php in your code editor and replace the constant's values with your database connection settings (DB_HOST, DB_NAME, DB_USER, DB_PASS, DB_PORT, DB_CHARSET).

Security concerns

For enhanced safety, store the file outside of your web server's document root (the directory that is served to the internet) and change the path accordingly in the configuration file (src/config.php). This prevents the file from being directly accessible via a URL.

Usage/Examples

Select records using the main Db class

  1. Include the bootstrap file and get the Db instance from the container:

    use Migliori\PowerLitePdo\Db;
    
    // Build the container and connect to the database
    $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
    $db = $container->get(Db::class);
  2. Use the select method from the Db class to select some records:

    $from = 'users'; // The table name
    $fields = ['id', 'username', 'email']; // The columns you want to select
    $where = ['status' => 'active']; // The conditions for the WHERE clause
    
    $db->select($from, $fields, $where);
  3. Fetch the selected records one by one:

    while ($record = $db->fetch()) {
        echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n";
    }

Select records using the fluent QueryBuilder

  1. Include the bootstrap file and get the QueryBuilder instance from the container:

    use Migliori\PowerLitePdo\Query\QueryBuilder;
    
    // Build the container and connect to the database
    $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
    $queryBuilder = $container->get(QueryBuilder::class);
  2. Use the QueryBuilder to select some records:

    $queryBuilder->select(['id', 'username', 'email'])->from('users')->where(['status' => 'active'])->execute();
  3. Fetch the selected records one by one:

    while ($record = $queryBuilder->fetch()) {
        echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n";
    }

Select records with Pagination

  1. Include the bootstrap file and get the Pagination instance from the container:

    use Migliori\PowerLitePdo\Pagination;
    
    // Build the container and connect to the database
    $container = require_once __DIR__ . '/vendor/migliori/power-lite-pdo/src/bootstrap.php';
    $pagination = $container->get(Pagination::class);
  2. Select some records:

    $from = 'users'; // The table name
    $fields = ['id', 'username', 'email']; // The columns you want to select
    $where = ['status' => 'active']; // The conditions for the WHERE clause
    
    $pagination->select($from, $fields, $where);
  3. Fetch the selected records one by one:

    while ($record = $pagination->fetch()) {
        echo $record->id . ', ' . $record->username . ', ' . $record->email . "\n";
    }
  4. Display the pagination:

    $url = '/users'; // The URL for the pagination links
    echo $pagination->pagine($url);

Running Tests

To run tests, run the following command

php ./vendor/bin/phpunit test

Dependencies

  • Composer: A dependency management tool for PHP.
  • PHP-DI: A dependency injection container for PHP.
  • PDO: The PHP Data Objects extension for accessing databases.
  • Database Drivers: The specific drivers for the databases you want to connect to (e.g., MySQL, PostgreSQL, Oracle, Firebird, ...).

Dev Dependencies

  • PHPUnit: A testing framework for unit testing PHP code.
  • PHPStan: A static analysis tool that helps find bugs in PHP code.
  • PHP CodeSniffer: A set of rules to ensure that PHP code follows coding standards.

Contributing

Contributions are always welcome!

Please contact us for any improvement suggestions or send your pull requests

License

GNU General Public License v3.0

power-lite-pdo's People

Contributors

migliori avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar

power-lite-pdo's Issues

Fatal Parse errors and TypeErrors throughout your code

This package appears to somewhat work similar to the old school Idiorm / Paris package except not as many features as that package.

However, there are parse errors in your default installation. No modifications have bee done to your base code and I'm trying to run your "Db Examples - PowerLite PDO" example. to take it for a spin. But, nothing runs from the get-go. No errors except the ones coming from your code base.

Running:
Localhost, with PHP 7.4.26 on Xampp

Below is the error. Are you limiting this to PHP greater than 8.0? Your documentation says otherwise and I will not be able to use any of your code base, nor will a substantial portion of the web if this is required. Many online applications will not function in 8.XX+ And, I do believe this error due to the "|" pipe is because of a requirement for 8.0+

Parse error: syntax error, unexpected '|', expecting variable (T_VARIABLE) in E:\xampp\htdocs\powerlitedb\vendor\migliori\power-lite-pdo\src\Query\QueryBuilder.php on line 50

inside here:
powerlitedb/vendor/migliori/power-lite-pdo/src/Query/QueryBuilder.php

There are further similar errors on Lines 74 and 76 if line 50 is corrected without using the pipe | symbol and using either int or array.

And, also correcting all three lines of 50, 74, 76 will yield the following TypeError
I did not explore your code further after these errors.

powerlitedb/vendor/migliori/power-lite-pdo/src/Result/Result.php on line 46.

Fatal error: Uncaught TypeError: Return value of Migliori\PowerLitePdo\Result\Result::fetch() must be an instance of Migliori\PowerLitePdo\Result\mixed, instance of stdClass returned in E:\xampp\htdocs\powerlitedb\vendor\migliori\power-lite-pdo\src\Result\Result.php:46 Stack trace: #0 E:\xampp\htdocs\powerlitedb\vendor\migliori\power-lite-pdo\src\Query\QueryBuilder.php(564): Migliori\PowerLitePdo\Result\Result->fetch(5) #1 E:\xampp\htdocs\powerlitedb\vendor\migliori\power-lite-pdo\src\Db.php(518): Migliori\PowerLitePdo\Query\QueryBuilder->fetch(5) #2 E:\xampp\htdocs\powerlitedb\db-examples-test.php(55): Migliori\PowerLitePdo\Db->fetch() #3 {main} thrown in E:\xampp\htdocs\powerlitedb\vendor\migliori\power-lite-pdo\src\Result\Result.php on line 46

Coding Error messages and scree grabs from VSCode:
vendor/migliori/power-lite-pdo/src/Query/QueryBuilder.php

line50

line_74-75

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.