thinkphp5Ôõô¾ÙÐÐÊý¾ÝµÄɾ³ý²Ù×÷
ThinkPHP 5.0 ÊÇÒ»¿îÇáÁ¿¼¶µÄ¿ªÔ´ PHP ¿ò¼Ü£¬Ëü»ùÓÚÃæÏò¹¤¾ßµÄ±à³ÌÍ·ÄÔºÍ MVC Éè¼Æģʽ£¬ÌṩÁ˼òÆÓÒ×ÓõĿª·¢¹¤¾ßºÍ¸ßЧÓÅÑŵĴúÂë½â¾ö¼Æ»®¡£ÔÚ¿ª·¢Àú³ÌÖУ¬Ê¹Óà ThinkPHP 5.0 ¾³£ÐèÒª¶ÔÊý¾Ý¿â¾ÙÐÐÔöÌí¡¢É¾³ý¡¢Ð޸ĺÍÅÌÎÊÊý¾ÝµÈ²Ù×÷¡£
»ù±¾Óï·¨
ÔÚ ThinkPHP 5.0 ÖУ¬É¾³ýÊý¾ÝµÄ»ù±¾Ó﷨Ϊ£º
Db::name('table')->where('condition')->delete();
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬Db::name(‘table’) Ö¸¶¨Òª²Ù×÷µÄÊý¾Ý±íÃû£¬where(‘condition’) Ö¸¶¨É¾³ýÊý¾ÝµÄÌõ¼þ£¬delete() ÌåÏÖÖ´ÐÐɾ³ý²Ù×÷¡£
Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡±£»
ɾ³ýÖ¸¶¨ ID µÄÊý¾Ý
ÈôÊÇҪɾ³ýÊý¾Ý¿âÖÐÖ¸¶¨ ID µÄÊý¾Ý£¬¿ÉÒÔƾ֤ÈçÏ·½·¨¾ÙÐвÙ×÷£º
Db::name('table')->delete(1);
µÇ¼ºó¸´ÖÆ
ÆäÖУ¬1 ÌåÏÖҪɾ³ýµÄÊý¾ÝµÄ ID ºÅ¡£
ɾ³ýÇкÏÌõ¼þµÄÊý¾Ý
ÈôÊÇҪɾ³ýÇкÏÌõ¼þµÄ¶àÌõÊý¾Ý£¬Ôò¿ÉÒÔʹÓà where Ìõ¼þ¹¹½¨É¾³ýÓï¾ä¡£ÀýÈ磬Ҫɾ³ýÊý¾Ý¿âÖÐËùÓÐ status ֵΪ 0 µÄÊý¾Ý£¬¿ÉÒÔÕâÑùд£º
Db::name('table')->where('status', 0)->delete();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÓï¾ä½«É¾³ýÊý¾Ý±í table ÖÐËùÓÐ status ֵΪ 0 µÄÊý¾Ý¡£
ɾ³ý¶àÌõÊý¾Ý
ÔÚ ThinkPHP 5.0 ÖУ¬¿ÉÒÔͬʱɾ³ý¶àÌõÇкÏÖ¸¶¨Ìõ¼þµÄÊý¾Ý¡£ÀýÈ磬Ҫɾ³ý status ֵΪ 0 ²¢ÇÒ score ֵСÓÚ 60 µÄÊý¾Ý£¬¿ÉÒÔÕâÑùд£º
Db::name('table')->where('status', 0)->where('score', '<', 60)->delete();
µÇ¼ºó¸´ÖÆ
ÒÔÉÏÓï¾ä½«É¾³ýÊý¾Ý±í table ÖÐËùÓÐ status ֵΪ 0 ²¢ÇÒ score ֵСÓÚ 60 µÄÊý¾Ý¡£
ɾ³ýËùÓÐÊý¾Ý
ÈôÊÇҪɾ³ýÊý¾Ý±íÖеÄËùÓÐÊý¾Ý£¬¿ÉÒÔʹÓÃÒÔÏ SQL Óï¾ä£º
truncate table table;
µÇ¼ºó¸´ÖÆ
ÔÚ ThinkPHP 5.0 ÖУ¬Ò²¿ÉÒÔʹÓÃÒÔÏ´úÂë¾ÙÐвÙ×÷£º
Db::name('table')->truncate();
µÇ¼ºó¸´ÖÆ
×¢ÖØ£ºtruncate() ÒªÁì²¢²»Ö§³Ö´øÓÐÌõ¼þµÄɾ³ý²Ù×÷£¬½ö½öÓÃÓÚɾ³ýÊý¾Ý±íÖеÄËùÓÐÊý¾Ý¡£
ɾ³ýÏà¹Ø²Ù×÷
ÔÚɾ³ýÊý¾Ýʱ£¬ÎÒÃǾ³£ÐèҪ˼Á¿Ò»Ð©Ïà¹ØµÄ²Ù×÷£¬ÀýÈ磬ͬʱɾ³ý¹ØÁª±íÖеÄÊý¾Ý£¬»òÕßɾ³ýÏà¹ØµÄÎļþµÈ¡£ÔÚ ThinkPHP 5.0 ÖУ¬¿ÉÒÔʹÓù³×Óº¯Êý after_delete() À´¾ÙÐÐÏà¹Ø²Ù×÷¡£ÀýÈ磬ɾ³ýÊý¾Ýʱɾ³ý¶ÔÓ¦µÄ´ÅÅÌÎļþ£¬¿ÉÒÔÕâÑùд£º
use think\Model; class User extends Model { protected static function afterDelete($user) { // ɾ³ý´ÅÅÌÎļþ unlink('/path/to/file/' . $user['avatar']); } }
µÇ¼ºó¸´ÖÆ
ÒÔÉÏ´úÂëÌåÏÖÔÚɾ³ýÊý¾Ýʱ£¬»á´¥·¢ after_delete() ¹³×Óº¯Êý£¬²¢´«È뱻ɾ³ýµÄÊý¾Ý¡£
ÒÔÉϾÍÊÇthinkphp5Ôõô¾ÙÐÐÊý¾ÝµÄɾ³ý²Ù×÷µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡