我是新手Yii Framework
。我有一个model called invoice
。在那个模型中,属性就像
invoice_title,invoice_issue_date,due_date,description
。现在我的问题是,由于我的模型同时具有 invoice_issue_date 和 due_date 都是日期字段,但只有一个存储日期,另一个只是存储它。这0000-00-00
是我的模型的代码。
<?php
/**
* This is the model class for table "{{invoices}}".
*
* The followings are the available columns in table '{{invoices}}':
* @property integer $id
* @property integer $customer_id
* @property integer $payment_id
* @property string $invoice_title
* @property string $invoice_issue_date
* @property string $due_date
* @property string $description
*/
class Invoices extends CActiveRecord
{
/**
* Returns the static model of the specified AR class.
* @param string $className active record class name.
* @return Invoices the static model class
*/
public static function model($className=__CLASS__)
{
return parent::model($className);
}
/**
* @return string the associated database table name
*/
public function tableName()
{
return '{{invoices}}';
}
/**
* @return array validation rules for model attributes.
*/
public function rules()
{
// NOTE: you should only define rules for those attributes that
// will receive user inputs.
return array(
array('invoice_title, invoice_issue_date,due_date', 'required'),
array('invoice_title','length','min'=>6),
array('invoice_title,description', 'length', 'max'=>120),
// The following rule is used by search().
// Please remove those attributes that should not be searched.
array('id, invoice_title, invoice_issue_date, due_date, description', 'safe', 'on'=>'search'),
);
}
/**
* @return array relational rules.
*/
public function relations()
{
// NOTE: you may need to adjust the relation name and the related
// class name for the relations automatically generated below.
return array(
);
}
protected function afterFind(){
parent::afterFind();
$this->due_date=date('d F, Y', strtotime(str_replace("-", "", $this->due_date)));
}
protected function beforeSave(){
if(parent::beforeSave()){
$this->due_date=date('Y-m-d', strtotime(str_replace(",", "", $this->due_date)));
return TRUE;
}
else return false;
}
/**
* @return array customized attribute labels (name=>label)
*/
public function attributeLabels()
{
return array(
'id' => 'ID',
'invoice_title' => 'Invoice Title',
'invoice_issue_date' => 'Invoice Issue Date',
'due_date' => 'Due Date',
'description' => 'Description',
);
}
/**
* Retrieves a list of models based on the current search/filter conditions.
* @return CActiveDataProvider the data provider that can return the models based on the search/filter conditions.
*/
public function search()
{
// Warning: Please modify the following code to remove attributes that
// should not be searched.
$criteria=new CDbCriteria;
$criteria->compare('id',$this->id);
$criteria->compare('invoice_title',$this->invoice_title,true);
$criteria->compare('invoice_issue_date',$this->invoice_issue_date,true);
$criteria->compare('due_date',$this->due_date,true);
$criteria->compare('description',$this->description,true);
return new CActiveDataProvider($this, array(
'criteria'=>$criteria,
));
}
}
查看文件的代码
<div class="form">
<?php $form=$this->beginWidget('CActiveForm', array(
'id'=>'invoices-form',
'enableAjaxValidation'=>false,
)); ?>
<p class="note">Fields with <span class="required">*</span> are required.</p>
<?php echo $form->errorSummary($model); ?>
<div class="row">
<?php echo $form->labelEx($model,'invoice_title'); ?>
<?php echo $form->textField($model,'invoice_title',array('size'=>45,'maxlength'=>45)); ?>
<?php echo $form->error($model,'invoice_title'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'invoice_issue_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute'=>'invoice_issue_date',
'model'=>$model,
'options' => array(
'mode'=>'focus',
'dateFormat'=>'d MM, yy',
'showAnim' => 'slideDown',
),
'htmlOptions'=>array('size'=>30,'class'=>'date'),
)
);
?>
<?php echo $form->error($model,'invoice_issue_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'due_date'); ?>
<?php
$this->widget('zii.widgets.jui.CJuiDatePicker',
array(
'attribute'=>'due_date',
'model'=>$model,
'options' => array(
'mode'=>'focus',
'dateFormat'=>'d MM, yy',
'showAnim' => 'slideDown',
),
'htmlOptions'=>array('size'=>30,'class'=>'date'),
)
);
?>
<?php echo $form->error($model,'due_date'); ?>
</div>
<div class="row">
<?php echo $form->labelEx($model,'description'); ?>
<?php echo $form->textArea($model,'description',array('rows'=>6, 'cols'=>50)); ?>
<?php echo $form->error($model,'description'); ?>
</div>
<div class="row buttons">
<?php echo CHtml::submitButton($model->isNewRecord ? 'Create' : 'Save'); ?>
</div>
<?php $this->endWidget(); ?>
</div><!-- form -->