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

Fellow Yii-ites,

I added a new date field at the DB table after the model and form code were generated. The value from this field is not getting persisted in the DB. Tried both create and update. However, it works if the field is made "required" in the model class. It is a nullable field in the DB and I do not want to force it be "required" in the model class.

In Model class function rules (check the field 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'),
    );

The property is not in the "required" list.

In _form.php view:

<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>

Note that I tried this without the DatePicker widget, the behaviour is the same.

DB Table definition goes like this -

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)

Additional info: The form shows the date value in the update action, if I populate the field in the DB table using a SQL UPDATE. There is no issue with the field names and binding because it works fine if made "required".

What seems to be the problem here?

Cheers. Mukul

Update: Here's another hint. The problem disappears if I add the following line of code in the rules() function.

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

It seems that model need some sort of evidence that the field exists.

I think the problem is array('name'=>'enrolby_date'). When you generate form, yii generates <input type="text" name="ModelName['fieldName']">. But if you change name attribute of the field and assign it's name manually, yii does not set it automatically. I think you have two solutions, first you can change name into it: array('name'=>'YourModelName[enrolby_date]').

Second solution: you can assign "enrolby_date" manually in the controller in this way:

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