fractalizer / php-sweet-pdo
小巧、简单且方便的PDO包装器
1.0.1
2015-06-24 19:52 UTC
Requires
- ext-pdo: *
This package is not auto-updated.
Last update: 2024-09-28 16:09:14 UTC
README
phpSweetPDO是一个php PDO包装器,它
- 极其简单易用
- Phar包以简化部署。您只需要一个9Kb的文件即可与库一起工作(压缩的phar文件)
- 紧凑(如400行注释代码,您甚至可以进一步压缩)
- 可选支持事件,允许轻松分析和调试
使用示例
<?php require_once('phpsweetpdo.phar'); //Connecting $connection = new \phpSweetPDO\Connection('mysql:dbname=test;host=127.0.0.1', 'root', 'password'); //Executing DDL $connection->execute("DROP TABLE IF EXISTS `phpsweetpdo`"); //Selecting only one value $value = $connection->getOneValue("SELECT field2 FROM phpsweetpdo WHERE id=? AND field2 <> ?", array(1, 300)); echo $value; //Selecing only one row $record = $connection->getOneRow("SELECT * FROM phpsweetpdo WHERE id=:id1 AND field2<>:id2", array('id1' => 1, 'id2' => 300)); echo $record->field1 . $record->field2; //Will throw exception if fields do not exist in a row //Selecting more than 1 row $recordset = $connection->select("SELECT * FROM phpsweetpdo ORDER BY field1 ASC"); foreach ($recordset as $currentRow) { echo $currentRow->id; //Will throw exception if field id does not exist in recordset } //Output parameters of stored procedures $connection->execute("CALL phpsweetpdo_out(@test)"); $result = $connection->getOneValue("SELECT @test"); //INSERT and UPDATE build helpers use phpSweetPDO\SQLHelpers\Basic as Helpers; $sql = Helpers::insert('mytable', array('field_name' => 'field_value')); $connection->execute($sql); // INSERT INTO mytable (field_name) VALUES (:field_name); //:field_name = 'field_value' $sql = Helpers::update('mytable', array('field_name' => 'field_value'), "field_2=13"); $connection->execute($sql); // UPDATE test SET field_name=:field_name WHERE field_2='13'; //:field_name = 'field_value'
事件
如果您将类型为Symfony\Component\EventDispatcher\EventDispatcher
的对象传递给Connection类的构造函数,它将在其操作上触发事件。
您可以在此处了解有关事件派发器的更多信息:http://components.symfony-project.org/event-dispatcher/documentation。
以下事件可以被跟踪
- phpsweetpdo.connect.started / phpsweetpdo.connect.finished
- phpsweetpdo.execute.started / phpsweetpdo.execute.finished
- phpsweetpdo.select.started / phpsweetpdo.select.finished
- phpsweetpdo.get_one_value.started / phpsweetpdo.get_one_value.finished
- phpsweetpdo.get_one_row.started / phpsweetpdo.get_one_row.finished
- phpsweetpdo.begin_transaction.started / phpsweetpdo.begin_transaction.finished
- phpsweetpdo.commit_transaction.started / phpsweetpdo.commit_transaction.finished
- phpsweetpdo.rollback_transaction.started / phpsweetpdo.rollback_transaction.finished
大多数事件都伴随着参数。它们主要是sql(正在执行的SQL查询),params(传递给查询的参数),以及driver_options - 如果有的话,使用的驱动器选项。
<?php function onEvent(\phpSweetPDO\Events\DbEvent $event) { echo $event->getName(); $params = $event->getParameters(); echo 'SQL query is ' . $params['sql']; } $eventDispatcher = new \Symfony\Component\EventDispatcher\EventDispatcher(); $eventDispatcher->addListener('phpsweetpdo.select.started', 'onEvent'); $eventDispatcher->addListener('phpsweetpdo.select.finished', 'onEvent'); $connection = new \phpSweetPDO\Connection('mysql:dbname=test;host=127.0.0.1', 'root', '', $eventDispatcher); $connection->select("SELECT * FROM phpsweetpdo ORDER BY field1 ASC"); //At this point our onEvent() function will be called twice with respected events and will print the query, //we tried to execute.
限制
phpSweetPDO不支持(至少目前不支持,可以发送pull requests)
- 类型化的PDO参数。所有参数都按原样传递给PDOStatement->execute()(例如,MySQL会自行处理类型转换。这很可能适用于许多其他数据库引擎)。
- 显式输出参数。您不能在phpSweetPDO调用中将参数标记为输出。但您可以使用上述示例部分中描述的方法。
要求
- PHP 5.3或更高版本(您可以使用较低版本,但代码将需要清理命名空间)。