-
-
Notifications
You must be signed in to change notification settings - Fork 192
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Gii sets wrong model rule for integer field with default value #335
Comments
This was added for sake of #224. |
I submitted a PR, not sure how to test other than what I did below (manual tests): My test DB tables: create table brand (id serial primary key, code character varying(20));
create table country (id serial primary key, code character varying(20));
create table region (id serial primary key, code character varying(20));
create table state (id serial primary key, code character varying(20));
create table car (
id serial primary key,
brand_id integer references brand(id) not null default 1,
country_id integer references country(id) not null,
region_id integer references region(id),
state_id integer references region(id) default 2,
original_code character varying(20) not null,
country_specific_code character varying(20),
internal_code character varying(20) not null default 'Secret-code',
external_code character varying,
media_code character varying(20) not null
);
Current gii output: public function rules()
{
return [
[['brand_id', 'country_id', 'region_id', 'state_id'], 'default', 'value' => null],
[['brand_id', 'country_id', 'region_id', 'state_id'], 'integer'],
[['country_id', 'original_code', 'media_code'], 'required'],
[['external_code'], 'string'],
[['original_code', 'country_specific_code', 'internal_code', 'media_code'], 'string', 'max' => 20],
[['brand_id'], 'exist', 'skipOnError' => true, 'targetClass' => Brand::className(), 'targetAttribute' => ['brand_id' => 'id']],
[['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => Country::className(), 'targetAttribute' => ['country_id' => 'id']],
[['region_id'], 'exist', 'skipOnError' => true, 'targetClass' => Region::className(), 'targetAttribute' => ['region_id' => 'id']],
[['state_id'], 'exist', 'skipOnError' => true, 'targetClass' => Region::className(), 'targetAttribute' => ['state_id' => 'id']],
];
} After gii code is updated: public function rules()
{
return [
[['brand_id', 'country_id', 'region_id', 'state_id'], 'integer'],
[['brand_id'], 'default', 'value' => 1],
[['state_id'], 'default', 'value' => 2],
[['country_id', 'original_code', 'media_code'], 'required'],
[['external_code'], 'string'],
[['internal_code'], 'default', 'value' => 'Secret-code'],
[['original_code', 'country_specific_code', 'internal_code', 'media_code'], 'string', 'max' => 20],
[['brand_id', 'country_id', 'state_id'], 'unique', 'targetAttribute' => ['brand_id', 'country_id', 'state_id']],
[['brand_id'], 'exist', 'skipOnError' => true, 'targetClass' => Brand::className(), 'targetAttribute' => ['brand_id' => 'id']],
[['country_id'], 'exist', 'skipOnError' => true, 'targetClass' => Country::className(), 'targetAttribute' => ['country_id' => 'id']],
[['region_id'], 'exist', 'skipOnError' => true, 'targetClass' => Region::className(), 'targetAttribute' => ['region_id' => 'id']],
[['state_id'], 'exist', 'skipOnError' => true, 'targetClass' => Region::className(), 'targetAttribute' => ['state_id' => 'id']],
];
} |
If default values are set in the database, why do you need it in your model? |
@schmunk42 the issue was that
was generated while default value was actually present in DB. |
I was referring to those two lines in the patched output
|
But my view is mirroring full table requirements in model. That allow in validation rules trust on default values. Currently my know problem is with require, but any other problems can occur. |
If there's a default value in the DB, I think it's debatable whether you want that in your model. If you "just" change it in the DB, you have to regenerate your code, otherwise you might end up in setting the wrong default value in your app. Even more special is the case if you have a default value and But I also understand your point, that's why I mentioned making it optional in #420 (comment). |
We had the same argument (db would handle default values), and the conclusion was:
|
What steps will reproduce the problem?
Next, create
child
table with FK pointing toparent.id
Next, generate models via
Gii/model
What is the expected result?
What do you get instead?
Additional info
The text was updated successfully, but these errors were encountered: