Answer the question
In order to leave comments, you need to log in
(YII2) How to make autoincrement work when adding record to Mysql table?
There is:
table Test (id(AUTO_INCREMENT), text)
I do:
$test = new Test();
$test -> text = 'test1';
$test -> save();
$test -> id = 1;
$test -> text = 'test1';
$test -> save();
Answer the question
In order to leave comments, you need to log in
It turned out that the ID field was entered in the rules of the model (created via gii),
[['id', 'text'], 'required'], left it like this: [['text'], 'required'], it worked, thanks everyone!
and in the DB is worth primary autoincrement? If the record is not saved - look (show) $model->getErrors() - we will be able to understand the reason for the non-saving.
When creating a table, mark the required field as AUTO_INCREMENT
CREATE TABLE animals (id MEDIUMINT NOT NULL AUTO_INCREMENT,
name CHAR(30) NOT NULL, PRIMARY KEY (id));
INSERT INTO animals (name) VALUES ("dog"),("cat"),("penguin"),
("lax"),("whale");
Use migrations, it's very easy to add and work with them. In this case, you will not have such problems.
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question