Q
Q
qrolf2017-09-11 14:09:23
Yii
qrolf, 2017-09-11 14:09:23

Why does not write data to the yii2 database?

For some reason the data is not being written to the database.

$transfer = new TransferOperations();

$transfer->from_user = $fromUser;
$transfer->to_user = $toUser;
$transfer->money = $money;
$transfer->created_at = time();

$transfer->save();

Model content
public static function tableName()
    {
        return 'transfer_operations';
    }

    /**
     * @inheritdoc
     */
    public function rules()
    {
        return [
            [['created_at', 'to_user', 'from_user'], 'required'],
            [['created_at'], 'integer'],
            [['money'], 'number'],
            [['to_user', 'from_user'], 'string', 'max' => 255],
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels()
    {
        return [
            'id' => 'ID',
            'created_at' => 'Created At',
            'to_user' => 'To User',
            'from_user' => 'From User',
            'money' => 'Money',
        ];
    }

Everything is ok, no errors, but nothing gets into the database.

Answer the question

In order to leave comments, you need to log in

[[+comments_count]] answer(s)
D
Dmitry, 2017-09-11
@slo_nik

Good afternoon.
First, do a check

if($transfer->save()){
  // какое-то действие
}
else{
  // показать все ошибки
}

You can show all errors using getErrors()
Secondly, replace this squalor
with a public method in the model
use yii\behaviors\TimestampBehavior;

    public function behaviors()
    {
        return [
          TimestampBehavior::className()
        ];
    }

p.s. Exactly!!! As suggested in a comment to my answer, remove created_at from validation and do as I wrote above.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question