Можно просто добавить в модель Event пустой afterSave
Код: Выделить всё
public function afterSave($insert, $changedAttributes)
{
}
Можно просто добавить в модель Event пустой afterSave
Код: Выделить всё
public function afterSave($insert, $changedAttributes)
{
}
Код: Выделить всё
Failed to instantiate component or class "created_at".
↵
Caused by: ReflectionException
Class created_at does not exist
Код: Выделить всё
public function behaviors()
{
return [
TimestampBehavior::className(),
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
];
}
Код: Выделить всё
public function behaviors()
{
return [
'class' => TimestampBehavior::class,
'createdAtAttribute' => 'created_at',
'updatedAtAttribute' => 'updated_at',
'value' => new Expression('NOW()'),
];
}
Код: Выделить всё
Database Exception – yii\db\Exception
SQLSTATE[HY000]: General error: 1364 Field 'created_at' doesn't have a default value
The SQL being executed was: INSERT INTO `event` (`title`) VALUES ('тестирую событие')
Error Info: Array
(
[0] => HY000
[1] => 1364
[2] => Field 'created_at' doesn't have a default value
)
Код: Выделить всё
<?php
namespace app\models;
use Yii;
use yii\behaviors\TimestampBehavior;
use yii\db\Expression;
use yii\db\ActiveRecord;
/**
* This is the model class for table "event".
*
* @property int $id
* @property string $title
*/
class Event extends \yii\db\ActiveRecord
{
public function behaviors()
{
return [
TimestampBehavior::class
];
}
/**
* {@inheritdoc}
*/
public static function tableName()
{
return 'event';
}
/**
* {@inheritdoc}
*/
public function rules()
{
return [
// [['created_at', 'updated_at'], 'required'],
[['created_at', 'updated_at'], 'integer'],
[['created_at', 'updated_at'], 'safe'],
// [['title'], 'required'],
[['title'], 'string', 'max' => 250],
];
}
/**
* {@inheritdoc}
*/
public function attributeLabels()
{
return [
'id' => 'ID',
'title' => 'Title',
];
}
public function afterSave($insert, $changedAttributes)
{
}
}
Скопировал вашу модель, создал таблицу и не смог воспроизвести ошибку, значения created_at и updated_at записываются корректно.svil писал(а): ↑2019.08.14, 17:17 ОшибкаКод: Выделить всё
Database Exception – yii\db\Exception SQLSTATE[HY000]: General error: 1364 Field 'created_at' doesn't have a default value The SQL being executed was: INSERT INTO `event` (`title`) VALUES ('тестирую событие') Error Info: Array ( [0] => HY000 [1] => 1364 [2] => Field 'created_at' doesn't have a default value )
Код: Выделить всё
Yii::$app->db->createCommand()->insert('event', ['title' => $event->sender->name . ' добавлен', 'created_at' => time()])->execute();
Обычно требуется указывать какие поля изменились.svil писал(а): ↑2019.08.14, 18:41 Добавила с 2 таблиц записи и в Event они пояились , но я не могу указать какие поля добавились
http://ilin.itcrk.ru/yii2_bz_events/web/event/index
Код: Выделить всё
Event::on(
\yii\db\ActiveRecord::class,
\yii\db\ActiveRecord::EVENT_AFTER_INSERT,
function ($event) {
$ev = new \common\models\Event();
$ev->title = get_class($event->sender);
$ev->event_type = 'insert';
$ev->model_id = $event->sender->id;
$ev->save();
}
);
Event::on(
\yii\db\ActiveRecord::class,
\yii\db\ActiveRecord::EVENT_AFTER_UPDATE,
function ($event) {
$ev = new \common\models\Event();
$ev->title = get_class($event->sender);
$ev->event_type = 'update';
$ev->model_id = $event->sender->id;
$ev->changed_attributes = json_encode($event->changedAttributes, JSON_UNESCAPED_UNICODE);
$ev->save();
}
);
Код: Выделить всё
public $mess;
...
public function attr()
{
$mess = $this->name.$this->id.$this->id ; //здесь поля разные могут быть
return $mess;
}
Код: Выделить всё
Yii::$app->db->createCommand()->insert('event', ['title' => $event->sender->attr() . ' изменен', 'created_at' => time(), 'updated_at' => time()])->execute();