title: Yii2 save in the table user_id account
body: <p>I'm a beginner in yii2 , I would like to save in the table associated with the account , the account id . Any suggestions?</p>
  
  <p><strong>My Model</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>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' =&gt; 45],
              [['note'], 'string', 'max' =&gt; 255],
              [['wifi', 'bagno', 'ac_dc','condizioni'],'integer'],
               [['user_id'], 'unique'],
              [['user_id'], 'exist', 'skipOnError' =&gt; true, 'targetClass' =&gt; User::className(), 'targetAttribute' =&gt; ['user_id' =&gt; 'id']],
             
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function attributeLabels()
      {
          return [
              'id_viaggio' =&gt; Yii::t('app', 'Id Viaggio'),
              'citta_part' =&gt; Yii::t('app', 'Citta Part'),
              'via_part' =&gt; Yii::t('app', 'Via Part'),
              'ora_part' =&gt; Yii::t('app', 'Ora Part'),
              'data_part' =&gt; Yii::t('app', 'Data Part'),
              'posti_disponibili' =&gt; Yii::t('app', 'Posti Disponibili'),
              'conferma_utenze' =&gt; Yii::t('app', 'Conferma Utenze'),
              'prezzo' =&gt; Yii::t('app', 'Prezzo'),
              'note' =&gt; Yii::t('app', 'Note'),
              'citta_arrivo' =&gt; Yii::t('app', 'Citta Arrivo'),
              'data_arrivo' =&gt; Yii::t('app', 'Data Arrivo'),
              'ora_arrivo' =&gt; Yii::t('app', 'Ora Arrivo'),
              'via_arrivo' =&gt; Yii::t('app', 'Via Arrivo'),
              'veicolo' =&gt; Yii::t('app', 'Veicolo'),
              'posti_max' =&gt; Yii::t('app', 'Posti Max'),
              'wifi' =&gt; Yii::t('app', 'Wifi'),
              'bagno' =&gt; Yii::t('app', 'Bagno'),
              'ac_dc' =&gt; Yii::t('app', 'Ac Dc'),
            
          ];
      }
      
    
      public function getUser()
      {
           
          return $this-&gt;hasOne(User::className(), ['id' =&gt; 'user_id'])-&gt;inverseOf('offri');
      }</code></pre>
  </div>
  </div>
  </p>
  
  <p>the field that I want to save in the table is user_id , which must be to the account id</p>
  
  <p><strong>My Controller</strong></p>
  
  <p><div class="snippet" data-lang="js" data-hide="false" data-console="true" data-babel="false">
  <div class="snippet-code">
  <pre class="snippet-code-html lang-html prettyprint-override"><code>&lt;?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' =&gt; [
                  'class' =&gt; AccessControl::className(),
                  'only' =&gt; ['logout'],
                  'rules' =&gt; [
                      [
                          'actions' =&gt; ['logout'],
                          'allow' =&gt; true,
                          'roles' =&gt; ['@'],
                      ],
                          [
                          'actions' =&gt; ['create'],
                          'allow' =&gt; true,
                          'roles' =&gt; ['admin'],
                      ],
                      [
                          'actions' =&gt; ['view', 'search'],
                          'allow' =&gt; true,
                          'roles' =&gt; ['?', '@', 'admin'],
                      ],
                  ],
              ],
              'verbs' =&gt; [
                  'class' =&gt; VerbFilter::className(),
                  'actions' =&gt; [
                      'logout' =&gt; ['post'],
                  ],
              ],
          ];
      }
  
      /**
       * @inheritdoc
       */
      public function actions()
      {
          return [
              'error' =&gt; [
                  'class' =&gt; 'yii\web\ErrorAction',
              ],
              'captcha' =&gt; [
                  'class' =&gt; 'yii\captcha\CaptchaAction',
                  'fixedVerifyCode' =&gt; YII_ENV_TEST ? 'testme' : null,
              ],
          ];
      }
  
     
     public function actionregisterUser()
      {
         $model = new Accounts();
      
  
          if ($model-&gt;load(Yii::$app-&gt;request-&gt;post())) {
              if ($user = $model-&gt;registerUser()) {
                  if (Yii::$app-&gt;getUser()-&gt;login($user)) {
                      return $this-&gt;goHome();
                  }
              }
          }
  
  }
  public function actionSomeAction($id) {
          $model = $this-&gt;findModel($id);
          if ($model-&gt;load(Yii::$app-&gt;request-&gt;post()) &amp;&amp; $model-&gt;save()) {
              return $this-&gt;redirect(['view', 'id' =&gt; (string) $model-&gt;_id]);
          }elseif (Yii::$app-&gt;request-&gt;isAjax) {
              return $this-&gt;renderAjax('_form', [
                          'model' =&gt; $model
              ]);
          } else {
              return $this-&gt;render('_form', [
                          'model' =&gt; $model
              ]);
          }
      }
  public function actionOffri()
     {
      $model = new Offri;
  
  
      if($model-&gt;load(Yii::$app-&gt;request-&gt;post())&amp;&amp; $model-&gt;validate() &amp;&amp; $model-&gt;save())
         {
     
          Yii::$app-&gt;session-&gt;setFlash('success', 'Hai inserito i dati correttamente');
      return $this-&gt;render('offri', ['model' =&gt; $model]);
         }else {
               Yii::$app-&gt;getSession()-&gt;setFlash('error', 'Completa correttamente tutti i campi.');
      return $this-&gt;render('offri', ['model' =&gt; $model]);
          
     
  
  }
  }
  }</code></pre>
  </div>
  </div>
  </p>