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" ;
}