tbp
1.0.0
TBP是一个旨在开发中小型网站的轻型框架。专注于面向组件
连接器到SQLITE3。使用准备好的陈述(强制性)。支持交易。
需要PHP扩展ext-sqlite3 。检查phpinfo() 。默认情况下, tbp-web-server Docker图像支持SQLite。
添加到您的composer.json require
"ext-sqlite3": "*"
创建一个配置文件,例如config/my-sqlite.config
# TBP/SQLite config file.
# ALL PARAMETERS ARE OPTIONAL!
# Database file name (if not defined, will use 'default.db')
filename=resources/test.db
# Access type
# readOnly=yes
# Encryption key (if not defined, no encryption used)
# encryptionKey=
创建一个扩展AbstractSqliteDomain域并传递您的配置文件。为每个需要查询添加一种方法。
class MyDomain extends AbstractSqliteDomain
{
public function __construct ()
{
$ configReader = new Config ( __CONFIG_PATH__ ); // <-- get a config reader, passing your config folder
$ config = $ configReader -> getConfig ( ' my-sqlite ' ); // <-- read your configuration, passing your config base name
$ config -> filename = // <-- convert your relative path into absolute path
__BASE_PATH__ .
DIRECTORY_SEPARATOR .
$ config -> filename ;
parent :: __construct ( $ config );
$ this -> open (); // <-- open a db connection and let's rock!
}
public function addRegister ( $ a , $ b )
{
$ sql = " INSERT INTO `MyTableWithAutoInc` (`a`, `b`) VALUES (?, ?) " ;
$ params = array ( $ a , $ b );
return self :: insertWithAutoincrement ( $ this -> connection -> command ( $ sql , $ params ));
}
public function addAnotherRegister ( $ a , $ b )
{
$ sql = " INSERT INTO `MyOtherTable` (`a`, `b`) VALUES (?, ?) " ;
$ params = array ( $ a , $ b );
return $ this -> connection -> command ( $ sql , $ params );
}
public function getRegisters ( $ b )
{
$ sql = " SELECT * FROM `MyTableWithAutoInc` WHERE `b` <= ? " ;
$ params = array ( $ b );
return self :: getAll ( $ this -> connection -> select ( $ sql , $ params ));
}
public function getRegisterById ( $ id )
{
$ sql = " SELECT * FROM `MyTableWithAutoInc` WHERE `MyId` = ? " ;
$ params = array ( $ id );
return self :: getSingle ( $ this -> connection -> select ( $ sql , $ params ));
}
}示例直线代码:
$ domain = new MyDomain ();
$ results = $ domain -> getRegisters ( 9391 ); // get an array带有交易的示例代码:
$ domain = new MyDomain ();
try {
$ domain -> begin ();
$ lastId = $ domain -> addRegister ( 1 , 2 );
echo " first query: last inserted id: $ lastId n" ;
$ lastId = $ domain -> addRegister ( 3 , 4 );
echo " second query: last inserted id: $ lastId n" ;
$ domain -> commit ();
} catch ( Exception $ e ) {
$ domain -> rollback ();
echo " ERROR: " . $ e -> getMessage () . "n" ;
}