当前位置: 首页 > 工具软件 > fluentpdo > 使用案例 >

FluentPDO使用

张逸清
2023-12-01

FluentPDO简介

FluentPDO是一个快速轻松的PHP库,用于快速构建查询。 它具有智能连接构建器,可自动创建表连接。

项目地址

https://github.com/envms/fluentpdo

用法

  • 安装
    在composer.json中加入,之后更新你的依赖,composer update
	"require": {
		...
		"lichtner/fluentpdo": "dev-master"  
	}
  • 创建实例
	$dsn = 'mysql:host=localhost;dbname=test;charset=utf8';
	$username = 'username';
	$password = 'password';
	$pdo = new PDO($dsn, $username, $password);
	$fpdo = new FluentPDO($pdo);
  • API
关键字描述
from($table)设置选择的数据表
from( t a b l e , table, table,id)等同于from($table)->where(‘id’, $id)
select($columns[, …])设置选择的字段
leftJoin($joinedTable)左连接
innerJoin($joinedTable)等值连接
where($condition[, $parameters])设定条件
groupBy($columns[, …])设置分组
having($columns[, …])设置分组的条件
orderBy($columns[, …])设置排序
limit($limit)设置数据范围
offset($offset)设置数据起始位置
fetch($column = ‘’)获取一行
fetchAll($index = ‘’, $selectOnly = ‘’)获取全部的行
fetchPairs($key, $value)获取键值对
query($sql)执行sql语句
  • 使用
	// SELECT
	$query = $fpdo->from('article')->where('id', 1);
	//简化
	$query = $fpdo->from('user', 1);

	// INSERT
	$values = array('title' => 'article 1', 'content' => 'content 1');
	$query = $fpdo->insertInto('article')->values($values);
	// 简化
	$query = $fpdo->insertInto('article', $values);

	// UPDATE
	$set = array('published_at' => new FluentLiteral('NOW()'));
	$query = $fpdo->update('article')->set($set)->where('id', 1);
	// 更新一条数据时,可以由主键来控制
	$query = $fpdo->update('article', $set, 1);

	// DELETE
	$query = $fpdo->deleteFrom('article')->where('id', 1);
	// 删除一条数据时,可以由主键来控制
	$query = $fpdo->deleteFrom('article', 1);

	//以上中,INSERT、UPDATE、DELETE的语句,都要使用execute()执行!!!
	//SELECT要使用fetch()或者fetchAll()才能获取数据。
  • 进阶
	// 1. Query with select, group, having, order
	$query = $fpdo
		->from('user')
		->select(null)
		->select('type, count(id) AS type_count')
		->where('id > ?', 1)
		->groupBy('type')
		->having('type_count > ?', 1)
		->orderBy('name');
	echo $query->getQuery();
	/*
	SELECT type, count(id) AS type_count
	FROM user
	WHERE id > ?
	GROUP BY type
	HAVING type_count > ?
	ORDER BY name
	*/
	// 获取结果
	$query->fetchAll();

	// 2. join 
	$query = $fpdo->from('article')
		->select('user.name')
		->leftJoin('user ON user.id = article.user_id')
		->orderBy('article.title');
	/*
	SELECT article.*, user.name
	FROM article
		LEFT JOIN user ON user.id = article.user_id
	ORDER BY article.title
	*/

	// 3. join two tables
	$query = $fpdo->from('comment')
		->where('comment.id', 1)
		->leftJoin('user comment_author')
		->select('comment_author.name AS comment_name')
		->leftJoin('article.user AS article_author')
		->select('article_author.name AS author_name');
	/*
	SELECT 
		comment.*, comment_author.name AS comment_name, article_author.name AS author_name
	FROM comment
	LEFT JOIN user AS comment_author ON comment_author.id = comment.user_id
	LEFT JOIN article ON article.id = comment.article_id
	LEFT JOIN user AS article_author ON article_author.id = article.user_id
	WHERE comment.id = 1
	*/

	// 4. INSERT with ON DUPLICATE KEY UPDATE
	//这个语法的目的是为了解决重复性
	//当数据库中存在某个记录时,执行这条语句会更新它,而不存在这条记录时,会插入它。
	//该语法时MySQL4.1版本之后特有的语法,使得原本需要执行3条SQL语句(SELECT,INSERT,UPDATE),缩减为1条语句即可完成。
	$query = $fpdo->insertInto('article', array('id' => 1))
		->onDuplicateKeyUpdate(array(
			'title' => 'article 1b',
			'content' => new FluentLiteral('abs(-1)') // let's update with a literal and a parameter value
		));
	echo 'last_inserted_id = ' . $query->execute() . "\n";
	$q = $fpdo->from('article', 1)->fetch();
	print_r($q);

	$query = $fpdo->insertInto('article', array('id' => 1))
		->onDuplicateKeyUpdate(array(
			'title' => 'article 1',
			'content' => 'content 1',
		))->execute();
	echo 'last_inserted_id = ' . $query->execute() . "\n";
	$q = $fpdo->from('article', 1)->fetch();
	print_r($q);
	// 输出
	/*
	last_inserted_id = 1
	Array
	(
		[id] => 1
		[user_id] => 1
		[published_at] => 2011-12-10 12:10:00
		[title] => article 1b
		[content] => 1
	)
	last_inserted_id = 1
	Array
	(
		[id] => 1
		[user_id] => 1
		[published_at] => 2011-12-10 12:10:00
		[title] => article 1
		[content] => content 1
	)
	*/


 类似资料: