<bdo id='kqWrI'></bdo><ul id='kqWrI'></ul>
<legend id='kqWrI'><style id='kqWrI'><dir id='kqWrI'><q id='kqWrI'></q></dir></style></legend>

    <i id='kqWrI'><tr id='kqWrI'><dt id='kqWrI'><q id='kqWrI'><span id='kqWrI'><b id='kqWrI'><form id='kqWrI'><ins id='kqWrI'></ins><ul id='kqWrI'></ul><sub id='kqWrI'></sub></form><legend id='kqWrI'></legend><bdo id='kqWrI'><pre id='kqWrI'><center id='kqWrI'></center></pre></bdo></b><th id='kqWrI'></th></span></q></dt></tr></i><div id='kqWrI'><tfoot id='kqWrI'></tfoot><dl id='kqWrI'><fieldset id='kqWrI'></fieldset></dl></div>

      <tfoot id='kqWrI'></tfoot>
    1. <small id='kqWrI'></small><noframes id='kqWrI'>

    2. 如何在 Yii 中将日期设置为 NULL?

      时间:2023-08-18
      <legend id='ZjBQ5'><style id='ZjBQ5'><dir id='ZjBQ5'><q id='ZjBQ5'></q></dir></style></legend>
        <tbody id='ZjBQ5'></tbody>

            <tfoot id='ZjBQ5'></tfoot>

            <small id='ZjBQ5'></small><noframes id='ZjBQ5'>

              <bdo id='ZjBQ5'></bdo><ul id='ZjBQ5'></ul>
              <i id='ZjBQ5'><tr id='ZjBQ5'><dt id='ZjBQ5'><q id='ZjBQ5'><span id='ZjBQ5'><b id='ZjBQ5'><form id='ZjBQ5'><ins id='ZjBQ5'></ins><ul id='ZjBQ5'></ul><sub id='ZjBQ5'></sub></form><legend id='ZjBQ5'></legend><bdo id='ZjBQ5'><pre id='ZjBQ5'><center id='ZjBQ5'></center></pre></bdo></b><th id='ZjBQ5'></th></span></q></dt></tr></i><div id='ZjBQ5'><tfoot id='ZjBQ5'></tfoot><dl id='ZjBQ5'><fieldset id='ZjBQ5'></fieldset></dl></div>

              1. 本文介绍了如何在 Yii 中将日期设置为 NULL?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个日期列,通常将值设为 dd.MM.yyyy.它在模型的 rules() 中的验证规则是这样的:

                I have a date column that usually takes values as dd.MM.yyyy. Its validation rule in the model's rules() is this:

                array('start, end', 'date', 'format' => 'dd.MM.yyyy'),
                

                我正在从 CSV 文件填充数据库,如果 CSV 记录为空,我希望能够将日期设置为 NULL(即没有).所以,我正在做:

                I'm populating the database from a CSV file, and I'd like to be able to set the date to NULL (i.e. nothing) if the CSV record is empty. So, I'm doing:

                if (empty($csv_data)) {
                  $user->start = new CDbExpression('NULL');
                } else {
                  $user->start = $csv_data;
                }
                

                但我收到一个错误,指出日期格式无效.这是为什么?

                But I get an error that the date format is invalid. Why is that?

                CDateValidator 文档 说allowEmpty 属性默认为 true,所以应该可以设置为 NULL 吧?请注意,如果我只是将 "" 字符串分配给日期,它会将其转换为 0000-00-00 00:00:00 时间戳,这不是NULL.

                The CDateValidator documentation says that the allowEmpty property is true by default, so it should be able to set this to NULL, right? Note that if I just assing the "" string to the date, it'll convert it to a 0000-00-00 00:00:00 timestamp, which is not NULL.

                推荐答案

                in model rules():

                in model rules():

                array('start, end', 'date', 'format' => 'dd.MM.yyyy'),
                array('start, end', 'default', 'setOnEmpty' => true, 'value' => null),
                

                还有,

                if (empty($csv_data)) {
                  $user->start = null;
                } ...
                

                也应该这样做.

                这篇关于如何在 Yii 中将日期设置为 NULL?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                上一篇:如何在 Yii 中禁用 Ajax 请求上的 jQuery 自动加载? 下一篇:尽管预计至少有一行,但未找到查询结果

                相关文章

                1. <tfoot id='TCw7R'></tfoot>

                  <legend id='TCw7R'><style id='TCw7R'><dir id='TCw7R'><q id='TCw7R'></q></dir></style></legend>
                2. <i id='TCw7R'><tr id='TCw7R'><dt id='TCw7R'><q id='TCw7R'><span id='TCw7R'><b id='TCw7R'><form id='TCw7R'><ins id='TCw7R'></ins><ul id='TCw7R'></ul><sub id='TCw7R'></sub></form><legend id='TCw7R'></legend><bdo id='TCw7R'><pre id='TCw7R'><center id='TCw7R'></center></pre></bdo></b><th id='TCw7R'></th></span></q></dt></tr></i><div id='TCw7R'><tfoot id='TCw7R'></tfoot><dl id='TCw7R'><fieldset id='TCw7R'></fieldset></dl></div>
                    • <bdo id='TCw7R'></bdo><ul id='TCw7R'></ul>

                    <small id='TCw7R'></small><noframes id='TCw7R'>