Search code examples
yii2yii2-modelyii-form

yii2 data to the db saves them all null


hello guys I have a problem when I send the form data in the database , all NULL fields are saved . Any suggestions? my Controller

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

my Model I just added the rules and variables of the fields feature ex.

public $name;
public function rules()
    {
        return [['name'],'string'.....
                 ....
               ];
}
My Full Model

class Offri extends \yii\db\ActiveRecord
{
    /**
     * @inheritdoc
     */
    public static function tableName()
    {
        return 'viaggio';
    }

public $data_par;
public $ora_part;
public $data_arrivo;
public $ora_arrivo;
public $citta_part;
public $citta_arrivo;
public $wifi;
public $bagno;
public $ac_dc;
public $condizioni;
    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['citta_part','citta_arrivo'], 'required'],
            [['citta_part','citta_arrivo'], 'string', 'max' => 255],
            [['posti_disponibili', 'conferma_utenze', 'accounts_id_account', 'posti_max', 'wifi', 'bagno', 'ac_dc'], 'integer'],
            [['prezzo'], 'number'],
            [['accounts_id_account'], 'required'],
            [['citta_part', 'via_part', 'citta_arrivo', 'via_arrivo', 'veicolo'], 'string', 'max' => 45],
            [['note'], 'string', 'max' => 255],
            [['accounts_id_account'], 'exist', 'skipOnError' => true, 'targetClass' => Accounts::className(), 'targetAttribute' => ['accounts_id_account' => 'id_account']],
        ];
    }

    /**
     * @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'),
            'accounts_id_account' => Yii::t('app', 'Accounts Id Account'),
            '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'),
        ];
    }


Solution

  • From what I can see from your rules, labels and members, it is probably because you're using virtual members where you shouldn't. Note that using virtual members will overwrite any value from user's input. Since you're not assigning any manual value, Yii just clears all and leaves them empty. You also most likely have set null as default value in your columns (if data is empty), that's why you're seeing null in every row.

    To solve this issue, remove these lines from your Model:

    public $data_par;
    public $ora_part;
    public $data_arrivo;
    public $ora_arrivo;
    public $citta_part;
    public $citta_arrivo;
    public $wifi;
    public $bagno;
    public $ac_dc;
    public $condizioni;
    

    These virtual attributes are being used differently from DB attributes and I think in your case you don't need them here.