ÔÚThinkPHP6ÖÐÔõÑùʹÓÃORM¾ÙÐÐÊý¾Ý¿â²Ù×÷£¿
Ëæ×ÅphpµÄÉú³¤£¬Ô½À´Ô½¶àµÄwebÓ¦ÓóÌÐòÐèÒª»á¼ûºÍ²Ù×÷Êý¾Ý¿â¡£orm(object-relational mapping)ÊÇÒ»ÖÖÊ¢ÐеÄÊý¾Ý¿â²Ù×÷·½·¨£¬ËüÒÔÃæÏò¹¤¾ßµÄ·½·¨ÖÎÀíÊý¾Ý¿â£¬²¢ÌṩÁË¿Éά»¤¡¢¿ÉÀ©Õ¹ºÍ¿ÉÒÆÖ²µÄ½â¾ö¼Æ»®¡£thinkphp6ÊÇÒ»¸öÊ¢ÐеÄphp¿ò¼ÜÖ®Ò»£¬ËüÌṩÁËÐí¶àormµÄʹÓ÷½·¨£¬²¢ÇÒºÜÊǼòÆÓÒ׶®¡£±¾ÎĽ«ÏÈÈÝÔÚthinkphp6ÖÐÔõÑùʹÓÃorm¾ÙÐÐÊý¾Ý¿â²Ù×÷¡£
Step1. ÅþÁ¬Êý¾Ý¿â
ÔÚʹÓÃORM²Ù×÷Êý¾Ý¿â֮ǰ£¬Ê×ÏÈÐèÒªÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£ÔÚThinkPHP6ÖУ¬¿ÉÒÔÔÚconfig/database.phpÖоÙÐÐÉèÖá£ÏêϸµÄÉèÖÃÐÅÏ¢°üÀ¨Êý¾Ý¿âÀàÐÍ¡¢Êý¾Ý¿âµØµã¡¢¶Ë¿Ú¡¢Êý¾Ý¿âÃû³Æ¡¢Óû§ÃûºÍÃÜÂëµÈ¡£ÀýÈ磬ÒÔÏÂÊÇÒ»·ÝMySQLÊý¾Ý¿âµÄÉèÖãº
<?php return [ // Êý¾Ý¿âÀàÐÍ 'type' => 'mysql', // Êý¾Ý¿âÅþÁ¬DSNÉèÖà 'dsn' => '', // ЧÀÍÆ÷µØµã 'hostname' => 'localhost', // Êý¾Ý¿âÃû 'database' => 'test', // Êý¾Ý¿âÓû§Ãû 'username' => 'root', // Êý¾Ý¿âÃÜÂë 'password' => '', // Êý¾Ý¿âÅþÁ¬¶Ë¿Ú 'hostport' => '', // Êý¾Ý¿âÅþÁ¬²ÎÊý 'params' => [], // Êý¾Ý¿â±àÂëĬÈϽÓÄÉutf8 'charset' => 'utf8', // Êý¾Ý¿â±íǰ׺ 'prefix' => '', // Êý¾Ý¿âµ÷ÊÔģʽ 'debug' => true, // Êý¾Ý¿â°²ÅÅ·½·¨:0 ¼¯ÖÐʽ(¼òµ¥Ð§ÀÍÆ÷),1 ÂþÑÜʽ(Ö÷´ÓЧÀÍÆ÷) 'deploy' => 0, // Êý¾Ý¿â¶ÁдÊÇ·ñÊèÉ¢ Ö÷´ÓʽÓÐÓà 'rw_separate' => false, // ¶ÁдÊèÉ¢ºó Ö÷ЧÀÍÆ÷ÊýÄ¿ 'master_num' => 1, // Ö¸¶¨´ÓЧÀÍÆ÷ÐòºÅ 'slave_no' => '', // ÊÇ·ñÑÏ¿á¼ì²é×Ö¶ÎÊÇ·ñ±£´æ 'fields_strict' => true, // ¿ªÆô×Ô¶¯Ð´Èëʱ¼ä´Á×ֶΠ'auto_timestamp' => false, // ʱ¼ä×Ö¶ÎÈ¡³öºóµÄĬÈÏʱ¼äÃûÌà 'datetime_format' => 'Y-m-d H:i:s', // ÊÇ·ñÐèÒª¾ÙÐÐSQLÐÔÄÜÆÊÎö 'sql_explain' => false, ];
µÇ¼ºó¸´ÖÆ
Step2. ½ç˵ģ×Ó
ÔÚʹÓÃORM²Ù×÷Êý¾Ý¿âʱ£¬ÐèÒª½ç˵¶ÔÓ¦µÄÄ£×ÓÀ´ÖÎÀíÊý¾Ý¿âµÄ±í¡£ÔÚThinkPHP6ÖУ¬¿ÉÒÔʹÓÃÏÂÁîÐй¤¾ß×Ô¶¯ÌìÉúÄ£×ÓÎļþ£¬Ò²¿ÉÒÔÊÖ¶¯½¨ÉèÄ£×ÓÎļþ¡£ÒÔÏÂÊÇÒ»·ÝÊÖ¶¯½¨ÉèµÄÄ£×ÓÎļþµÄÀý×Ó£º
<?php namespace appmodel; use thinkModel; class User extends Model { protected $table = 'user'; protected $pk = 'id'; protected $autoWriteTimestamp = true; protected $createTime = 'create_time'; protected $updateTime = 'update_time'; protected $dateFormat = 'Y-m-d H:i:s'; protected $type = [ 'id' => 'integer', 'create_time' => 'datetime', 'update_time' => 'datetime', 'status' => 'boolean', ]; }
µÇ¼ºó¸´ÖÆ
ÉÏÊö´úÂëÖУ¬ÎÒÃǽç˵ÁËÒ»¸öÃûΪUserµÄÄ£×Ó£¬Ëü¶ÔÓ¦µÄÊý¾Ý¿â±íÃûΪ’user’£¬Ö÷¼ü×Ö¶ÎΪ’id’¡£Í¬Ê±£¬ÎÒÃǽç˵ÁË×Ô¶¯Ê±¼ä´ÁºÍʱ¼äÃûÌõÈÊôÐÔ¡£ÔÚÄ£×ÓÖУ¬ÎÒÃÇ¿ÉÒÔ½ç˵һЩÆäËûµÄÊôÐÔºÍÒªÁ죬ÀýÈç½ç˵ģ×Ó¹ØÁª¹ØϵµÄÒªÁìµÈ¡£ÕâЩÊôÐÔºÍÒªÁìµÄÓ÷¨½«ÔÚºóÃæµÄ°ì·¨ÖÐÏêϸÏÈÈÝ¡£
Step3. ÅÌÎÊÊý¾Ý
ÔÚʹÓÃORMÅÌÎÊÊý¾Ýʱ£¬¿ÉÒÔʹÓÃÐí¶àµÄÒªÁìÀ´Öª×ã²î±ðµÄÐèÇó¡£ÒÔÏÂÊÇһЩ½ÏÁ¿³£ÓõÄORMÅÌÎÊÒªÁ죺
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
tableÒªÁì
$result = hinkacadeDb::table('user')->where('id', $id)->find();
µÇ¼ºó¸´ÖÆ
ʹÓÃtableÒªÁì¿ÉÒÔÖ±½Ó²Ù×÷Êý¾Ý¿â±í£¬²¢¿ÉÒÔÁ´Ê½Å²ÓÃwhere¡¢order¡¢limitµÈÒªÁì¡£
findºÍselectÒªÁì
$user = User::find($id); $users = User::where('status', 1)->select();
µÇ¼ºó¸´ÖÆ
findÒªÁì¿ÉÒÔ²éÕÒÒ»ÌõÖ¸¶¨Ö÷¼üÖµµÄ¼Í¼£¬selectÒªÁì¿ÉÒÔÅÌÎÊÖª×ãÌõ¼þµÄ¶àÌõ¼Í¼¡£
valueÒªÁì
$username = User::where('id', $id)->value('username');
µÇ¼ºó¸´ÖÆ
ʹÓÃvalueÒªÁì¿ÉÒÔÅÌÎÊÖ¸¶¨×ֶεÄÖµ¡£
countÒªÁì
$count = User::count();
µÇ¼ºó¸´ÖÆ
ʹÓÃcountÒªÁì¿ÉÒÔÅÌÎÊÇкÏÌõ¼þµÄ¼Í¼Êý¡£
»òÕßÅÌÎÊ
$user = User::where('username', $username)->whereOr('email', $email)->find();
µÇ¼ºó¸´ÖÆ
»òÕßÓï¾ä¿ÉÒÔʹÓÃwhereOrÒªÁìÀ´ÊµÏÖ¡£
·ÖÒ³ÅÌÎÊ
$users = User::where('status', 1)->paginate(10);
µÇ¼ºó¸´ÖÆ
ʹÓÃpaginateÒªÁì¿ÉÒÔʵÏÖ·ÖÒ³ÅÌÎÊ£¬²¢¿ÉÒÔ×Ô½ç˵ÿҳÏÔʾµÄ¼Í¼Êý¡£
Step4. ¸üÐÂÊý¾Ý
ÔÚʹÓÃORM¸üÐÂÊý¾Ý¿â¼Í¼ʱ£¬¿ÉÒÔʹÓÃupdateÒªÁì¡£ÀýÈ磬ÒÔÏÂÊǸüÐÂÒ»Ìõ¼Í¼µÄÀý×Ó£º
$user = User::find($id); $user->username = 'new_username'; $user->save();
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔͨ¹ýfindÒªÁìÕÒµ½Ö¸¶¨Ö÷¼üÖµµÄ¼Í¼¡£È»ºó£¬ÎÒÃÇ¿ÉÒÔÐ޸Ť¾ßµÄÊôÐÔ¡£×îºó£¬ÎÒÃÇŲÓÃsaveÒªÁìÀ´½«Ð޸ĺóµÄ¼Í¼ÉúÑĵ½Êý¾Ý¿âÖС£
Step5. ɾ³ýÊý¾Ý
ÔÚʹÓÃORMɾ³ýÊý¾Ý¿â¼Í¼ʱ£¬¿ÉÒÔʹÓÃdeleteÒªÁì¡£ÀýÈ磬ÒÔÏÂÊÇɾ³ýÒ»Ìõ¼Í¼µÄÀý×Ó£º
$user = User::find($id); $user->delete();
µÇ¼ºó¸´ÖÆ
ÎÒÃÇ¿ÉÒÔͨ¹ýfindÒªÁìÕÒµ½Ö¸¶¨Ö÷¼üÖµµÄ¼Í¼¡£È»ºó£¬ÎÒÃÇŲÓÃdeleteÒªÁìÀ´½«¼Í¼´ÓÊý¾Ý¿âÖÐɾ³ý¡£
×ܽá
±¾ÎÄÏÈÈÝÁËÔÚThinkPHP6ÖÐʹÓÃORM¾ÙÐÐÊý¾Ý¿â²Ù×÷µÄ»ù±¾°ì·¨¡£Ê×ÏÈ£¬ÎÒÃÇÐèÒªÉèÖÃÊý¾Ý¿âÅþÁ¬ÐÅÏ¢¡£È»ºó£¬ÎÒÃÇÐèÒª½ç˵ģ×ÓÀ´ÖÎÀíÊý¾Ý¿âµÄ±í¡£ÔÚʹÓÃORMÅÌÎÊÊý¾Ýʱ£¬ÎÒÃÇ¿ÉÒÔʹÓÃÐí¶àµÄÒªÁìÀ´Öª×ã²î±ðµÄÐèÇó¡£ÔÚ¸üкÍɾ³ýÊý¾Ýʱ£¬ÎÒÃÇ¿ÉÒÔʹÓöÔÓ¦µÄÒªÁìÀ´ÊµÏÖ¡£Ê¹ÓÃORM¿ÉÒÔʹÎÒÃÇ´Ó·±ËöµÄSQL²Ù×÷ÖнâÍѳöÀ´£¬Ìá¸ß¿ª·¢Ð§ÂÊ¡£Ï£Íû±¾ÎÄÄܹ»×ÊÖúµ½Ä㣡
ÒÔÉϾÍÊÇÔÚThinkPHP6ÖÐÔõÑùʹÓÃORM¾ÙÐÐÊý¾Ý¿â²Ù×÷£¿µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡