×ðÁú¿­Ê±¹ÙÍøµÇ¼

thinkphp5ÔõÑù½«Îȹ̵ÄÊý¾Ý¸üÐÂΪ0

ThinkPHP 5ÊÇÒ»ÖÖ¿ªÔ´µÄPHP¿ò¼Ü£¬Äܹ»ÏÔÖøÌá¸ßWebÓ¦ÓóÌÐòµÄ¿ª·¢Ð§ÂÊ¡£ÆäÖУ¬Éæ¼°µ½Êý¾ÝµÄÅÌÎÊ¡¢²åÈë¡¢¸üС¢É¾³ýµÈ²Ù×÷ÊÇʹÓÃƵÂÊ×î¸ßµÄ²¿·ÖÖ®Ò»¡£±¾ÎĽ«Ì½ÌÖÔÚʹÓÃThinkPHP 5¿ò¼Üʱ£¬ÔõÑù½«²»ÐèÒª¸üеÄÊý¾Ý¸üÐÂΪ0¡£

ʹÓÃORM¹¤¾ß¹ØϵӳÉäÔÚThinkPHP 5ÖвÙ×÷Êý¾ÝºÜÊÇÀû±ã¡£Í¨³£ÇéÐÎÏ£¬ÎÒÃÇʹÓÃÄ£×Ó¹¤¾ßµÄsave()ÒªÁìÀ´¸üÐÂÊý¾Ý¡£µ±Ê¹ÓÃsave()ÒªÁìʱ£¬ÈôÊÇ±íµ¥ÖÐij¸ö×Ö¶ÎûÓб»Ð޸ģ¬Ôò¸Ã×ֶεÄÖµ½«²»»á±»¸üС£

ÀýÈ磬ÎÒÃÇÓµÓÐÒ»ÕÅÓû§ÐÅÏ¢±í¸ñ£¬ÆäÖк­¸ÇÁËÓû§µÄID¡¢Óû§Ãû¡¢ÃÜÂë¡¢ÐÔ±ðºÍÄêËêµÈÏêϸÐÅÏ¢¡£Óû§½öÐÞ¸ÄÁËÃÜÂëºÍÄêËê×ֶΣ¬Óû§ÃûºÍÐÔ±ð×Ö¶Îδ×÷Èκθü¸Ä¡£Ö´ÐиüвÙ×÷ʱ£¬save£¨£©ÒªÁì²»»á¸Ä±äÓû§ÃûºÍÐÔ±ð×ֶεÄÖµ¡£¿ÉÊÇ£¬ÈôÊÇÎÒÃÇÏëÒª½«Îȹ̵ÄÊý¾Ý¸üÐÂΪ0£¬ÎÒÃǸÃÔõÑù×öÄØ£¿

ÎÒÃÇ¿ÉÒÔͨ¹ýÖØдģ×Ó¹¤¾ßµÄsave()ÒªÁìÀ´ÊµÏÖÕâ¸ö¹¦Ð§¡£ÏêϸµÄʵÏÖ·½·¨ÈçÏ£º

Á¬Ã¦Ñ§Ï°¡°PHPÃâ·ÑѧϰÌõ¼Ç£¨ÉîÈ룩¡± £»

1.ÔÚÄ£×ÓÀàÖУ¬½ç˵һ¸ö¾²Ì¬Êý×é$zero_fields£¬ÓÃÓÚ´æ´¢ÐèÒª±»¸üÐÂΪ0µÄ×Ö¶ÎÊý×é¡£

protected static $zero_fields = ['field1', 'field2', ...];

µÇ¼ºó¸´ÖÆ

2.ÔÚsave()ÒªÁìÖУ¬ÅжÏÐèÒª±»¸üÐÂΪ0µÄ×Ö¶ÎÊÇ·ñÒѾ­ÓÐÖµ£¬ÈôÊÇûÓУ¬Ôò½«ÆäÖµ¸üÐÂΪ0£¬×îºóÔÙŲÓø¸ÀàµÄsave()ÒªÁì¾ÙÐÐÆäËû×ֶεĸüС£

public function save(array $data = [], $where = [], $sequence = null)
{
    foreach (self::$zero_fields as $field) {
        if (!isset($data[$field])) {
            $data[$field] = 0;
        }
    }
    return parent::save($data, $where, $sequence);
}

µÇ¼ºó¸´ÖÆ

ͨ¹ýÖØдsave()ÒªÁ죬ÎÒÃÇ¿ÉÒÔÕë¶ÔÐԵضÔÐèÒª¸üеÄ×ֶξÙÐд¦Àí£¬½«Îȹ̵Ä×ֶθüÐÂΪ0£¬×èÖ¹ÁË±íµ¥Ìá½»µÄ²»ÍêÕûÊý¾Ý¶ÔÊý¾Ý¿âÔì³ÉµÄÓ°Ïì¡£

ÒÔÉϾÍÊÇthinkphp5ÔõÑù½«Îȹ̵ÄÊý¾Ý¸üÐÂΪ0µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡

ÃâÔð˵Ã÷£ºÒÔÉÏչʾÄÚÈÝȪԴÓÚÏàÖúýÌå¡¢ÆóÒµ»ú¹¹¡¢ÍøÓÑÌṩ»òÍøÂçÍøÂçÕûÀí£¬°æȨÕùÒéÓë±¾Õ¾Î޹أ¬ÎÄÕÂÉæ¼°¿´·¨Óë¿´·¨²»´ú±í×ðÁú¿­Ê±¹ÙÍøµÇ¼ÂËÓÍ»úÍø¹Ù·½Ì¬¶È£¬Çë¶ÁÕß½ö×ö²Î¿¼¡£±¾ÎĽӴýתÔØ£¬×ªÔØÇë˵Ã÷À´ÓÉ¡£ÈôÄúÒÔΪ±¾ÎÄÇÖÕ¼ÁËÄúµÄ°æȨÐÅÏ¢£¬»òÄú·¢Ã÷¸ÃÄÚÈÝÓÐÈκÎÉæ¼°ÓÐÎ¥¹«µÂ¡¢Ã°·¸Ö´·¨µÈÎ¥·¨ÐÅÏ¢£¬ÇëÄúÁ¬Ã¦ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼ʵʱÐÞÕý»òɾ³ý¡£

Ïà¹ØÐÂÎÅ

ÁªÏµ×ðÁú¿­Ê±¹ÙÍøµÇ¼

18523999891

¿É΢ÐÅÔÚÏß×Éѯ

ÊÂÇéʱ¼ä£ºÖÜÒ»ÖÁÖÜÎ壬9:30-18:30£¬½ÚãåÈÕÐÝÏ¢

QR code
ÍøÕ¾µØͼ