除非在Yii 1.1中强制要求,否则新添加的字段不会更新

时间:2015-03-17 17:02:22

标签: php model-view-controller yii

Yii-ites,

在生成模型和表单代码后,我在数据库表中添加了一个新的日期字段。此字段中的值不会在DB中保留。尝试创建和更新。但是,如果该字段是“#34; required"在模型类中。它在数据库中是一个可以为空的字段,我不想强​​迫它是“#34; required"在模型类中。

在Model类函数规则中(检查字段enrolby_date):

return array(
        array('level, start_date, court_type, org_id', 'required'),
        array('level, court_type, org_id, status, no_courts', 'numerical', 'integerOnly'=>true),
        array('location, referee', 'length', 'max'=>45),
        // The following rule is used by search().
        // @todo Please remove those attributes that should not be searched.
        array('id, location, level, start_date, enrolby_date, referee, court_type, org_id', 'safe', 'on'=>'search'),
    );

该物业不在"必需"列表。

_form.php视图中:

<tr><td>
        <?php echo $form->labelEx($model,'enrolby_date'); ?>
        </td><td><?php $this->widget('zii.widgets.jui.CJuiDatePicker', array('name'=>'enrolby_date',
                'attribute'=>'enrolby_date',
                'model'=>$model,
                'options'=>array('showAnim'=>'fold', 'dateFormat'=> 'yy-mm-dd'),
            ));
        ?>
        <?php echo $form->error($model,'enrolby_date'); ?>
    </td></tr>

请注意,我在没有DatePicker小部件的情况下尝试了这个,行为是相同的。

数据库表定义是这样的 -

mysql> desc tour;
+--------------+-------------+------+-----+---------+----------------+
| Field        | Type        | Null | Key | Default | Extra          |
+--------------+-------------+------+-----+---------+----------------+
| id           | int(11)     | NO   | PRI | NULL    | auto_increment |
| location     | varchar(45) | YES  |     | NULL    |                |
| level        | int(2)      | YES  |     | NULL    |                |
| start_date   | date        | YES  |     | NULL    |                |
| enrolby_date | date        | YES  |     | NULL    |                |
| referee      | varchar(45) | YES  |     | NULL    |                |
| court_type   | int(2)      | YES  |     | NULL    |                |
| status       | int(2)      | YES  |     | NULL    |                |
| org_id       | int(11)     | NO   | MUL | NULL    |                |
| no_courts    | int(2)      | YES  |     | NULL    |                |
+--------------+-------------+------+-----+---------+----------------+
10 rows in set (0.06 sec)

附加信息:如果我使用SQL UPDATE填充数据库表中的字段,则表单会在更新操作中显示日期值。字段名称和绑定没有问题,因为如果制作&#34;必需&#34;它可以正常工作。

这里似乎有什么问题?

干杯。 穆库尔

更新:这是另一个提示。如果我在rules()函数中添加以下代码行,问题就会消失。

array('start_date, enrolby_date', 'type', 'type' => 'date', 'message' => '{attribute}: is not a date!', 'dateFormat' => 'yyyy-MM-dd'),

模型似乎需要某种证据证明该领域存在。

1 个答案:

答案 0 :(得分:0)

我认为问题是array('name'=>'enrolby_date')。生成表单时,yii会生成<input type="text" name="ModelName['fieldName']">。但是,如果您更改字段的name属性并手动分配name,则yii不会自动设置它。我认为您有两个解决方案,首先您可以将名称更改为:array('name'=>'YourModelName[enrolby_date]')

第二个解决方案:您可以指定&#34; enrolby_date&#34;以这种方式在控制器中手动:

$model->enrolby_date = $_POST['enrolby_date'];