2017 © Pedro Peláez
 

library php-crud

This class provides a common method of performing simple CRUD operations on any database supported by PHP PDO

image

mehdirochdi/php-crud

This class provides a common method of performing simple CRUD operations on any database supported by PHP PDO

  • Friday, February 19, 2016
  • by mehdirochdi
  • Repository
  • 1 Watchers
  • 3 Stars
  • 13 Installations
  • PHP
  • 0 Dependents
  • 0 Suggesters
  • 1 Forks
  • 0 Open issues
  • 1 Versions
  • 18 % Grown

The README.md

PHP CRUD PDO-V2

PHP CRUD Lib 2.0 - 9 April 2015, (*1)

By Mehdi Rochdi, (*2)

PHP Class/MySQL use, create, all, update and delete and other functions. It uses PDO driver it's capable to interacting with your mysql database, with easy methods inspire since framworks (cakePHP). you can integrate into you OOP architecture, (*3)

### Installation

Clone the repository, (*4)

git clone https://github.com/mehdirochdi/php-crud-V2.git

Download composer:, (*5)

curl -sS https://getcomposer.org/installer | php

Install vendors:, (*6)

php composer.phar install

### How Using The Class

You will need to change some variable in config.php, for your own Database local and a distance, (*7)

"db_host" => "localhost", // change as required
"db_user" => "username",  // change as required
"db_pass" => "password",  // change as required
"db_name" => "database_name", // change as required

Test Mysql, (*8)

Start by creating test table in your Database, (*9)

CREATE TABLE IF NOT EXISTS `authors` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `name` varchar(255) NOT NULL,
  `emails` varchar(255) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

INSERT INTO posts VALUES('', 'Name 1', 'name 1@email.com');
INSERT INTO posts VALUES('', 'Name 2', 'name 2@email.com');
INSERT INTO posts VALUES('', 'Name 3', 'name 3@email.com');
INSERT INTO posts VALUES('', 'Name', 'name 4@email.com');

### Examples

Insert exemple
table = 'authors'; // Table name

$response = $db->create([
    
    'name' => 'Name 5',
    'email' => 'name 5@email.com',
]);

echo 'ID : '.$db->lastInsertId(); // Last insert ID
var_dump($response);
?>
Select Data with a function read(bool $tinyint, int $numberOfPage, array $order)
table = 'authors'; // Table name
$response = $db->all();
var_dump($response);

?>
Use function all() with only option (order)
table = 'authors'; // Table name
$response = $db->all([
    'order' => ['id' => 'DESC']
]);
var_dump($response);

?>
Use function all() for Pagination exemple
table = 'authors'; // Table name
$response = $db->read(true,4, [
        'order' => ['id' => 'DESC']
]);
echo 'The count of row for each page is : '.$db->rowCount().'
'; echo 'The total count of rows is : '.$db->countStatement; var_dump($response); // Display Pagination for($i=1; $i_paginate_number; $i++){ if($i == $db->_paginate_currentPage){ echo ' / '.$i; }else{ echo ' / '.$i.''; } } ?>
For more possibilities you can use function find() or findById(int $id, string $fetch_mode)
you can choice your favorite fetch mode ('num', 'both', 'assoc', 'obj')
table = 'authors'; // Table name
$response->$db->findById(1, 'obj'); // num || both || assoc || obj 
var_dump($response);

?>
for advanced requirements, use
find(string $genre, array $params, array $attribute, string fetch_mode, int $numberOfPage)
table = 'authors'; // Table name
$response->$db->find('all', [
'fields'     => ['name', 'emails'],
'conditions' => ['id' => '?'],
'order' => ['id' => 'DESC']
], ['2']);
var_dump($response);

?>
Joins exemple with function find()
start by Another table in your Database
CREATE TABLE IF NOT EXISTS `posts` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `author_id` int(11) NOT NULL,
  `title` varchar(255) NOT NULL,
  `description` text NOT NULL,
  `is_actived` tinyint(1) NOT NULL DEFAULT '1',
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 1');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 2');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 3');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 4');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 5');
INSERT INTO posts VALUES('', 2, 'Post name 1', 'my description for post 6');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 7');
INSERT INTO posts VALUES('', 1, 'Post name 1', 'my description for post 8');

after use the function find() to select rows using a join in the database
find('all', [
        'table'      => ['posts' => 'pos'],
        'fields'     => ['pos.id', 'pos.title','pos.description', 'auth.name '],
        'joins'      => [
                        'tables'    => ['authors'],
                        'alias'     => ['auth'],
                        'type'      => ['LEFT'],
                        'condition' => ['auth.id' => 'pos.author_id']
        ],
        'conditions' => ['author_id' => '?'],
        'order' => ['pos.id' => 'DESC']
    ], ['1']
);
echo 'The count of row for each page is : '.$db->rowCount();
var_dump($response);

?>
Pagination exemple with Find();
find('pagination', [
        'table'      => ['posts' => 'pos'],
        'fields'     => ['pos.id', 'pos.title','pos.description', 'auth.name '],
        'joins'      => [
                        'tables'    => ['authors'],
                        'alias'     => ['auth'],
                        'type'      => ['LEFT'],
                        'condition' => ['auth.id' => 'pos.author_id']
        ],
        'order' => ['pos.id' => 'DESC']
    ]
);

echo 'The count of row for each page is : '.$db->rowCount().'
'; echo 'The total count of rows is : '.$db->countStatement; var_dump($response); // Display Pagination for($i=1; $i_paginate_number; $i++){ if($i == $db->_paginate_currentPage){ echo ' / '.$i; }else{ echo ' / '.$i.''; } } ?>
Update exemple
table = 'authors'; // Table name

$response = $db->update([
        'fields' => [
            'name' => 'My Name four',
        ],
        'conditions' => ['id' => '?']
], [4]);
var_dump($response);
?>
Delete exemple
table = 'authors'; // Table name

$response = $db->deleteById(5);
var_dump($response);
?>

Delete with conditions, (*10)

table = 'authors'; // Table name

$response = $db->delete(['id' => 5]);
var_dump($response);
?>

License

The PHP CRUD is open-source licensed under the GNU license., (*11)

The Versions

19/02 2016

dev-master

9999999-dev

This class provides a common method of performing simple CRUD operations on any database supported by PHP PDO

  Sources   Download

GNU

by Mehdi Rochdi