Php Yii2-防止插入时的行为
我的模型中有以下行为:Php Yii2-防止插入时的行为,php,yii,yii2,Php,Yii,Yii2,我的模型中有以下行为: public function behaviors() { return [ 'styles' => [ 'class' => ImageStyleBehavior::className(), 'path' => \Yii::getAlias('@webroot') . '/files/userphotos/styles',
public function behaviors()
{
return [
'styles' => [
'class' => ImageStyleBehavior::className(),
'path' => \Yii::getAlias('@webroot') . '/files/userphotos/styles',
'url' => \Yii::getAlias('@web') . '/files/userphotos/styles',
'attribute' => 'photo',
'styles' => [
'300x300' => [$this, 'style300'], //can be any valid callable
'100x100' => [$this, 'style100'], //can be any valid callable
]
]
];
}
照片的默认值为noavatar.png,当我尝试插入时,出现以下错误:
Exception 'Imagine\Exception\RuntimeException' with message 'Unable to open image /var/www/c2c/Care2Shine/www/files/userphotos/'
我有没有办法防止插入操作的行为 ImageStyle行为是否扩展了?在这种情况下,您应该能够使用:
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'attribute1',
ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2',
],
'value' => function ($event) {
return 'some value';
},
],
];
}
ImageStyleBehavior是否扩展了?在这种情况下,您应该能够使用:
public function behaviors()
{
return [
[
'class' => AttributeBehavior::className(),
'attributes' => [
ActiveRecord::EVENT_BEFORE_INSERT => 'attribute1',
ActiveRecord::EVENT_BEFORE_UPDATE => 'attribute2',
],
'value' => function ($event) {
return 'some value';
},
],
];
}
您可以通过分离特定命名行为来删除它们:
$model->detachBehavior('styles');
或者,如果这是唯一的行为,则可以拆离所有:
$model->detachBehaviors();
要确保仅在插入时分离,请选中
isNewRecord
属性。可以通过分离特定命名行为来删除它们:
$model->detachBehavior('styles');
或者,如果这是唯一的行为,则可以拆离所有:
$model->detachBehaviors();
要确保仅在插入时分离,请检查isNewRecord
属性。覆盖beforeSave()对您没有帮助?覆盖beforeSave()对您没有帮助?