Home > Yii > ActiveRecord::afterFind

ActiveRecord::afterFind

Ein Datum wird in MySQL in der Form 0000-00-00 abgelegt.
Nun will ich zum Beispiel dem Benutzer die Möglichkeit geben, seinen Geburtstag einzugeben. Ich mach das eigentlich ziemlich gerne mit einem CMaskedTextField.

<div class="simple">
    <?php echo CHtml::activeLabelEx($model,'birthday'); ?>
    <?php $this->widget('CMaskedTextField', array('model' => $model, 'attribute' => 'birthday', 'mask' => '99.99.9999', 'htmlOptions' => array('size' => 32)));?>
</div>

ich geb hier schon ein deutsches Format ein und muss es dementsprechend ins MySQL-Format bringen:

public function beforeSave()
{
    if (!empty($this->birthday)) {
        list($day, $month, $year) = explode('.', $this->birthday);
        $this->birthday = implode('-', array($year, $month, $day));
    } else {
        $this->birthday = '0000-00-00';
    }
 
    return parent::beforeSave();
}

… und für das Formular auch wieder in das deutsche Anzeigeformat. Dazu benutz ich die afterFind()-Methode:

public function afterFind()
{
    list($year, $month, $day) = explode('-', $this->birthday);
    $this->birthday = implode('.', array($day, $month, $year));
    parent::afterFind();
}

Wer hier ne andere Möglichkeit sieht, bitte um Feedback.

Author: admin Categories: Yii Tags:
  1. Dan
    25. Mai 2010, 14:54 | #1

    Danke für den Tipp.

    An dem Problem mit der deutschen Darstellung eines Datums habe ich schon Stunden habe ich schon oft …

  1. Bisher keine Trackbacks