Saba Saba - 3 months ago 9
MySQL Question

Yii2 save in the table user_id account

I'm a beginner in yii2 , I would like to save in the table associated with the account , the account id . Any suggestions?

My Model



class Offri extends \yii\db\ActiveRecord
{
/**
* @inheritdoc
*/
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'], 'unique'],
[['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'),

];
}


public function getUser()
{

return $this->hasOne(User::className(), ['id' => 'user_id'])->inverseOf('offri');
}





the field that I want to save in the table is user_id , which must be to the account id

My Controller



<?php

namespace app\controllers;

use Yii;
use yii\filters\AccessControl;
use yii\web\Controller;
use app\filters\AccessRule;
use yii\filters\VerbFilter;
use app\models\LoginForm;
use app\models\ContactForm;
use app\models\Accounts;
use app\models\Offri;
class SiteController extends Controller
{
/**
* @inheritdoc
*/
public function behaviors()
{
return [
'access' => [
'class' => AccessControl::className(),
'only' => ['logout'],
'rules' => [
[
'actions' => ['logout'],
'allow' => true,
'roles' => ['@'],
],
[
'actions' => ['create'],
'allow' => true,
'roles' => ['admin'],
],
[
'actions' => ['view', 'search'],
'allow' => true,
'roles' => ['?', '@', 'admin'],
],
],
],
'verbs' => [
'class' => VerbFilter::className(),
'actions' => [
'logout' => ['post'],
],
],
];
}

/**
* @inheritdoc
*/
public function actions()
{
return [
'error' => [
'class' => 'yii\web\ErrorAction',
],
'captcha' => [
'class' => 'yii\captcha\CaptchaAction',
'fixedVerifyCode' => YII_ENV_TEST ? 'testme' : null,
],
];
}


public function actionregisterUser()
{
$model = new Accounts();


if ($model->load(Yii::$app->request->post())) {
if ($user = $model->registerUser()) {
if (Yii::$app->getUser()->login($user)) {
return $this->goHome();
}
}
}

}
public function actionSomeAction($id) {
$model = $this->findModel($id);
if ($model->load(Yii::$app->request->post()) && $model->save()) {
return $this->redirect(['view', 'id' => (string) $model->_id]);
}elseif (Yii::$app->request->isAjax) {
return $this->renderAjax('_form', [
'model' => $model
]);
} else {
return $this->render('_form', [
'model' => $model
]);
}
}
public function actionOffri()
{
$model = new Offri;


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]);



}
}
}




Answer

update your actionOffri() like below

public function actionOffri()
{
    $model = new Offri;
    if($model->load(Yii::$app->request->post())){

        $model->user_id=Yii::$app->user->identity->id;
            if($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]);
      }
}

work my Controller

public function actionOffri()
   {
    $model = new Offri;

     $model->user_id = Yii::$app->user->identity->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]);
        
   

}
}

work my model

 public function rules()
    {
        return [
            [['citta_part','citta_arrivo'], 'required'],
            [['data_part','ora_part','data_arrivo','ora_arrivo'],'safe'],
            [['posti_disponibili', 'conferma_utenze','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'],
           
           
        ];
    }

i have removed in rules 'user_id'