SimplePHP
v1.9
OBS:該包已被 MinasORM 包取代。
開發一個小型 CRUD 項目,旨在協助日常工作並加快安全性和透明度與資料庫通訊的過程。
下載和安裝軟體包的一些注意事項和逐步說明。
為了能夠使用SimplePHP您需要具備:
PHP ^7.2.5
EXT-PDO *
SimplePHP可以透過composer.json或透過命令終端安裝:
composer require nicollassilva/ SimplePHP
或composer.json :
"nicollassilva/ SimplePHP ": "^1.9"
>>>>>>> Stashed changes
若要設定與資料庫的連接,您必須存取: SourceRootConfig.php 。要查找的文件範例:
protected $ config = [
" driver " => " mysql " ,
" hostname " => " localhost " ,
" charset " => " utf8mb4 " ,
" port " => 3306 ,
" username " => " root " ,
" password " => "" ,
" database " => "" ,
" timezone " => " America/Sao_Paulo " ,
" pathLog " => __DIR__ . " /../../../../../your-log.log " ,
" options " => [
PDO :: ATTR_ERRMODE => PDO :: ERRMODE_EXCEPTION ,
PDO :: ATTR_CASE => PDO :: CASE_NATURAL ,
PDO :: ATTR_ORACLE_NULLS => PDO :: NULL_EMPTY_STRING
]
];
完成資料庫配置後,在專案的根目錄中建立一個資料夾,您的模型將在其中建立並建立類別。
namespace Models ;
use SimplePHP Model SimplePHP ;
class User extends SimplePHP {
function __construct ()
{
/**
* @param string Table Name
* @param string Primary Key
*/
parent :: __construct ( ' users ' , ' id ' );
}
}
完成前面的所有步驟後,在專案的根目錄建立一個 index.php ,在 Composer 自動載入和模型類別中給出一個 require ,之後,實例化您的模型,您就可以開始使用SimplePHP了。下面是一個例子:
require " vendor/autoload.php " ;
require " models/user.php " ;
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> find ()-> execute ();
致命錯誤:未捕獲錯誤:未找到類別“ SimplePHP Model SimplePHP ”
要修復它,請在專案的根資料夾中執行以下命令:
composer dump -o
use Models User ;
$ userModel = new User ();
/** find all users */
$ user = $ userModel -> find ()-> execute ();
/** find user by id */
$ user = $ userModel -> find ( 5 )-> execute ();
/** find users and return the total result count */
$ count = $ userModel -> count ()-> execute ();
/** find user with one where */
$ user = $ userModel -> where ([
[ ' name ' , ' = ' , ' Nicollas ' ]
])-> execute ();
/** find user with several where. Conditional AND default */
$ user = $ userModel -> where ([
[ ' name ' , ' = ' , ' John ' ],
[ ' email ' , ' = ' , ' [email protected] ' ]
])-> execute ();
/** find user with LIKE. Conditional AND default */
$ user = $ userModel -> where ([
[ ' name ' , ' LIKE ' , ' %Guilherme% ' ],
[ ' email ' , ' = ' , ' [email protected] ' ]
])-> execute ();
/** find user with conditional where. Condicional OR */
$ user = $ userModel -> where ([
[ ' name ' , ' LIKE ' , ' %Nicollas% ' ],
[ ' name ' , ' LIKE ' , ' %Nicolas% ' ]
], ' OR ' )-> execute ();
/** find users by name = Nicollas OR name = Nicolas */
$ user = $ userModel -> where ([
[ ' name ' , ' = ' , ' Nicollas ' ]
])
-> orWhere ([
[ ' name ' , ' = ' , ' Nicolas ' ]
])-> execute ();
/** find user using the basic structure query */
$ user = $ userModel -> whereRaw ( " name = 'Nicollas' " )-> execute ();
/** find users with limit */
$ user = $ userModel -> limit ( 5 )-> execute ();
/** find users with limit & offset */
$ user = $ userModel -> limit ( 5 )-> offset ( 5 )-> execute ();
/** find users with skip & take | the same as limit & offset */
$ user = $ userModel -> take ( 5 )-> skip ( 5 )-> execute ();
/** find users with orderBy. second parameter optional, default ASC */
$ user = $ userModel -> orderBy ( ' id ' , ' DESC ' )-> orderBy ( ' name ' )-> execute ();
/** find users and return results as attributes. EXAMPLE: $user->name instead of $user['name'] */
$ user = $ userModel -> find ()-> execute ( true );
/** find users with specific columns. */
$ user = $ userModel -> find ()-> only ([ ' name ' , ' id ' , ' email ' ])-> execute ();
/** find users creating exceptions in columns. */
$ user = $ userModel -> find ( 5 )-> except ([ ' password ' ])-> execute ();
/** search in other database table */
$ user = ( new User ())-> useTable ( ' posts ' )-> find ()-> where ([[ ' owner_id ' , ' = ' , $ user -> id ]])-> execute ();
/** debug query for possible errors | return string */
$ user = $ userModel -> whereRaw ( " uuid = 'f031b537-672f-4fba-b8a6-af45e778ad93' " )-> debugQuery ();
/** group by method */
$ user = $ userModel -> groupBy ( ' id ' );
注意: except()方法不能與execute(true)方法連結使用,只能與不含參數true 的execute() 連結使用。
注意: except()方法僅在尋找特定資訊時有效,在多維數組中它不起作用。這個問題很快就會解決。
所有方法都彼此友好,這意味著您可以進行複雜的查詢。真實例子:
/** Search for a user's posts varying between the privacy in which it was saved */
/** You can pass false as a fourth argument, the class will understand that you are trying to relate tables and will not put single quotes */
$ posts = ( new User ())-> useTable ( ' posts p, users u ' )
-> where ([[ ' u.id ' , ' = ' , $ user [ ' id ' ] ?? $ visit [ ' id ' ]],
[ ' u.id ' , ' = ' , ' p.user_id ' , false ], /** example of the fourth argument for relating tables */
[ ' p.privacity ' , ' <= ' , isset ( $ privacity ) && is_array ( $ privacity ) ? 3 : ( $ visit [ ' url ' ] != $ flag ? 1 : 4 )]])
-> only ([ ' p.* ' , ' u.name ' , ' u.url ' , ' u.i_profile ' ])
-> limit ( 10 )
-> orderBy ( ' p.id ' )
-> execute ();
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> find ( 3 )-> execute ( true );
/** @return null|bool */
if ( $ user -> destroy ()) {
echo " Success delete! " ;
}
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> find ( 5 )-> execute ( true );
$ user -> name = " Other name " ;
$ user -> email = " [email protected] " ;
/** @return null|bool */
if ( $ user -> save ()) {
echo " Success! " ;
}
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> find ( 8 )-> only ([ ' id ' , ' name ' ])-> execute ( true );
$ user -> name = " Russian Gabolev " ;
$ user -> email = " [email protected] " ;
/** This informations was not called from the database, but they exist. */
$ user -> updated_at = time ();
/** @return null|bool */
if ( $ user -> save ()) {
echo " Success! " ;
}
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> request ([
" name " => " Dr. Haylie Bahringer " ,
" email " => ' [email protected] ' ,
" password " => 123456 // Encrypt before sending to the database
])-> create ();
也可以透過直接傳遞數組來實現:
use Models User ;
$ userModel = new User ();
$ _POST = [
" name " => " Hadjei Moccab " ,
" email " => " [email protected] " ,
" password " => 123456 // Encrypt before sending to the database
];
$ user = $ userModel -> request ( $ _POST )-> create ();
use Models User ;
$ userModel = new User ();
$ user = $ userModel -> find ( 18 )-> execute ( true );
/** @return string | @param string $hash, @param Array $options */
$ user -> password = $ userModel -> hashFormat ( $ _POST [ ' password ' ]);
/** @return string | @param string $url */
$ user -> home = $ userModel -> urlFormat ( ' Dr. John,Sik!@ ' ); // dr-john-sik
/** @return bool | @param string $email */
if (! $ userModel -> validateEmail ( $ _POST [ ' email ' ])) {
echo " Invalid email! " ;
}
/** @return bool | @param string $pass */
if (! $ userModel -> validatePassword ( $ _POST [ ' password ' ])) {
echo " Invalid password! " ;
}
/** @return bool | @param string $phone */
if (! $ userModel -> validatePhone ( $ _POST [ ' telephone ' ])) {
echo " Invalid telephone! " ;
}
/** @return bool | @param int $size */
$ user -> recovery_token = $ userModel -> aleatoryToken ( 15 );
$ user -> save ()
"Pác^kà@gê Sí#mp%lePHP" -> "pac-ka-ge-si-mp-lephp"
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
[email protected]
123456 // false
QUASE123! // false
#OpA1 // false
#essaSenhaEGrande1234 // false
#OpA1? // true
Foi123! // true
(00)0000-0000
(00)00000-0000
0000-0000
00000-0000
當SimplePHP產生錯誤時,它會出現在資料夾配置中配置的目錄中,報告由monolog 套件產生。
該項目已根據 MIT 許可證獲得許可 - 有關詳細信息,請參閱 LICENSE.md 文件