Wreszcie znalazłem odpowiedź korzystając AttributeBehavior.
W mojej klasie modelu mam napisać kod zachowań:
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
// update 1 attribute 'created' OR multiple attribute ['created','updated']
ActiveRecord::EVENT_BEFORE_INSERT => ['created','updated'],
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated',
],
'value' => function ($event) {
return date('Y-m-d H:i:s', strtotime($this->Created));
},
],
];
}
moim modelu klasy
namespace frontend\models;
use Yii;
use yii\db\ActiveRecord;
use yii\behaviors\AttributeBehavior;
/**
* This is the model class for table "product".
*
* @property integer $id
* @property integer $product_id
* @property string $product_name
* @property string $created
* @property string $updated
*/
class Product extends ActiveRecord
{
public $csv_file;
/**
* @inheritdoc
*/
public static function tableName()
{
return 'product';
}
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => ['created','updated'], // update 1 attribute 'created' OR multiple attribute ['created','updated']
ActiveRecord::EVENT_BEFORE_UPDATE => 'updated', // update 1 attribute 'created' OR multiple attribute ['created','updated']
],
'value' => function ($event) {
return date('Y-m-d H:i:s', strtotime($this->LastUpdated));
},
],
];
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['id', 'product_id', 'product_name', created, updated], 'required'],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'product_id' => 'Product ID',
'product_name' => 'Product Name',
'created' => 'Created',
'updated' => 'Updated',
];
}
}
Jeśli format wejściowy jest D/M/Y wtedy należy zastąpić "/" przez "-"
jak: data wejścia (utworzone) 10/09/2015
date('Y-m-d H:i:s', strtotime(str_replace("/","-",$this->created)));
Jeśli tylko zmienić kolumnę 'Utworzony' do 'created_at', kod będzie bardziej czysty. –