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µÄÏêϸÄÚÈÝ£¬¸ü¶àÇë¹Ø×¢±¾ÍøÄÚÆäËüÏà¹ØÎÄÕ£¡