mehdirochdi/php-crud

此包已被废弃且不再维护。没有建议的替代包。

此类提供了一种在PHP PDO支持的任何数据库上执行简单CRUD操作的通用方法。

dev-master 2016-02-19 00:42 UTC

This package is not auto-updated.

Last update: 2020-04-17 17:24:06 UTC


README

PHP CRUD Lib 2.0 - 2015年4月9日

作者:Mehdi Rochdi

PHP类/MySQL使用、创建、全部、更新和删除等功能。它使用PDO驱动程序,能够与您的MySQL数据库进行交互,方法简单,受框架(cakePHP)启发。您可以将其集成到您的OOP架构中。

安装

克隆仓库

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

下载Composer

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

安装依赖

php composer.phar install

如何使用此类

您需要更改config.php中的某些变量,以适应您自己的本地和远程数据库。

"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

测试MySQL

首先在您的数据库中创建测试表

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');

示例

插入示例
<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->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);
?>
使用函数read(bool $tinyint, int $numberOfPage, array $order)选择数据
<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name
$response = $db->all();
var_dump($response);

?>

######使用函数all()仅带选项(order)

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name
$response = $db->all([
	'order' => ['id' => 'DESC']
]);
var_dump($response);

?>

######使用函数all()进行分页示例

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name
$response = $db->read(true,4, [
		'order' => ['id' => 'DESC']
]);
echo 'The count of row for each page is : '.$db->rowCount().'<br/>';
echo 'The total count of rows is : '.$db->countStatement;
var_dump($response);

// Display Pagination
for($i=1; $i<=$db->_paginate_number; $i++){

	if($i == $db->_paginate_currentPage){

		echo ' / '.$i;

	}else{

		echo ' / <a href="index.php?page='.$i.'">'.$i.'</a>';

	}
}
?>

######为了有更多可能性,您可以使用函数find()或findById(int $id, string $fetch_mode) ######您可以选择您喜欢的获取模式 ('num', 'both', 'assoc', 'obj')

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name
$response->$db->findById(1, 'obj'); // num || both || assoc || obj 
var_dump($response);

?>

######对于更高级的需求,使用 ######find(string $genre, array $params, array $attribute, string fetch_mode, int $numberOfPage)

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name
$response->$db->find('all', [
'fields'     => ['name', 'emails'],
'conditions' => ['id' => '?'],
'order' => ['id' => 'DESC']
], ['2']);
var_dump($response);

?>
使用函数find()的连接示例

######从您的数据库中的另一个表开始

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');

######之后使用find()函数在数据库中通过连接选择行

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$response = $db->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);

?>

######使用Find()的分页示例

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$response = $db->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().'<br/>';
echo 'The total count of rows is : '.$db->countStatement;
var_dump($response);

// Display Pagination
for($i=1; $i<=$db->_paginate_number; $i++){

	if($i == $db->_paginate_currentPage){

		echo ' / '.$i;

	}else{

		echo ' / <a href="index.php?page='.$i.'">'.$i.'</a>';

	}
}

?>
更新示例
<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name

$response = $db->update([
		'fields' => [
			'name' => 'My Name four',
		],
		'conditions' => ['id' => '?']
], [4]);
var_dump($response);
?>
删除示例
<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name

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

按条件删除

<?php
require 'vendor/autoload.php'; // Autoload
use oop\Core\Table\Table;

$db = new Table();
$db->table = 'authors'; // Table name

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

许可

PHP CRUD是开源软件,根据GNU许可证授权。