2012-06-22 2 views
0

나는 에 새내기입니다. 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 --> 
+0

데이터를 데이터베이스에 실제로 삽입하는 코드를 게시 하시겠습니까? 0000-00-00은 DB 스키마의 기본값으로 정의 될 수 있습니다. 그래서 적절한 값을 전달하지 않으면 기본값을 사용합니다. – Orlymee

+0

정확하게 문제가 무엇인지 명확히 할 수 있습니까? – Orlymee

+0

invoice_issue_date 및 due_date에 입력 된 날짜를 저장할 때 due_date 만 날짜 값을 저장하고 invoice_issue_date 필드에 0000-00-00 만 저장합니다. – NewUser

답변

0

내가이

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

처럼 볼 파일에 afterFind()beforeSave()에 대한 변경을 {{_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> 
1

보기 파일의 코드 : P 추가를 'DUEDATE'규칙 배열에 :

array('dueDate', 'safe'), 
+0

due_date @'array ('invoice_title, invoice_issue_date, due_date', 'required'), ' invoice_issue_date가 저장되지 않습니다. – NewUser

+0

확인. 'beforeSave'함수에서 due_date 값을 설정하려고합니다. 거기서 beforeSave 부모 함수를 호출했지만 CActiveRecord 정의에서는 부울 값을 반환하지 않으므로 코드가 쓸모가 없습니다. S 조건을 제거하고 'due_date'에 대한 설정 코드 만 남기십시오. – sucotronic

+0

'invoice_issue_date'의 경우 값을 가져 오는보기 코드를 보는 데 도움이됩니다. – sucotronic

0
afterFind에서

및 값이 아닌 due_date 값의 서식을 다시 지정합니다. 있음 beforeSave 모든 값이 데이터베이스에서 처리 할 수있는 유효한 타임 스탬프 형식인지 확인해야합니다. YYYY-MM-DD 데이터베이스가 아닌 경우 0000-00-00 대신 데이터베이스를 저장합니다. 내 모델에서

+0

cebe 설명서를 읽었습니까? 나에 따르면 부모님은 부울을 돌려 주실 것입니다. 뭔가 빠진 것이 있습니까? 왜냐하면 나는 sucotronic의 대답에 대해서도 부모님에 관해서 댓글을 달았습니다. : beforeSave –

+0

네가 옳다. 전에 Thougt 이것을 쓸 때를 찾아보십시오. 그것을 제거했다. – cebe

+0

모든 날짜 필드가 데이터베이스의 유효한 타임 스탬프에 있습니다. – NewUser

관련 문제