tbp
1.0.0
TBP เป็นกรอบแสงที่ออกแบบมาเพื่อพัฒนาเว็บไซต์ขนาดเล็กและขนาดกลาง มุ่งเน้นไปที่องค์ประกอบที่มุ่งเน้น
ตัวเชื่อมต่อไปยัง SQLITE3 ทำงานร่วมกับข้อความที่เตรียมไว้ (บังคับ) สนับสนุนการทำธุรกรรม
ต้องใช้ PHP ext-sqlite3 ตรวจสอบ phpinfo() โดยค่าเริ่มต้นภาพ Docker tbp-web-server รองรับ SQLite
เพิ่มลงใน composer.json ของคุณ 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" ;
}