I
I
Ivan Komarov2017-01-30 10:43:26
Yii
Ivan Komarov, 2017-01-30 10:43:26

YII: What are the reasons why Model->Save() may not be saved to the database?

Model:

namespace common\models;

use Yii;
use yii\base\Model;

class Messages extends \yii\db\ActiveRecord
{
    public $message;
    public $subject;
    public $creation_date;
    public $update_date;
    public $status;
    public $user_id;
    public $parent_id;


    public function rules()
    {
        return [
            'blank' => [
                ['subject', 'message'],
                'required',
                'message' => 'This field can not be blank',
            ]
        ];
    }

    public function saveDialog() {
        if ($this->validate()) {
            $dialog = new Messages();
            //$dialog  = Messages::findOne(1);
            $dialog->message = '111';
            $dialog->subject = "1222";
            var_dump($dialog);
            return $dialog->save();
        } else {
            return false;
        }
    }
}

Everything runs without errors and an empty row is created in the table.
here is a dump of what is sent to save()
object(common\models\Messages)#113 (15) {
  ["message"]=
  string(3) "test"
  ["subject"]=
  string(4) "test"
  ["creation_date"]=
  NULL
  ["update_date"]=
  NULL
  ["status"]=
  NULL
  ["user_id"]=
  NULL
  ["parent_id"]=
  NULL
  ["_attributes":"yii\db\BaseActiveRecord":private]=
  array(0) {
  }
  ["_oldAttributes":"yii\db\BaseActiveRecord":private]=
  NULL
  ["_related":"yii\db\BaseActiveRecord":private]=
  array(0) {
  }
  ["_errors":"yii\base\Model":private]=
  NULL
  ["_validators":"yii\base\Model":private]=
  NULL
  ["_scenario":"yii\base\Model":private]=
  string(7) "default"
  ["_events":"yii\base\Component":private]=
  array(0) {
  }
  ["_behaviors":"yii\base\Component":private]=
  array(0) {
  }
}

And the strangest thing for me is that exactly the same code in another model works fine (

Answer the question

In order to leave comments, you need to log in

1 answer(s)
M
Maxim Fedorov, 2017-01-30
@FreeTibet

delete it

public $message;
public $subject;

look at the source code of ActiveRecord - how attributes are arranged in it, then you will have no questions.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question