나는 에 새내기입니다. 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,
));
}
}
이 YII로 시작에서 매우 일반적인 실수이다
<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 -->
데이터를 데이터베이스에 실제로 삽입하는 코드를 게시 하시겠습니까? 0000-00-00은 DB 스키마의 기본값으로 정의 될 수 있습니다. 그래서 적절한 값을 전달하지 않으면 기본값을 사용합니다. – Orlymee
정확하게 문제가 무엇인지 명확히 할 수 있습니까? – Orlymee
invoice_issue_date 및 due_date에 입력 된 날짜를 저장할 때 due_date 만 날짜 값을 저장하고 invoice_issue_date 필드에 0000-00-00 만 저장합니다. – NewUser