不在单个模型中存储两个日期字段

时间:2012-06-22 11:25:19

标签: php mysql yii

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

3 个答案:

答案 0 :(得分:1)

从yii开始这是一个非常常见的错误:P将'dueDate'添加到规则数组中:

array('dueDate', 'safe'),

答案 1 :(得分:0)

afterFindbeforeSave中,您要重新格式化due_date值,而不是invoice_issue_date值。 在beforeSave中您必须确保所有值都是数据库可以处理的有效时间戳格式。 因此,如果不是YYYY-MM-DD数据库,则可能会保存0000-00-00

答案 2 :(得分:0)

在我的模型中,我对afterFind()beforeSave()进行了更改

  protected function afterFind(){
    parent::afterFind();
    $this->due_date=date('d F, Y', strtotime(str_replace("-", "", $this->due_date)));
    $this->invoice_issue_date=date('d F, Y', strtotime(str_replace("-", "", $this->invoice_issue_date)));
  }

  protected function beforeSave(){
    if(parent::beforeSave()){
        $this->due_date=date('Y-m-d', strtotime(str_replace(",", "", $this->due_date)));
        $this->invoice_issue_date=date('Y-m-d', strtotime(str_replace(",", "", $this->invoice_issue_date)));
        return TRUE;
    }
    else return false;
  }

并在视图文件{{_form}}中。我就这样做了

<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', 'value'=>date("d F, Y")),
        )
      );      
     ?>
   <?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>