admin 发表于 2020-11-22 15:16:29

ThinkPHP,三、自动时间写入创建和更新时间字段及时间输出...


<p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">系统支持自动写入创建和更新的时间戳字段,有两种方式配置支持。<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">第一种方式,是在数据库配置文件中添加全局设置:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">开启自动写入时间戳字段<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">'auto_timestamp'
=&gt; true,<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">第二种是直接在单独的模型类里面设置:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$autoWriteTimestamp = true;<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">如果这两个地方设置为<span lang="EN-US">true</span>,默认识别为整型<span lang="EN-US">int </span>类型,如果你的时间字段不是<span lang="EN-US">int </span>类型的话,例如使用<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">datetime
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">类型的话,可以这样设置:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">开启自动写入时间戳字段<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">'auto_timestamp'
=&gt; 'datetime',<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">或者<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$autoWriteTimestamp = 'datetime';<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">字段名默认创建时间字段为<span lang="EN-US">create_time </span>,更新时间字段为<span lang="EN-US">update_time </span>,支持的字段类型包括<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">timestamp/datetime/int
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">。<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">写入数据的时候,系统会自动写入<span lang="EN-US">create_time </span>和<span lang="EN-US">update_time </span>字段,而不需要定义修改器,例如:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user
= new User();<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user-&gt;name
= 'THINKPHP';<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user-&gt;save();<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">echo
$user-&gt;create_time; // </span><span style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">输出类似<span lang="EN-US"> 2016-10-12 14:20:10<o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">时间戳<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">echo
$user-&gt;update_time; // </span><span style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">输出类似<span lang="EN-US"> 2016-10-12 14:20:10<o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">V5.0.5+
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">版本开始,时间字段输出的时候会自动进行格式转换,如果不希望自动格式化输出,可以把数据<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">库配置文件的<span lang="EN-US"> datetime_format </span>参数值改为<span lang="EN-US">false </span>(<span lang="EN-US"> V5.0.6+ </span>版本支持,之前版本可以使用类<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">型转换方式关闭自动格式化)<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:red;mso-font-kerning:0pt">数据表字段名不是默认的<span lang="EN-US">create_time</span>可以用此方式设置数据库字段自动写入时间,如数据库字段名为<span lang="EN-US">create_at</span>。数据表字段名为默认值<span lang="EN-US">create_time</span>时会直接写入。:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">class
User extends Model<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">{<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">定义时间戳字段名<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$createTime = 'create_at';<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$updateTime = 'update_at';<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">}<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">下面是修改字段后的输出代码:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user
= new User();<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user-&gt;name
= 'THINKPHP';<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">$user-&gt;save();<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">echo
$user-&gt;create_at; // </span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">输出类似<span lang="EN-US"> 2016-10-12 14:20:10<o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">echo
$user-&gt;update_at; // </span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">输出类似<span lang="EN-US"> 2016-10-12 14:20:10<o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">如果你只需要使用<span lang="EN-US">create_time </span>字段而不需要自动写入<span lang="EN-US">update_time </span>,则可以单独设置关闭某个字段,<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">例如:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">class
User extends Model<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">{<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">关闭自动写入<span lang="EN-US">update_time</span>字段<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$updateTime = false;<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">}<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">如果不需要任何自动写入的时间戳字段的话,可以关闭时间戳自动写入功能,设置如下:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">class
User extends Model<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">{<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">关闭自动写入时间戳<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">protected
$autoWriteTimestamp = false;<o:p></o:p></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">}<o:p></o:p></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">如果是关闭全局的自动时间写入,则可以使用:<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span style="font-size:12.0pt;font-family:彩虹粗仿宋;
mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">时间戳<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">//
</span><span style="font-size:12.0pt;font-family:彩虹粗仿宋;mso-bidi-font-family:
MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">关闭全局自动写入时间字段<span lang="EN-US"><o:p></o:p></span></span></p><p class="MsoNormal" align="left">









































































































</p><p class="MsoNormal" align="left"><span lang="EN-US" style="font-size:12.0pt;font-family:
彩虹粗仿宋;mso-bidi-font-family:MicrosoftYaHei;color:#525252;mso-font-kerning:0pt">'auto_timestamp'
=&gt; false,<o:p></o:p></span></p>

leya 发表于 2022-3-29 12:14:35

写入数据的时候,系统会自动写入幸运飞艇走势图双色球开奖结果澳洲幸运20稳赚技巧 段,而不需要定义修改器,例如:


页: [1]
查看完整版本: ThinkPHP,三、自动时间写入创建和更新时间字段及时间输出...