大家好我正在使用dektrium的yii2用户。我会在user_id中保存一个帐户。 User表与我的表有1..N的关系。我是princiapiante,不知道如何在我的表中保存用户ID。我试过了,但数据库中的记录是空白的。
我的模特
public static function tableName()
{
return 'viaggio';
}
/**
* @inheritdoc
*/
public function rules()
{
return [
[['citta_part','citta_arrivo','user_id'], 'required'],
[['data_part','ora_part','data_arrivo','ora_arrivo'],'safe'],
[['posti_disponibili', 'conferma_utenze', 'user_id','posti_max'], 'integer'],
[['prezzo'], 'number'],
[['citta_part', 'via_part', 'citta_arrivo', 'via_arrivo', 'veicolo'], 'string', 'max' => 45],
[['note'], 'string', 'max' => 255],
[['wifi', 'bagno', 'ac_dc','condizioni'],'integer'],
[['user_id'], 'exist', 'skipOnError' => true, 'targetClass' => User::className(), 'targetAttribute' => ['user_id' => 'id']],
];
}
/**
* @inheritdoc
*/
public function attributeLabels()
{
return [
'id_viaggio' => Yii::t('app', 'Id Viaggio'),
'citta_part' => Yii::t('app', 'Citta Part'),
'via_part' => Yii::t('app', 'Via Part'),
'ora_part' => Yii::t('app', 'Ora Part'),
'data_part' => Yii::t('app', 'Data Part'),
'posti_disponibili' => Yii::t('app', 'Posti Disponibili'),
'conferma_utenze' => Yii::t('app', 'Conferma Utenze'),
'prezzo' => Yii::t('app', 'Prezzo'),
'note' => Yii::t('app', 'Note'),
'citta_arrivo' => Yii::t('app', 'Citta Arrivo'),
'data_arrivo' => Yii::t('app', 'Data Arrivo'),
'ora_arrivo' => Yii::t('app', 'Ora Arrivo'),
'via_arrivo' => Yii::t('app', 'Via Arrivo'),
'veicolo' => Yii::t('app', 'Veicolo'),
'posti_max' => Yii::t('app', 'Posti Max'),
'wifi' => Yii::t('app', 'Wifi'),
'bagno' => Yii::t('app', 'Bagno'),
'ac_dc' => Yii::t('app', 'Ac Dc'),
'user_id' => Yii::t('app', 'User ID'),
];
}
public function getUser()
{
return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('offris');
}
我的控制器
public function actionOffri() {
$model = new Offri;
if($model->load(Yii::$app->request->post()) && $model->validate() && $model->save()) {
$model->$user_id = Yii::$app->user->identity->id;
Yii::$app->session->setFlash('success', 'Hai inserito i dati correttamente');
return $this->render('offri', ['model' => $model]);
} else {
Yii::$app->getSession()->setFlash('error', 'Completa correttamente tutti i campi.');
return $this->render('offri', ['model' => $model]);
}
}
建议?
答案 0 :(得分:2)
First try move the assignment for user_id before validation otherwise the validation surely fail and the model is not saved
public function actionOffri()
{
$model = new Offri;
$model->$user_id = Yii::$app->user->id;
if($model->load(Yii::$app->request->post())&& $model->validate() && $model->save()) {
Yii::$app->session->setFlash('success', 'Hai inserito i dati correttamente');
return $this->render('offri', ['model' => $model]);
} else {
Yii::$app->getSession()->setFlash('error', 'Completa correttamente tutti i campi.');
return $this->render('offri', ['model' => $model]);
}
}
or if yuo need the username you can use Yii::$app->user->identity->username
答案 1 :(得分:1)
I think your code has error:
$model->$user_id = Yii::$app->user->identity->id;
Try replace it by:
$model->user_id = Yii::$app->user->identity->id;